Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/161.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;getline和append_C++_String_Append - Fatal编程技术网

C++ C++;getline和append

C++ C++;getline和append,c++,string,append,C++,String,Append,我试图编写一个非常简单的程序,逐行读取其标准输入(直到“end”出现在行首)。同时,它尝试构造一个包含所有行的串联的新字符串 这种行为令人费解。行被正确读取(如cout所示,我发现了问题。我的输入文件是一个带有CRLF行终止符的ascii文件(我使用的是mac).construct变量创建正确,但终端未正确显示。我将.txt文件的内容复制到Word,并删除了所有硬返回,这很有效,但并不总是理想或可能的。字符编码似乎没有影响。当我添加d断线和断线 Text::Text(string file) {

我试图编写一个非常简单的程序,逐行读取其标准输入(直到“end”出现在行首)。同时,它尝试构造一个包含所有行的串联的新字符串


这种行为令人费解。行被正确读取(如
cout所示,我发现了问题。我的输入文件是一个带有CRLF行终止符的ascii文件(我使用的是mac).construct
变量创建正确,但终端未正确显示。

我将.txt文件的内容复制到Word,并删除了所有硬返回,这很有效,但并不总是理想或可能的。字符编码似乎没有影响。当我添加d断线和断线

Text::Text(string file) {
    ifstream in;
    string line;
    in.open( file.c_str() ); // because the file name is a parameter decided at runtime in my code
    while(getline(in,line,'\n')){
        fileContents.append(line+"\n"); // fixed by adding "\n" here
    }
    cout << "\n Final product:\n";
    cout << fileContents;
}
Text::Text(字符串文件){
如果输入;
弦线;
in.open(file.c_str());//因为文件名是在我的代码中运行时确定的参数
while(getline(in,line,'\n')){
fileContents.append(line+“\n”);//通过在此处添加“\n”进行修复
}

cout.我们可以看到一些输入和输出示例吗?在Visual Studio 2010 Express中也可以。我也有同样的问题。在Cygwin中,它只显示最后一行,但在Netbeans中,它显示了整个内容。非常令人沮丧。
g++ -o main main.cpp -Wall -std=c++0x  
abcdef
ghij
end
abcdef
ghij
ghijef
Text::Text(string file) {
    ifstream in;
    string line;
    in.open( file.c_str() ); // because the file name is a parameter decided at runtime in my code
    while(getline(in,line,'\n')){
        fileContents.append(line+"\n"); // fixed by adding "\n" here
    }
    cout << "\n Final product:\n";
    cout << fileContents;
}