Writing to a file at some point

Say I have a file with some information in it and after my program runs I want to 'update' this information. Using seekp function I can set the position where the next character is to be inserted. But this overwrites the already existing information after the position I set to. How do I make it so that it adds information and doesn't overwrite.
How are you opening the file ?
umm
 
ofstream dataFile ("Data.txt", ios::out || ios::in)
Couple of ways to accomplish that:

1
2
3
ofstream dataFile ("Data.txt", ios::out | ios::ate | ios::in) 
//  ok
dataFile.seekp (ios_base::end);


Note that mode bits to open are combined with the bitwise or operator, not the logical or operator. Using the logical or operator will result in a value of true.



Last edited on
oops that was a type using the logical or operator. But anyway take a look at this.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <fstream>      
#include <iostream>
int main () {

  std::fstream outfile ("test.txt", std::ios::out);
if(outfile.is_open())
{
    outfile.write ("This is an apple",16);
    long pos = outfile.tellp();
    outfile.seekp (pos-7);
    outfile.write (" sam",4);
}
else
{
    std::cout<<"failed to open";
}
  outfile.close();

  return 0;
}


This will write "This is a sample" to the file. But i want it to write "This is a samn apple". So nothing is overwritten.
I don't think inserting into a file like that is possible. Probably the easiest solution is to build the text in memory, then write it out.
1
2
3
        std::string text = "This is an apple";
        text.insert(9, " sam");
        outfile.write (text.c_str(), text.size());




Thanks chervil.
Yeah i was thinking of something like that. But I'd have to load the whole file into memory and then modify it at places I want, I was just trying to find a more efficient solution.
Say i do something like this to load the file into memory

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
#include <iostream>     
#include <fstream>      
int main () {

  std::ifstream is ("example.txt");
  if (is) {
    // get length of file:
    is.seekg (0, is.end);
    int length = is.tellg();
    is.seekg (0, is.beg);

    char * buffer = new char [length];

    std::cout << "Reading " << length << " characters... ";
    // read data as a block:
    is.read (buffer,length);

    if (is)
      std::cout << "all characters read successfully.";
    else
      std::cout << "error: only " << is.gcount() << " could be read";
    is.close();
    // ...buffer contains the entire file...

    delete[] buffer;
  }
  return 0;
}


How would i add stuff to buffer like i want to. And is there no better ways to do this?
Last edited on
There are always choices, depending on what it is you want to do.
One way to read the file into a std::string (I'm sure there are other, better ways). This assumes that there is no ASCII zero character in the file.

1
2
3
4
    std::ifstream is ("example.txt");
    std::string text;
    char delim = 0;
    std::getline(is, text, delim);
Read the file into a std::string, then you can user erase/insert/replace to manipulate the data as required.
Ok thanks guys.
Topic archived. No new replies allowed.