如何将特定字符从TXT文件输出到特定点

How do I output specific characters from a txt file to specific points?

本文关键字:文件 输出 TXT 字符      更新时间:2023-10-16

教程的第一个任务,我已经很困惑了,

对,我应该将3个数字写入文本文件,打开该文件,输出所有3个数字和平均值。设法完成了前两个部分,但我已经在实际输出部分撞到了墙。

这是文本文件的内容,就像文件中所显示的那样:

25

10

12

这是我到目前为止的代码:

#include <fstream>
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
// Create an ifstream input stream for reading of data from the file
ifstream inFile;
inFile.open("ProgrammingIsFun.txt");
// Create an ofstream output stream for writing data to a file
ofstream outFile;
outFile.open("Results.out");

cout << "The first integer is " << endl;
cout << "The second integer is " << endl;
cout << "The third integer is " << endl;
cout << "The average is " << endl;
// Close the files since we're done with them
outFile.close();
inFile.close();
system("Pause");
return 0;
}

据我了解,txt文件的内容只能包含这3个数字,而没有其他内容(虽然我可能错了)

任何帮助都将不胜感激。

我猜想,从文件中读取整数的首选方法是:

int first, second, third;
inFile >> first;
inFile >> second;
inFile >> third;
然后,您可以使用&lt;&lt;可以类似地输出Outfile的操作员。