location certain line in a file to then write over

I'm trying to have the user enter the information, then entering the bookname to go to that point in the file so it can then be written over.

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
fstream reservationFile;												//creating output stream file to write reservation input

	reservationFile.open("reservationLog.txt");
	(ios::app);
	if(reservationFile.fail()){
		cout<<"File reservation unsuccessfully opened."<<endl
			<<"Please check to see if file exists."<<endl;
		exit(1);
	}
int main(){
int bookName;
int currentDate;
int startDate;
int lengthStay;
cout<<"Please enter book name:"<<endl;								//enter book name
				
				cin>>bookName;
				
				reservationFile<<bookName;									//write book name to reservationFile file
				
		
				cout<<endl<<"please enter toady's date(yyyymmdd):"<<endl;			//enter current date
				cin>>currentDate;	
				reservationFile<<currentDate;								//write current date to reservationfile
				cout<<endl<<"please enter start date:(yyyymmdd)"<<endl;				//enter start date
				cin>>startDate;
				reservationFile<<startDate;									//write start date to reservationfile
			 
				cout<<"Please enter length of stay:"<<endl;						//enter length of stay
				cin>>lengthStay;
				reservationFile<<lengthStay;							//write length of stay to reservation file


			
		
	
																		
			
				cout<<"Welcome to the edit booking menu!"<<endl
					<<"select booking"<<endl;
				cin>>bookName;
				cout<<reservationFile;
				
			
				}
I need to get to bed and take my last final in the morning but I noticed you are using an fstream so I take it that you want to both read and write through it.

I am not positive on this but I believe that it is automatically set to be an input filestream only so you might have to enable both by using:


reservationFile.open("reservationLog.txt", fstream::in | fstream::out);
Last edited on
bump
Show us the the file. Depending on how it is formatted it could be pretty simple in finding and overwriting the booknames.
its not updating to when i run it again, but

mike20111204201112161-858993460m-858993460-858993460-858993460
bump
Ok, so I took some time and browsed this site and I can show you 95% of the way. This code will find the bookname and overwrite it but it will only work perfectly if the number of characters are the same. So I guess it's up to you to figure out how to make it work for different sized booknames. You can do it guy, I have faith in you!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
int main() {

	fstream fin;
	string tmp;
	string name = "Dave";
	string newName = "Joe";
	long pos;
	
	fin.open("reservationLog.txt", fstream::in | fstream::out); 

  	getline(fin, tmp, '-'); 
 
	while (fin) {
	        if (tmp == name) {
			pos = fin.tellp();
			fin.seekp (pos - name.length() - 1);
			fin << newName;			
		}
		getline(fin, tmp, '-'); 
	}
        
        return 0;
}




Last edited on
Topic archived. No new replies allowed.