如何从Turbo c ++ 4.0文件中读取数据? [英] How to read data from files in Turbo c++ 4.0?

查看:270
本文介绍了如何从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();

 }

文件包含数字: 2 3

输出: 2 0

作为函数调用时输出: 2 -28903 这是非常不同的,如果我称之为一个函数比较时,它在主要是我应该做的
我只是使用它主要看看会发生什么。

Output when called as a function: 2 -28903 which is very different if I call it as a function compared when its in main which is what I was supposed to be doing I was just using it in main to see what happens.

对不起,我们的老师想让我们在Turbo c ++中学习,所以我不
真的有选择与编译器。

Sorry, but our teacher wanted us to learn in Turbo c++ so I don't really have choice with the compiler.

推荐答案

std :: cout 循环:

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

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

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