C++ programming help needed

Status
Not open for further replies.
You'll have to be careful with Flux Capacitor's sample code because the line buffer is limited to 100 characters. Usually i would use dynamic storage in this case, like a string.

#include <string>
#include <fstream>
#include <iostream>
using namespace std;

string buffer;

//file opener
getline(cin, buffer);
ifstream file(buffer);

//file reading code with string
while(!file.eof())
{ getline(file, buffer); //your code here to do what you need to do with file stuff
}
 
Heh, ok, i'll try to save this from being a cheat forum.

Sorry, i didn't see the dot there so i didn't know you had to use the member function to get the line.

Lets take a look at the routine:
/******************************************/
ifstream infile;
ofstream outfile;
infile.open(/*path and filename of input*/);
outfile.open(/*path and filename of output*/);
while( infile.getline(line) )
{ //do processing here}
infile.close();
outfile.close();
/******************************************/

Ok, you should already know that when a file is opened in memory, there really aren't any "lines" but rather end of line markers in a long string of characters. When you make a call to the member function getline, it will return the string of characters from where the current input file pointer starts up to the next end of line/end of file character, however the string will not contain that marker (that is, kill the line feed and carriage return). This string is stored in the character pointer you give to it, in this case is called line and can store up to 100 characters per line. If the length of the "line" exceeds 100 characters, you will probably be truncated. A remedy to this is what i noted before in using dynamic data storage OR if you know beforehand the length of the lines, you can set your array size accordingly (i believe using a regular ol' pointer should work too, but i've always had problems doing things like that and have since stopped).

Now since you want to read in a line and execute code following that, you need to setup a loop. You can see in Screndib's routine, a while conditional loop is used to loop the execution of your code while the condition itself is the statement which obtains a line from the input file. What happens is that the ifstream object will return NULL or false to the while conditional check if it is unable to read from the file any longer (which should be when EOF is reached). This effectively loops the execution of your code for as many lines as your input file has.

You also asked about writing to your output file several times without overwriting. If all the outputs are written to your file on a single execution, a way to make all the outputs appear one after another after execution is to keep your output file object open during the entire execution of your write to output code. For example, if you want to write to the output file in the subcode of the while loop, then you simply open the output file before the loop, do all your writing in the loop, then you can close after the loop OR after any additional appending code. If you need to write to that file in multiple files, you can declare the output file as a global and close it after the completion of your program (usually not recommended) or just open it in your main and pass it by reference to each function that needs to use it. Appending works too, but if you always open your file by appending, each run of your program successively adds to your output file and you might not want that.

Now isn't this a learning experience?
 
Status
Not open for further replies.