Hello all,
I am working on an assignment for my OOP1 class that requires reading a .DAT file, performing a bubble sort according to user input using the keyboard and then writing the sorted data to an output.DAT file. I am able to get my program to compile with no errors and the program accepts user input and says it has written the sorted data to the specified output file. But when I check the output file, there is nothing written to it. Please point me in the right direction and tell me what I may be overlooking.

Thank you

#include <stdio.h>
#include <iostream>
#include <fstream>
#include <string>

using namespace std;


int main(int argc, char*argv[])
{
	ifstream in ("C:\Assignment_4_Smith_Stacey_CIS326\clientListInput.DAT");
	

	int numRecords = 0;
	string line;
	while (!in.eof()){
		getline(in, line);

		if (in.fail()){
		
			break;
		}
		numRecords += 1;
	}

	in.close();

	string *clients = new string[numRecords];
	string *businessTypes = new string[numRecords];

	in.clear();
	in.open("C:\Assignment_4_Smith_Stacey_CIS326\clientListInput.DAT");

	int pos =0;
	while (!in.eof()){
		getline(in, line);
		if (pos < numRecords){
			clients[pos] = line.substr(0, line.find(" : "));

			businessTypes[pos]= line.substr(line.find(" , 1") +1);
			pos +=1;
		}
		if (in.fail()){
			break;
		}
	}
	int option = 0;
	while (option !=3){
		cout <<"Sort by Business or Client?" <<endl;
		cout <<"To sort by Client, enter 1" <<endl;
		cout << "To sort by Business Type enter 2" <<endl;
		cout << "To exit program enter 3" <<endl;
		cin>>option;
		cin.clear();
		cin.ignore(INT_MAX,'\n');

		string temp;
		bool loop = true;
		if (option == 3){
			break;
		}
		if (option == 1){

			while (loop){
				loop = false;
				for (int i = 0; i<numRecords -1; i++){
					if (clients[i].compare(clients[i+1])>0){
						
						temp = clients[i];
					    clients[i] = clients [i+1];
					    clients[i+1] = temp;

					    temp = businessTypes[i];
					    businessTypes[i] = businessTypes [i+1];
					    businessTypes[i+1] = temp;
					    loop = true;
					}
				}
			}
		}
		else if (option ==2) {
			loop = true; 
			while (loop){
				loop = false;
				for (int i = 0; i < numRecords - 1; i++){
					if (businessTypes[i].compare(businessTypes[i +1]) >0) {
						
						temp = businessTypes [i];
						businessTypes[i] = businessTypes[i+1];
						businessTypes[i+1] = temp;

						temp = clients[i];
						clients[i] = clients [i+1];
						clients [i+1] = temp;
						loop = true;
					}

				}
			}
		}
		
		cout << endl <<"Client List has been sorted and copied to 'clientListOuput.DAT.'" << endl;
		}

		ofstream ofs ("C:\Assignment_4_Smith_Stacey_CIS326\clientListOutput.DAT");
		for (int i = 0; i<numRecords; i++){
			ofs <<clients [i] << " : " << businessTypes[i] << endl;
		}

		ofs.close();

		delete[] clients;
		delete[] businessTypes;
		return 0;
	}

Ok, so I don't have compiler where I am, and even if I did, I don't have your .dat file, or an example of the information therein, so I couldn't test your program even if I did. Having said that I offer the following:

1) Don't use the output of eof() to control a loop. It will lead to reading in an extra item sooner or later.

The technical explanation for why this happens is a bit wierd, but it goes something like this: File are separated from each other in memory by an end of file marker. eof() uses that marker to determine if you have reached the end of the current file or not. However, eof() returns a value of true when you try to read past the end of the file, not when you find the end of the file. Therefore, the body of the loop will be implemented when you read in the end of file marker, causing an "extra" read, probably, but necessarily duplicating the last "valid" file entry in your program. The next time when you try to read past the end of file marker, eof() will return false, the not true condition will be interpreted as false and the loop will stop, but one time too late. There are probably better explanations someplace, but that's close enough.

Instead of you call to eof() in the while conditional use the call to getline() and this (potential)problem should be solved/avoided.

2) I believe you are correct in terms of clearing the ifstream after reading completely through the file but I'd try clearing it before I closed the file, not after.

3) Have you tried printing clients[pos] and business[pos] to the screen or viewed the results in your debugger to know that you have succesfully read in the values you think you have.

4) When specifying the full path of a file you may need to use \\ instead of \ as \ is a default character telling the compiler to interpret the following char different than usual. For example \t tells the compiler that t should be tab and not lower case alphabetical t, etc. You might try something like "C\\OOPS\\myName\\program33.dat".

Good luck.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.