Removing \n from end of file output?

I am using a linked list to create a password class. One of the methods is to update the password.txt file that is formatted as such:

1111
2222
3333
4444
5555

The code to output the file:

1
2
3
4
5
6
7
8
9
10
11
12
13
void password::updatePassList(){
    ofstream outFile;
    current = first;    
    
    outFile.open("password.txt");   //opens output file

    current = first;
    while(current != NULL){
        outFile << current->info << endl;
        current = current->link;
    }
    outFile.close();           //close file
}


After the function runs it leaves the cursor on a new line in the .txt file ex:
5555
<------cursor is here.

When I reload the program is copies the last password twice ex:
5555
5555
Then the next time it will show 3 times etc etc etc.

Is here a way to ignore that final /n? thanks
Maybe there is something wrong with the code that reads from the file.
What does the read code look like?

BTW, don't use open/close on fstreams.
read 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
void password::createList(){
    int num = 0;
    
    ifstream inFile;

    first = NULL;

    inFile.open("password.txt");	// Open Input File
	if(!inFile.is_open()) {			// Check if the file didn't open.
		cout << "Can't open input file. Closing." << endl;
		system("PAUSE");
		exit(1);					// abnormal termination of the program.
	}

    while(!inFile.eof()){    //Begin EOF while loop
    
    inFile >> num;
    
    newNode = new password;

    newNode->info = num;

    newNode->link = NULL;

    if(first == NULL){
        first = newNode;
        last = newNode;
    }
    else{
        last->link = newNode;
        last = newNode;
    }

}

}
http://www.parashift.com/c++-faq/istream-and-eof.html


Thank you for this. I changed

1
2
3
while(!inFile.eof()){    //Begin EOF while loop
    
    inFile >> num;


to

while(inFile >> num)

Problem cleared right up :)
Topic archived. No new replies allowed.