gpt4 book ai didi

C++ 文件处理错误 : Write and Read from same file and same program

转载 作者:行者123 更新时间:2023-11-28 03:14:40 26 4
gpt4 key购买 nike

我试图在同一个 cpp 程序中写入和读取文件,但出现 3 个错误

conflicting decleration 'std::istream theFile'
'theFile' has a previous decleration as 'std::istream theFile'
no match for 'operator>>' 'in theFile >>n' 

while you answer this question try to be more noob specific.here is my code.

#include<iostream>
#include<fstream>
using namespace std;

int main()
{
int n;
string name;
ofstream theFile;
istream theFile;
theFile.open("Olive.txt");

while(cin>> n>>name)
{
theFile<< n<<' '<< name;
}

while(theFile>>n>>name)
{
cout <<endl<<n<<","<<name;
}

return 0;
}

最佳答案

改用 std::fstream。它可以读/写文件,并做你需要的。因此您不必像 ifstream 和 ofstream 那样打开文件两次。

关于C++ 文件处理错误 : Write and Read from same file and same program,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17298222/

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