C++逐行读取文件

使用getline()函数可实现逐行读取:

    ifstream in(argv[1]);
    if (!in.is_open())
        cout << "Error" << endl;
    else
    {
        string buf;
        while (!in.eof())
        {
            getline(in, buf);
            cout<<buf<<endl;
        }
    }
以下是一些可能的 C++ 语言优化方法: 1. 对于文件不存在的情况,可以使用异常机制来处理,避免直接退出程序。 ```c++ try { std::ifstream infile(filename); if (!infile) { throw std::runtime_error("文件打开失败"); } // 读取文件内容并输出到屏幕上 std::string data; while (std::getline(infile, data)) { std::cout << data << std::endl; } // 关闭文件 infile.close(); } catch (std::exception& ex) { std::cerr << ex.what() << std::endl; return 1; } ``` 2. 在读取文件时,可以使用文件流对象的 read 函数进行二进制读取,避免字符编码的转换。 ```c++ std::ifstream infile(filename, std::ios::binary); if (!infile) { std::cerr << "文件打开失败" << std::endl; return 1; } // 获取文件大小 infile.seekg(0, std::ios::end); std::size_t filesize = infile.tellg(); infile.seekg(0, std::ios::beg); // 读取文件内容并输出到屏幕上 std::vector<char> buffer(filesize); infile.read(buffer.data(), filesize); std::cout.write(buffer.data(), filesize); // 关闭文件 infile.close(); ``` 3. 在读取文件时,可以使用多线程进行处理,提高读取效率。 ```c++ std::ifstream infile(filename, std::ios::binary); if (!infile) { std::cerr << "文件打开失败" << std::endl; return 1; } // 获取文件大小 infile.seekg(0, std::ios::end); std::size_t filesize = infile.tellg(); infile.seekg(0, std::ios::beg); // 读取文件内容并输出到屏幕上 std::vector<char> buffer(filesize); std::vector<std::future<void>> futures; for (int i = 0; i < std::thread::hardware_concurrency(); ++i) { std::size_t start = i * filesize / std::thread::hardware_concurrency(); std::size_t end = (i + 1) * filesize / std::thread::hardware_concurrency(); futures.push_back(std::async(std::launch::async, [&infile, &buffer, start, end]() { infile.seekg(start, std::ios::beg); infile.read(buffer.data() + start, end - start); })); } for (auto& future : futures) { future.wait(); } std::cout.write(buffer.data(), filesize); // 关闭文件 infile.close(); ``` 4. 在读取文件时,可以使用内存映射方式进行读取。需要注意的是,内存映射方式适用于较大的文件,且需要使用操作系统提供的 API。 ```c++ #include <sys/mman.h> #include <fcntl.h> #include <unistd.h> std::string filename = argv[1]; int fd = open(filename.c_str(), O_RDONLY); if (fd == -1) { std::cerr << "文件打开失败" << std::endl; return 1; } struct stat sb; if (fstat(fd, &sb) == -1) { std::cerr << "获取文件大小失败" << std::endl; close(fd); return 1; } char* addr = (char*)mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0); if (addr == MAP_FAILED) { std::cerr << "内存映射失败" << std::endl; close(fd); return 1; } std::cout.write(addr, sb.st_size); munmap(addr, sb.st_size); close(fd); ``` 需要注意的是,内存映射方式需要手动进行内存释放和文件关闭操作。另外,对于 Windows 系统,需要使用 CreateFile 和 CreateFileMapping 等 API 来进行内存映射。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值