Hello everyone. I've been working as a consultant for a small printing company, and now that the main project is done, they've asked me to help with any number of "computer related" projects. I have 15 year old BS in computer programming and the languages I know today are proprietary or outdated. (When I learned C, the course was called "C/C+", to give you an idea.)
Ultimately, I need to create a standalone program to convert any "single stream" file into a "multiple stream" file, whether that's 2-up, 4-up, etc.
For example, let's say I have this .csv file:
John Doe, 5559091234
Jane Doe, 5559092345
Jack Doe, 5559093456
Jill Doe, 5559094567
Jerry Doe, 5559095678
Julie Doe, 5559096789
The output for a 2-up program would be a new .csv file:
John Doe, 5559091234, Jane Doe, 5559092345
Jack Doe, 5559093456, Jill Doe, 5559094567
Jerry Doe, 5559095678, Julie Doe, 5559096789
The output for a 3-up program would be:
John Doe, 5559091234, Jane Doe, 5559092345, Jack Doe, 5559093456
Jill Doe, 5559094567, Jerry Doe, 5559095678, Julie Doe, 5559096789
(Hope that makes sense)
In my head, it seems like a simple enough problem. Read in X number of records, where X = the "multiple up" you need as output. But I don't know if I should be using a multi-dimensional array, or reading records into a string first, or something else entirely.
Any hints or sample code to get me started would be greatly appreciated.
Until next week, I won't be able to install a C compiler here at work, but I'll be working on my own compiler at home over the weekend. I'd love to get advice throughout the process as well.
int ups = 3; //lets say
while(not end of file)
{
int index = 0;
while(index is less than ups)
{
read a line and output to file
increment index;
if(end of file encountered)
{
break from the loop;
}
output a comma here to the output file
}
output new line to the file
}
I hope you will understand what i mean.
to read a file:
Your idea seems fine to me. Read X records, then print X records on the same line, print a newline, and repeat.
I personally would read X records into a vector of strings, loop over the vector and print each record. Outside the loop, print a newline, clear() the vector then start over again.
that's fine too, i didn't said about vector because may be he need to learn stl for that and which may effect his timelines.
Also, that will increase the loops because we will read in the loop and for printing we need more loops. what you say?
Never said it couldn't be optimized. ;) Your loop is more efficient, but checking on end of file only will loop infinitely if there is a problem with reading from the file.
Okay, I'm missing something. There are a few things that aren't really necessary in the code, and I tried to get fancy and add a menu before the program was working then dumped it so it's a bit messy; but I have 2 main problems right now:
1. After I enter a number at the prompt, it seems to enter an endless loop. The output file keeps growing, and I can't seem to figure out why it's not reaching the eof. (My testfile is only 160 lines long.)
2. It was working before, but writing out each input record to its own line in the output record.
#include <iostream>
#include <fstream>
#include <string>
usingnamespace std;
void menu(void);
void readData(void);
int NumberUp = 1;
constchar InFileName[] = "C:/CardInput.txt";
constchar OutFileName[] = "C:/CardOutput.txt";
int main()
{
menu();
cin.ignore(1);
return 0;
} //end main
void menu(void){
//allow user to choose to append records, display records or exit the program
// char Choice = 0;
cout << "\nInput the Repeat number: ";
cin >> NumberUp;
readData();
return;
}//end menu
void readData(void){
//read data from a file
//write out a number of Input Records for each Output Record equal to number entered by user.
string lineBuffer;
ifstream inMyStream (InFileName); //open input file
ofstream outMyStream (OutFileName,ios::app); //open output file
if (inMyStream.is_open()){
if (outMyStream.is_open()){
//read the text file
int Counter = 0;
int InputRecord = 0;
int OutputRecord = 0;
while (!inMyStream.eof()){
//get a line of text
while (Counter < NumberUp){
getline (inMyStream, lineBuffer);
InputRecord++;
Counter++;
outMyStream << lineBuffer << ",";%
}
outMyStream << endl;
OutputRecord++;
}
cout << "Processing Complete: " << NumberUp << "-Up Program." << endl;
cout << "Total Input Records: " << InputRecord << endl;
cout << "Total Output Records: " << OutputRecord << endl;
inMyStream.close(); //close the input file
outMyStream.close(); //close the output file
}
else{
cout << "Error: Open Output File." << endl;
}
}
else{
cout << "Error: Open Input File." << endl;
}
}//end read data
There might be an error reading the file, causing you to never reach the eof(). So the loops never terminate. Try looping on .good() instead and see if you break out early for some reason.
Hmm...I did that and I'm not writing anything out. Exits the loop with nothing written to the output file. I'll be honest, I'm at a total loss. I keep looking at the code, and it looks fine to me. But obviously I'm not an expert. Anymore suggestions?