|
||||||
| 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. |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
|
|||
|
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 |
| Sponsored Links |
|
|
|
|||||
|
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! |
|
|||
|
#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... |
|
|||
|
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. |
| Sponsored Links |
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
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 |
| 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 |