October 5th, 2009, 07:49 AM
|
#1 (permalink)
|
| Junior Member
Join Date: Oct 2009
Posts: 1
|
Hello everyone,
I am new to this forum, so please excuse me if I started this thread at the wrong place.
I wrote a c++ program using the turboc 3 ide, may be rather old version, but I can't help as our school has that version only.
The program aims at reading a text file containing numbers, and 1.output the same on to the screen 2. write it to another file to save it as a binary file.
The code I wrote worked for the first part, however, the last line of the file is being read twice. Seems the program does not understand that it has reached the eof.
The code is: #include <iostream.h>
#include <fstream.h>
#include <conio.h>
void main(){
clrscr();
fstream fp;
fp.open ("temp.txt", ios::in||ios: ut);
int d, m;
while(!fp.eof())
{fp>>d>>m;
cout<<d<<" "<<m<<"\n";}
fp.close();
} I used a text file containing numbers 12 15
15 16
21 35
45 50
12 19
The output is given correctly except that the last line 12 19 is repeated.
2 Can I open a text file as a binary file (so that I need not write some code for the second part?)
Thanks in advance |
| |