replacing line of text problems

So I'm working on my C++ final and I'm having a problem replacing a line in a text file. The code is supposed to look through the list and replace the work "open" with a name input by the user. This is basically what I have so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
string name;
string seat;
fstream flightOne;

cin>>name;
flightOne.open("flightOne");
for(int i=0; i<5; i++)
{
    getline(flightOne,seat);
    if(seat=="open")
    {
        flightOne<<name<<"\n";
        break;
    }
}

flightOne.txt
bill
bob
open
sam

Now I'm pretty sure this should put the name entered by the user on the line after "open" in the text file, but it doesn't. So if someone could help me get that fixed and tell me how to remove a line from the file (so i can't remove the line that has "open"), that'd be great.
Last edited on
You need to put " " around open, otherwise the compiler will think it is a variable.
woops, sorry, that was just a typo in rewriting the code in the text box. There aren't any errors when I compile the program, so it's nothing like that.
If you're happy with string manipulation you could do something like:

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
#include <iostream>
#include <fstream>
#include <string>

int main()
{
	std::string name;								// String to store your name.
	std::fstream flightOne;							// fstream to hold the file.
	flightOne.open("flightOne.txt");

	std::cout << "Enter your name\n\n  >> ";
	std::cin >> name;
	std::cout << std::endl;

	std::string fileContent;						// String to hold the files contents.

	if(flightOne != NULL)							// If the file is not NULL get it's contents.
	{
		
		std::string line;							// String to temp store each line in the file.

		while(std::getline(flightOne, line))		// Get each line inside the file.
		{
			fileContent += line;					// Add the line to the fileContents string.
			fileContent += "\n";					// Add a line break after each line you get.
		}

		flightOne.close();							// Need to close the file and open it again for writing.

		std::cout << "--- Seats ---" << std::endl;
		std::cout << fileContent;					// Display the flight seats
		std::cout << "-------------" << std::endl;
	}
	else
	{
		std::cout << "File is NULL" << std::endl;	// Error: File is NULL	
	}

	int position = fileContent.find("open");		// Find an 'open' position in the string.

	if( position != -1 )							// If we have a valid position insert a new name into the string.
	{
		std::cout << "Found an open seat at position: " << position << " in the 'string'\n" << std::endl;
		fileContent.erase(position, 4);				// erase 4 positions from where open was found
		fileContent.insert(position, name);			// Insert 'name' to the position open was found.

		flightOne.open("flightOne.txt");			// Open up the text file again.
		flightOne.clear();							// Clear the current data in it.
		flightOne << fileContent;					// Write our updated string to the file.
		flightOne.close();							// Close the file.

		std::cout << "--- Seats ---" << std::endl;
		std::cout << fileContent;					// Display the edited string.
		std::cout << "-------------" << std::endl;
		std::cout << "Inserted " << name << " to the open seat" << std::endl;
	}
	else
	{
		std::cout << "*** Sorry - No seats avaliable ***" << std::endl;
	}

	return 0;
}
Hmmm why do the comments always come up stuffed when I paste them in :\
Topic archived. No new replies allowed.