哪里来的牛马 2022-02-12 20:10 采纳率: 100%
浏览 21
已结题

java怎么转C++呢

private String getFileContent(File file) {
String content = "";
if (!file.isDirectory()) {
try {
InputStream instream = new FileInputStream(file);
if (instream != null) {
InputStreamReader inputreader
= new InputStreamReader(instream, "UTF-8");
BufferedReader buffreader = new BufferedReader(inputreader);
String line = "";
while ((line = buffreader.readLine()) != null) {
content += line;
}
instream.close();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return content;
}

  • 写回答

1条回答 默认 最新

  • fortunely2 2022-02-13 10:43
    关注

    这是一次读取完一个文件吧,有很多种方法。这里举2个典型的:

    1. 用fread按指定缓冲区大小循环读取完毕整个文件;
    2. 用文件流ifstream 按行读取完文件;
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <unistd.h>
    #include <iostream>
    using namespace std;
    
    /**
     * fread读取文件
     * 适合读小文件,一次读取完毕
     * @param filepath 文件路径
     * @return 文件文本内容
     */
    string readFile(const char* filepath)
    {
        assert(filepath != nullptr);
        FILE* fp;
        string content;
        char buf[1024]; // 自定义缓冲区大小
    
        // 获取文件大小
        struct stat st;
        if (stat(filepath, &st) < 0) {
            perror("stat error");
            return content;
        }
        int filelen = st.st_size;
    
        // 文本模式打开文件
        fp = fopen(filepath, "r");
        if (!fp) {
            perror("open file error");
            return content;
        }
        cout << "open file success: " << filepath << endl;
        int nleft = filelen;
        while (nleft > 0) {
            int nread = fread(buf, sizeof(buf[0]), sizeof(buf), fp); // 按1byte从fp读取最多buf缓冲区大小字节数
            if (nread == 0) { // complete reading
                break;
            }
            else {
                buf[nread] = '\0';
                content += buf;
                nleft -= nread;
            }
        }
        fclose(fp);
        return content;
    }
    
    #include <fstream>
    /**
     * 文件流读取指定文件
     */
    string readFileByStream(const char* filepath)
    {
        string content;
        char buf[256];
    
        ifstream in("test.txt"); // 只读模式打开文件
        if (!in.is_open()) {
            cerr << "open file error: " << filepath << endl;
            return content;
        }
    
        while (!in.eof()) {
            in.getline(buf, sizeof(buf));
            content += buf;
    
            if (!in.eof()) { // ifstream::getline 会替换\n为\0, 还原时可根据需要加上\n
                content += '\n';
            }
        }
        return content;
    }
    // 测试用例,文件"test.txt"跟当前程序位于同一路径
    int main()
    {
        cout << readFileByStream("test.txt") << endl;
        cout << readFile("test.txt") << endl;
        return 0;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录

报告相同问题?

问题事件

  • 系统已结题 2月22日
  • 已采纳回答 2月14日
  • 创建了问题 2月12日

悬赏问题

  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟