How to edit a text file content ?

Hello, i am making a library program that can store books and their information, eg.. Title, author.. to a text file. My problem is how can i edit the text file content or delete a line.

This is what the text file look like:
The C++ Programming Language
Bjarne Stroustrup
The C Programming Language 2nd Edition
Daniel Kerninghan & Dennis Rithcie


What if i want to replace Line 2 with something else? How do i do that..

By the way this is my addBook() function:
This works correctly but as i said how can i edit file content?

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
void addBook()
{
    // Create write only object
    // ios::out --open for output only
    // ios::app --writing operations performed at the end of file
    ofstream listOfBooks (CONFIG_FILE, ios::out | ios::app);
    clearScreen();
    
    string title, author;
    cout <<"====================\n";
    cout <<"     Add a book     \n";
    cout <<"====================\n";
    cout <<"#Enter Information Below:\n";
    
    cout <<"\nTitle: ";
    getline (cin, title);
    cout <<"Author: ";
    getline (cin, author);
	
    // Write to File:
    listOfBooks << title <<"\n";
    listOfBooks << author <<"\n";

    cout <<"\n\n#Succesfully added:\n\n";
    cout << "--" << title << "\n";
    cout << "--" << author;
    cout << "\n\n\n\n#Press any key to continue.";
    getch();
    
    listOfBooks.close(); // Close File
}
read file to memory, modify what you want, write memory content to file.
You can use an STL object, an array, (if you know the size) or write your own linked list.

At the beginner level, (unless your instructor tells you not to) I would suggest using a vector. Vectors can be dynamically resized, randomly accessed, and have convenient member functions.

Another methode would be to use the getline(istream&, string&), and write a new file, then erase the old.
Ok, thank you all, i'll google for that.. now that i have an idea how to do it .
Topic archived. No new replies allowed.