How to read

Still a beginner here.
I have a txt file with all the values (Values are not fixed)

1
2
3
4
5
//Information inside values.txt
1234
12345
123
12345678


How to for example overwrite the first sentence (1234) to 1234567 so that the end result will be

1
2
3
4
5
//Information inside values.txt
1234567
12345
123
12345678


You need to rewrite the file. You can't insert into a sequential stream without changing the whole structure.
Last edited on
That example isn't quite right. You shouldn't call open/close on a stream. You should specify the file name in the constructor and not call close.
kbw wrote:
You shouldn't call open/close on a stream.


whatabout if you want to deal with multiple files with a stream object?

Aceix.
Create an object for each stream.

Have you considered RAII?
what is its advantage over opening and closing? and also i think opening and closing helps sace memory?

I read about RAII, which is actually a very good practice. I was worried about memory consumption(even though it is small amounts).
I also have one question. If one uses new with RAII, is it still necessary to call delete?

Aceix.
Last edited on
There are many arguments why that is less than ideal.

When you use an fstream, you shouldn't thinking file. You ought to be thinking stream. You don't open/close streams. You just read/write them.

Like I said before, RAII. If you have an fstream, it should be in a usable and functional state from the start. There are a number of interlocking concepts that support robust design, such as Adam's exception guarantees. Being careless about state doesn't help.

I think it's just plain wrong to have a beginner's tutorial on fstreams that calls open/close on them. You'll be checking for EOF next It's not called the I/O stream library for nothing.
> whatabout if you want to deal with multiple files with a stream object?

C++ streams are moveable.
If the standard library implementation is conforming (for example Microsoft), we can do this:
1
2
3
4
5
6
7
8
std::ifstream file( "some_file" ) ;
std::cout << file.rdbuf() << '\n' ;

file = std::ifstream ( "another_file" ) ; // move
std::cout << file.rdbuf() << '\n' ;
        
std::swap( file, std::ifstream ( "a third file" ) ) ; // swap => move
std::cout << file.rdbuf() << '\n' ;



To modify a text file containing white space delimited values:
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
#include <fstream>
#include <vector>
#include <cstdio>

int main()
{
    const char* const file_name = "values.txt" ;
    const char* const backup_file_name = "values.txt.bak" ;

    // read the values into a vector
    std::vector<int> values ;
    {
       std::ifstream file(file_name) ; // open for input
       int value ;
       while( file >> value ) values.push_back(value) ;
    }

    // modify the values as needed
    values.push_back(234) ;
    values[0] = 1234567 ;
    // etc.

    // just to be safe, rename the old file to create a backup
    std::rename( file_name, backup_file_name ) ;

    // create the file afresh and write the modified values, one value per line
    {
       std::ofstream file(file_name) ; // create, and open for output
       for( int v : values ) file << v << '\n' ;
    }

    // note: in this snippet, error checking has been elided for brevity
    // if we have reached here with no errors, the backup may be deleted.
    // std::remove( backup_file_name ) ;
}


@kbw
editted before refreshing...

Aceix.
kbw wrote:
That example isn't quite right. You shouldn't call open/close on a stream. You should specify the file name in the constructor and not call close.

Good point; the maintainer of the tutorials should be notified about it.
Last edited on
Topic archived. No new replies allowed.