gpt4 book ai didi

c++ - 将自定义字符串放入文件中

转载 作者:行者123 更新时间:2023-11-28 05:31:45 25 4
gpt4 key购买 nike

我想在现有文件的末尾添加一个新行(字符串)。但它没有用。这是代码:

    #include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>

using namespace std;

int main()
{
ifstream input("Sample.ini");
ofstream output("Sample.ini",ios::app);

cout << "Lines that have existed in file:" << endl;

while (input) // Print out the existed line
{
string newstring;
getline(input,newstring);
cout << newstring << endl;
}

cout << "Line you want to add:" << endl;

string outputstring;
getline(outputstring,output); // get the whole line of outputstring,
// and deliver it into output file
return 0;
}

第一个将文件中的行读取为字符串的 getline 效果很好。但是,第二个,不是。编译器返回如下:

...\file test.cpp|35|error: no matching function for call to 'getline(std::istream&, std::ofstream&)'|

最佳答案

你写了太多代码。你只需要两行:

ofstream output("Sample.ini",ios::app);
output << outputstring;

关于c++ - 将自定义字符串放入文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39340724/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com