如何使用turbo c++ 4.0读取文本文件上的数字? [英] How to read numbers on text files using turbo c++ 4.0?

查看:40
本文介绍了如何使用turbo c++ 4.0读取文本文件上的数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是编程的初学者,我正在尝试编写一个代码,从文件中读取 2 个数字,然后将其显示在 turbo c++ 的输出窗口中.我的代码只读取第一个数字并为第二个数字产生错误的输出.

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();

}

推荐答案

#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();
}

这篇关于如何使用turbo c++ 4.0读取文本文件上的数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆