如果您的文件像您提到的那样非常小,您可以将它放入一个字符串数组(每行文本一个元素)。然后对数组进行更改并将整个数组重新写入文件。
例如,您可以像这样将其读入 arrya:
//assuming you've defined the array A
for(int i = 0; i < 6; i++) //NOTE: I've changed the loop counter i
{
getline(file, line);
A[i] = line;
cout << A[i] < "\n"; //This is the NEW LINE I wanted you to put
//If the ABOVE line gives nothing, you ought to check your TXT file.
}
//Change line 6
A[5] = "555.00";
//Now reopen the file to write
ofstream Account ("accounts.txt");
if (Account.is_open())
{
for(i=0;i<6;i++)
{//NOTE THAT I HAVE INCLUDED BRACES HERE in case you're missing something.
Account << A[i] << "\n"; //Loop through the array and write to file
}
Account.close();
}
我没有对此进行测试,但我认为没关系。更多代码:如果您在主代码末尾添加以下代码,您应该会看到数组中的内容。如果这没有显示任何内容,则表明您的文件为空。
for(int i = 0; i < 6; i++)
{
cout << A[i] < " This is a row with data\n";
}
注意:虽然我想在这个论坛上帮助你澄清问题,但我认为这个问题超出了这个论坛的性质。也许您需要花一些时间学习循环和其他结构的艺术:)