My code right now:

        std::string line;
        std::fstream localFile;
        localINIFile.open(MyToReadFile/*, std::ios::in || std::ios::out*/); // the MyToReadFile is declared somewhere else, and is being declared before this

        if (localFile.is_open())
        {
            std::string Name = A[ID]; // ID is delcared somewhere else too
            std::string lookFor = Name + " false";
            while ( getline (localFile,line) )
            {
                if (line == lookFor)
                {

                    // this is where I want to edit the current line

                    AddOutput = true; // also declared somewhere else
                    break;
                }

            }
            localFile.close();

        }

My problem is that I can't find anything about being able to change a line like:

// start of file
Line I don't care about
Also a line I don't care about
Line I care about and want to change
One more line I don't care about
and just one more line
// end of file

But I can't find anything about how to do it so my question is how could I possibly do it?

Recommended Answers

All 4 Replies

You have to rewrite the entire file in order to edit one of the lines. It is not possible to just expand a line like you would in memory. If there are lots of edits then first read the entire file into memory (such as into a vector of strings), edit inmemory the strings, then when done rewrite the whole file. Otherwise if the file is too big to read into memory all at once or you only want to edit a single line the algorithm is something like this

open input file
open temp output file
start of loop:
   read a line from input file
   edit the line if necessary
   write the line to output file
   repeat loop
end of loop

close both files
delete the original file
rename temp file with the name of the original file

Remeber a file is just a stream (or an array if you like) of characters. There are no lines that is just how some programs present the data to you, the use a special character or character combination (newline '\n' or carridge return '\r' or a combination of the 2) to tell the program when it should start displaying data on the next line so the file you described
contains the data

Line I don't care about\nAlso a line I don't care about\nLine I care about and want to change\nOne more line I don't care about\nand just one more line

You can read characters from a file and you can write characters from a file but strictly you can't read and write lines. The is a getline function but it doesn't read lines it reads characters until it sees the special character that identifies a place where the data would be shifted to the next line if it were being displayed like that.

That means that if you want to relace a "line" then you have 2 options the first is find the location of the data you want to replace and overwrite it with exactly the same number of characters as the "line" originally had. That is you can not insert data into a file and you can not delete data from the middle of a file intrinsiclly. This obviously only works if the data you want to write is the same length as the data you want to replace.

The other option is to use a temporary file, you read data from the original file and write it to the temporary file until you get to the data you wish to replace. Then you write the new data to the temporary file and skip the data you don't want from the original file. Then copy the rest of the original file to the temporary file. Finally delete the original file and rename the temporary file to have the same name as the original.

A final option is to do the whole thing in memory, load the entire file into memory, manipulate the data in memory to make the changes you require, truncate the file and write the new data to it. This has the disadvantage that you go through a point were the only place your data is is in memory so if the computer/program crashes at this point you have lost all your data.

so I could take all the lines and paste it into an array/table,
then do a loop through the array/table and then check each var if it equals what I want then change that var to whatever new I want, then do the IO stuff?
because that seems like something simple to do. Anyways thanks for the help.

Yes, you can do it that way. And yes, it's pretty simple to do.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.