Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.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++_String_Function_Endl - Fatal编程技术网

C++ 如何使声明为字符串的函数结束行

C++ 如何使声明为字符串的函数结束行,c++,string,function,endl,C++,String,Function,Endl,我有一个函数,从一个文本文件读取,然后输出整个文本文件。 看起来是这样的, string FileInteraction :: read() const { ifstream file; string output; string fileName; string line; string empty = ""; fileName = getFilename(); file.open(fileName.c_str()); i

我有一个函数,从一个文本文件读取,然后输出整个文本文件。 看起来是这样的,

string FileInteraction :: read() const
{
    ifstream file;
    string output;
    string fileName;
    string line;
    string empty = "";


    fileName = getFilename();

    file.open(fileName.c_str());

    if(file.good())
    {
        while(!file.eof())
        {
            getline(file, line);
            output = output + line ;
        }

        file.close();
    return output;
    }
    else
        return empty;

};
cout << FI.read(); //PS I cant change the way it's called so I can't simply put an endl here
我这样调用函数

string FileInteraction :: read() const
{
    ifstream file;
    string output;
    string fileName;
    string line;
    string empty = "";


    fileName = getFilename();

    file.open(fileName.c_str());

    if(file.good())
    {
        while(!file.eof())
        {
            getline(file, line);
            output = output + line ;
        }

        file.close();
    return output;
    }
    else
        return empty;

};
cout << FI.read(); //PS I cant change the way it's called so I can't simply put an endl here
我不想在两行之间留有空隙

所以在调用函数之后,我需要结束这行。 我如何在函数中做到这一点

另外,如果有比我更好的方式输出文本文件中的所有内容,我将非常感谢您的建议。

只需更改即可

return output;

简单地改变

return output;

简化:

std::string fileName = getFilename();
std::ifstream file(fileName.c_str());
std::string output;
std::string line;
while (getline(file, line))
    output.append(line);
output.append(1, '\n');
return output;
简化:

std::string fileName = getFilename();
std::ifstream file(fileName.c_str());
std::string output;
std::string line;
while (getline(file, line))
    output.append(line);
output.append(1, '\n');
return output;

只需返回
output+'\n'
,而不是只返回
output

'\n'
是新行字符的转义码。

只需返回
output+'\n'
,而不只是
output
'\n'
是新行字符的转义码。

这是:

所以在调用函数之后,我需要结束这行。怎么可能 我在函数中这样做

这是荒谬的。您不能在函数中执行调用函数后应该发生的任何操作。如果调用代码没有在应该的时候将
std::endl
发送到
cout
,这是调用代码的问题,您不能也不应该尝试在函数中修复此问题。

所以在调用函数之后,我需要结束这行。怎么可能 我在函数中这样做


这是荒谬的。您不能在函数中执行调用函数后应该发生的任何操作。如果调用代码没有在应该的时候将
std::endl
发送到
cout
,这是调用代码的问题,您不能也不应该尝试在函数中修复此问题。

不执行eof时不要读取。它坏了。你在哪里看到的?忘了那些参考/学习材料吧。这是错误的。正确的读取循环应该是
while(getline(file,line);
.returnoutput+'\n';?
returnoutput+“\n”;
@Blobiu5:那么你需要改变程序中的其他东西,而不是这个函数。编写函数来完成调用代码应该做的事情是非常糟糕的设计。如果你想在调用这个函数后在一个新行开始东西,那么你应该在调用这个函数后开始一个新行,不要试图让它变得有趣这样做,否则你就无法达到将操作分解为函数的目的。你知道……你总是可以添加另外一两个函数。有一个函数只读取一个文件。有一个函数准备输出。有一个函数在顶层,符合外部要求。PS-“我这样调用函数是不正确的措辞。你的教授(或他的助手)这样称呼你的方法,而不是你。不要边读边读。它坏了。你在哪里看到的?忘记那些参考/学习材料。这是错误的。正确的阅读循环应该是
while(getline(file,line);
。return output+'\n';?
return output+“\n”;
@Blobiu5:那么你需要改变程序中的其他东西,而不是这个函数。编写函数来完成调用代码应该做的事情是非常糟糕的设计。如果你想在调用这个函数后在一个新行开始东西,那么你应该在调用这个函数后开始一个新行,不要试图让它变得有趣这样做,否则你就无法达到将操作分解为函数的目的。你知道……你总是可以添加另外一两个函数。有一个函数只读取一个文件。有一个函数准备输出。有一个函数在顶层,符合外部要求。PS-“我这样调用函数你的教授(或他的助手)这样称呼你的方法,而不是你。