Lost Password?


Go Back   CodeCall Programming Forum > Software Development > C and C++

C and C++ C and C++ forum for discussing all forms of C except for C#. These languages are powerful low level languages used for creating Operating Systems, Device Drivers, compilers and much more.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-03-2007, 02:03 AM
ntmurphy ntmurphy is offline
Newbie
 
Join Date: Apr 2007
Posts: 3
Rep Power: 0
ntmurphy is on a distinguished road
Default File I/O (newbie question)

Hello,

I'm a C++ newbie, and I'm running into some problems. I'm trying to create a simple text program that reads from a file, and outputs to another file. However, my while loop executes one too many times, and it always duplicates the very last character in the text file. To control my loop, I'm using

while ( !infile.eof() ) { ....

I was told to use infile >> ws; but when I do this, it skips all of the whitespaces rather than just the end of the file. So an input file that says something like "Test 1 2 3", outputs "Test123". And obviously if I don't use infile >> ws; it outputs "Test 1 2 33". How do I fix this??

Nick
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 04-03-2007, 05:21 PM
Jordan's Avatar   
Jordan Jordan is offline
Administrator
 
Join Date: Nov 2005
Location: Hendersonville, NC
Posts: 9,650
Last Blog:
PHP Objects, Patterns,...
Rep Power: 20
Jordan is just really niceJordan is just really niceJordan is just really niceJordan is just really nice
Send a message via ICQ to Jordan Send a message via AIM to Jordan Send a message via MSN to Jordan
Default

Can you post the full code for reading and writing (including your loop)?
__________________
CodeCall Blog | CodeCall Wiki | Shareware Site | Linux Forum | Write a Blog
The CodeCall Wiki is now fully integrated with vBulletin users! Check it out and add some new pages!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-03-2007, 06:32 PM
ntmurphy ntmurphy is offline
Newbie
 
Join Date: Apr 2007
Posts: 3
Rep Power: 0
ntmurphy is on a distinguished road
Default

#include <iostream>
#include <fstream>

int main(){
using namespace std;

ifstream infile;
ofstream outfile;

infile.open("infile.txt");
outfile.open("outfile.txt");

char ans = 'y';

while ( (infile.fail()) && (ans == 'y') )
{
cout << "File opening failed. Try again? ";
cin >> ans;
if (ans != 'y')
{return 0;}
}

char symbol;
while ( !infile.eof() )
{
infile.get(symbol);
infile >> ws;
outfile << symbol;

}


infile.close();
outfile.close();
return 0;
}

I tried replacing "infile.get(symbol)" with "infile >> symbol", and tried with and without "infile >> ws" in various combinations. But I can't get the program to duplicate the infile exactly in the outfile... Any suggestions? Its gotta be something simple I'm missing...
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 04-04-2007, 12:59 AM
ntmurphy ntmurphy is offline
Newbie
 
Join Date: Apr 2007
Posts: 3
Rep Power: 0
ntmurphy is on a distinguished road
Default

ahh..I fixed it....

Heres what I did below. Just moved the input statement before the loop for the first symbol, then kept it as the last task inside the loop, so before it can execute one last time, it had already passed the end of the file...

...

infile.get(symbol);

while ( !infile.eof() ) {

outfile << symbol;
infile.get(symbol);
}
....

what can I say...clearly a noob here.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 04-04-2007, 03:30 AM
deusprogrammer's Avatar   
deusprogrammer deusprogrammer is offline
Newbie
 
Join Date: Mar 2007
Posts: 4
Rep Power: 0
deusprogrammer is on a distinguished road
Send a message via AIM to deusprogrammer Send a message via MSN to deusprogrammer Send a message via Yahoo to deusprogrammer
Default

Why not just use in.getline() ? In the case of interpreting a text file, it's best to read in the whole line and then parse it in a buffer. Or you can buffer the whole file and then look through it searching for tokens to parse it and interpret the data. For the most part though you don't want to use files like this. It's easier to just output binary and then just read everything back in as binary.
__________________

SUPER MARIO SUNSHINE!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Project: ionFiles - Joomla Simple File Download Jordan Community Projects 331 11-26-2008 12:35 PM
Windows XP Tricks & Tips!!!!..new ones. pranky Tutorials 9 08-23-2008 04:22 PM
text file manipulations in vb6.0 Ronin_paes Visual Basic Programming 3 06-11-2007 05:54 AM
Doubt re: interface LED with TMS320F2812 ?? Newbie question skn82 C and C++ 3 05-07-2007 12:04 PM
Newbie Question Toxic11 Visual Basic Programming 5 08-20-2006 08:52 AM


All times are GMT -5. The time now is 02:03 AM.

Contest Stats

WingedPanther ........ 2753.6
Xav ........ 2704
Brandon W ........ 1702.32
John ........ 1207.73
marwex89 ........ 1175.24
morefood2001 ........ 966.05
dcs ........ 655.75
Steve.L ........ 475.59
orjan ........ 418.58
Aereshaa ........ 383.54

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 100%


Complete - Celebrate!

Ads