How to read numbers on text files using turbo c++

2019-12-16 20:00发布

I am a beginner in programming and I am trying to make a code that reads 2 numbers from a file and then displays it in the output window on turbo c++. My code only reads the first number and produces incorrect output for the second number.

 #include<iostream.h>
 #include<fstream.h>
 #include<conio.h>

void main()
{
    int x, y;
    clrscr();
    ifstream inFile;
    ofstream outFile;
    inFile.open("prac.txt");

    while(!inFile.eof())
    inFile >> x >> y;
    cout << x << " " << y;

    inFile.close();

}

1条回答
别忘想泡老子
2楼-- · 2019-12-16 20:57
#include<iostream.h>
#include<fstream.h>
#include<conio.h>
void main() {
  clrscr();
  ifstream inFile;
  inFile.open("prac.txt");
  while(!inFile.eof()) {
    int num;
    inFile>>num;
    cout<<num<<" ";
  }
  inFile.close();
}
查看更多
登录 后发表回答