Multiple-Up project advice

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.

That may go like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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:
1
2
3
4
5
6
7
8
std::ifstream ifs("input.txt");
std::string line;
while(!ifs.eof())
{
std::getline(ifs, line);
std::cout << line;
}
ifs.close();
Last edited on
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.

Here is the code:
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

void menu(void);
void readData(void);

int NumberUp = 1;
const char InFileName[] = "C:/CardInput.txt";
const char 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 


Sample input file:
98725001,Full Name1
98725002,Full Name2
98725003,Full Name3
...

A lot has come back to me, but not as much as I'd like. I appreciate all the help so far, and thank you for looking this over again.
I'm really at a loss here, and running out of options. Can anyone help me figure out what's wrong? I appreciate the advice.
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?
I got it to work. Thanks for all your help!
Topic archived. No new replies allowed.