简体   繁体   中英

add or remove from line or character from txt file in cpp

I am working on a program of Airlines. it have 4 options on main screen as:

1.Our Services \\tells the timings of flights 2.Flight Information \\**here is the problem defined below. 3.Book Your Seat \\as name shows for booking seat. 4.Check Your States \\takes name of user and tells his flight name and time

the program is running but i want that in second option Flight Information to tell that how many seats are booked in any flight. to do that i used the logic that whenever user books seat int count is increased by one and write it in file right after that flight name. and next time replace is by new number. but i an unable to write and replace that count in file at that specific location. here is my try to write in file on specific line.but it writes in the end of file:

#include <iostream>
#include <fstream>
#include <string.h>
using namespace std;
main()
{
    string a,b;
    cout<<"Enter your name :: ";
    getline(cin, a);
    fstream as("status.txt", ios::in|ios::app);
    while(!as.eof())
    {
    getline(as, b);
    if(a==b)
    {
        as<<"34";
    }
}
}

Except if you explicitly change the write position in the file all the writes will be done at the end of the file, whatever you also read into it or not

Look at fstream::seekg fstream::seekp

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM