Read from file - Write data in particular position in the file

Is it really possible to write in a text file in the middle or somewhere not in beginning or at the end? Here is my code which reads two lines from the file and I want to modify the second line and write to the file - yes overwriting.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <string>
#include <iostream>
#include <fstream>

    int main()
    {
        std::ifstream file( "data.txt" ) ;
        std::string search_str = "Man" ;
        std::string line ;
        int line_number = 0 ;
        bool gotMatch = false;
        while( std::getline( file, line ) )
        {
            ++line_number;

            if (gotMatch || line.find(search_str) != std::string::npos )
            {
                std::cout << "line " << line_number << ": " << line << '\n' ;
                gotMatch = !gotMatch;
            }
        }

        return (0);
    }


Contents of my file:

1
2
3
4
5
6
7
8
Ram
Toshiba or Dell
Screen
25 or 26
Man
grep or cd
Super
Computer or Human


I want to modify grep or cd to mkdir or rm. Can I insert it in the middle while reading the file in my program?
Your code has a logic issue. gotMatch is false at start. Therefore, condition on line 16 is always false. For that reason line 17-20 are never evaluated, and gotMatch remains false. The logical purpose of gotMatch is probably to show only the first line that contains search_str. If so, mere break; of the while loop would do.

Your current program reads from one stream and writes to an another. Your question seems to be about inplace modification of an existing file. There is a mode for that, but I don't know it. Notice though that s/grep or cd/mkdir or rm/ overwrites 10 characters and inserts 1. That insertion shifts the rest of file content one character forward.

It is usually safer to create new (small, text -- not huge databases) files. If edit is successful, then you can remove original and rename the new.
you can overwrite data in a stream, but the file doesn't automatically resize.

The file must be opend with read and write access. And keep in mind that the stream holds different pointers to the actual position which can be changed using seekg() and seekp()
Thanks a lot, any help in the form of code could be very grateful or atleast some links for to be achieved. If i rewrite the entire file to the new, I dont want my line number atleast to be changed, data can change, in place of `grep or cd`, I get `mkdir or rm`

What is the right way to rewrite my file, with modified line at that particular position? please let me know...
Last edited on
A change of line contents does not change the number of lines (unless you insert additional newlines).

Is your purpose to write code, or to edit files? If the latter:
sed "s/grep or cd/mkdir or rm/" infile > outfile
diff infile outfile

(sed has inplace mode too.)
@coder777 You are right, any link or guidance will be appreciated : )
Last edited on
Topic archived. No new replies allowed.