home hardware prices news articles forums photos user reviews
Go Back   Tech Support Forums - TechIMO.com > PC Hardware and Tech > Webmastering and Programming
Ask a Tech Support Question (free)!

c++ end of file

Reply
Get bargains at  »  Dealighted.com
 
Thread Tools Search this Thread
Currently Active Users: 1767
Discussions: 200,924, Posts: 2,379,110, Members: 246,290
Old October 5th, 2009, 07:49 AM   Digg it!   #1 (permalink)
Junior Member
 
Join Date: Oct 2009
Posts: 1
c++ end of file

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

c_and_c is offline   Reply With Quote
Old October 11th, 2009, 10:41 AM     #2 (permalink)
Caveat Emptor
 
Rootstonian's Avatar
 
Join Date: Mar 2005
Location: Out of my mind
Posts: 3,241
Send a message via AIM to Rootstonian
1. try changing it to a do/while loop
2. you need to open a second file to write to

You should also check for success on every file open; that is
if(!fp) {
cout << "Unable to open file!";
return 1;
}

Last edited by Rootstonian : October 12th, 2009 at 09:10 AM.
Rootstonian is offline   Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search

Similar Threads
Thread Thread Starter Forum Replies Last Post
Remove File types/Repair file extension .0xx..? atlboi General Tech Discussion 3 November 2nd, 2009 10:35 AM
How to convert .rar file back to a viewable movie file goturk1 Applications and Operating Systems 16 June 21st, 2008 04:06 AM
North Korea agrees to end all nuclear work by end-2007 EXreaction DebateIMO: Politics, Religion, Controversy 39 September 5th, 2007 08:00 PM
High end system needs a high end video card bandin Graphics Cards and Displays 31 July 16th, 2004 03:12 AM
FS New high end, old low end stuff ebaker Traders Forum: Buy, Sell, Trade 10 September 20th, 2002 12:57 AM


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Most Active Discussions
Is It Just Me? (2858)
Obama the Muslim (14)
Why is Khalid Sheikh Mohammed even .. (9)
windows vista security holes (9)
Is the PSU I received dead? (10)
Foreign voltage (10)
HIS HD5770 graphic card question (15)
Print spooler problem (13)
Install XP pro and a Vista laptop ?.. (9)
Dept. of HS: NSA 'Helped' Develop V.. (15)
A good PSU? (10)
New Computer wont recognize XP disc (7)
Ideal cheap graph card for PC-Gamin.. (15)
EVGA 9800 gtx help with finding a g.. (8)
Recent Discussions
Optical Audio A-B Switch (0)
windows vista security holes (9)
Fire in DVD (0)
radeon x850xt platinum & shader 3 (2)
The NTDVM CPU has encountered an ille.. (24)
[F@H SPAM 11/16/09] ! 1/2 months to r.. (34)
Wireless speakers for PC? (11)
Print spooler problem (13)
Help getting around port 80 for camer.. (2)
Display shows 3x5 inch in middle of s.. (3)
monitor will not turn on at all, (1)
World's largest Monopoly Game using G.. (331)
Foreign voltage (10)
FiOS modem/router interfering with ne.. (7)
Browsers wont load websites (2)
Virus Doctor Popup? (1)
Dept. of HS: NSA 'Helped' Develop Vis.. (15)
Install XP pro and a Vista laptop ?? (9)
EVGA 9800 gtx help with finding a goo.. (8)
Modern Warfare For the PC (32)
Problem with speed step/turbo boost? (1)
Modern Warfare 2: Who Bought It? (61)
SIS 740 and Widescreen (8)
Baffling Problem with my CPU/MoBo's. .. (0)
HIS HD5770 graphic card question (15)


All times are GMT -4. The time now is 10:49 PM.
TechIMO Copyright 2009 All Enthusiast, Inc.



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28