c ++ getline在Windows中读取整个文件 [英] c++ getline reads entire file in Windows

查看:237
本文介绍了c ++ getline在Windows中读取整个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这看起来与这一个类似的问题,但是我认为我的情况可能实际上有所不同.代码如下:

This looks like a similar question to this one, however I think my case might actually be a bit different. The code is as below:

void readOmronResults(string fileName)
{
    ifstream inFile(fileName);
    ofstream testRead("test_read.txt");

    string line;
    //getline(inFile, line);
    //cout << line << endl;
    while (getline(inFile, line))
    {
        testRead << line << endl;
    }


    inFile.close();
    testRead.close();

    cout << "Finished reading omron results" << endl;
}

testRead仅用于调试.输入文件是一个.csv文件,如下所示:

testRead is just used for debugging. The input file is a .csv file which looks like:

    IMAGE,RIGHT_EYE_IN_X,RIGHT_EYE_IN_Y,RIGHT_EYE_OUT_X,RIGHT_EYE_OUT_Y,LEFT_EYE_IN_X,LEFT_EYE_IN_Y,LEFT_EYE_OUT_X,LEFT_EYE_OUT_Y
    0001_2m_-15P_-10V_-10H,2386,1627,2171,1613,2754,1623,3009,1583
    0001_2m_-15P_-10V_-15H,2377,1620,2171,1606,2750,1611,3003,1574
    0001_2m_-15P_-10V_-5H,2376,1614,2166,1599,2752,1609,3012,1577
           ...

如果运行上面的代码,则test_read.txt中的输出与输入文件中的输出完全相同.但是,如果我还原了两条注释掉的行,则控制台窗口将显示输入文件中的所有行(从第二行开始重复),并且test_read.txt为空.从链接的文章中,我想它可能与不同操作系统中行尾的不同有关.我的操作系统是Windows,根据我的文本编辑器,原始输入文件是Mac-OS风格.但是,正因为如此,为什么原始代码(两行都被注释掉了)才能给出正确的结果?

If I run the above code, the output in test_read.txt is exactly the same as it is in the input file. However if I restore the two commented out lines, the console window shows all the lines in the input file( repeatedly from the second line) and the test_read.txt is empty. From the linked post I guess it probably has something to do with the difference of line endings in different operating systems. My operating system is Windows and according to my text editor the original input file is Mac-OS style. But if it is because of this, why is the original code (with the two lines commented out) able to give a correct result?

我的IDE是Visual Studio 2012,而我的计算机是64位.

My IDE is Visual Studio 2012 and my machine is 64-bit.

推荐答案

我的操作系统是Windows,根据我的文本编辑器,原始输入文件是Mac-OS风格.

My operating system is Windows and according to my text editor the original input file is Mac-OS style.

是的,这就是问题所在. Windows的C和C ++标准库将假定文本文件使用Windows行尾U+0D U+0A.

Yes, this is the problem. Windows' C and C++ standard libraries will assume that text files use the Windows line ending, U+0D U+0A.

"Mac OS风格"对于文本编辑器来说很奇怪,因为另一行以通用结尾的U+0A结束,这是整个Unix系列(包括Linux)通用的.很久以前,Mac OS使用U+0D使得短语"Mac OS风格"模棱两可且不合时宜.

"Mac OS style" is an odd thing for the text editor to say, because the other line ending in common use U+0A which is common to the entire Unix family including Linux. Long ago, Mac OS used U+0D which makes the phrase "Mac OS style" ambiguous and anachronistic.

但是,正因为如此,为什么原始代码(用两行注释掉)才能给出正确的结果?

But if it is because of this, why is the original code (with the two lines commented out) able to give a correct result?

没有.程序的两个版本都将文件视为包含很长一行的文件.

It didn't. Both versions of the program treat the file as if it contains one very long line.

这篇关于c ++ getline在Windows中读取整个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆