Thread: FILE I/O. data written at wrong location.

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    86

    FILE I/O. data written at wrong location.

    I got a series of functions that are connected too eachother, and that write in a certain data file, but when I look into the data file, the data entered isnt where it is supposed too be! it always writes itself in the back of the file

    this is the code:

    Code:
      std::ofstream WriteIt ("C:\Datacab.dat", ios::app | ios::binary );
      WriteIt.seekp ( ( (Location)* sizeof (member) ), ios::beg );
      std::cout <<"location in bytes: "<<  (Location)* sizeof (member) << std::endl;
      WriteIt.write ( ( reinterpret_cast < char * > (&Wthis) ),sizeof (member) );
    I am 100&#37; sure that somthin is wrong RIGHT THERE since this is what i got as output checking:

    Code:
    Location: LOOP -> 1
    Registered!!
    location in bytes: 0
    wheras Location represents the times looped untill the place is found where the data must written, we do minus 1 (-1) to get the exact location in the file that needs too be overwritten. this is shown by : location in bytes -> 0

    but it just doesnt write there! help?!
    instead it just allways APPENDS on the back of the file, increasing it size too!! which isnt what i want

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > std::ofstream WriteIt ("C:\Datacab.dat"
    Erm, two backslashes perhaps?

    > ios::app
    AFAIK, if you open a file in append mode, then all writes go to the end, no matter where you've previously seeked to.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    86
    how am i supposed to write to certain locations then? ... all i know is ios::app
    *edit* i have changed my program and now it writes at the right location... but i still dont know why
    std::fstream WriteIt ("C:\Datacab.dat", ios::in | ios:ut | ios::binary );
    Last edited by epidemic; 04-05-2007 at 04:28 PM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    int main ( ) {
        ofstream f("foo.bin");
        f.write("hello",5);  // 5 chars at the start 
        f.seekp(5,ios::cur);  // a gap of 5 (will be zero filled)
        f.write("world",5);  // 5 more chars
        return 0;
    }
    
    Result
    $ od -Ax -t x1z foo.bin
    000000 68 65 6c 6c 6f 00 00 00 00 00 77 6f 72 6c 64     >hello.....world<
    00000f
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    86
    can i ask, what the diffrence between .bin files and .dat files are?
    and i also solved my problem this way:

    Code:
    std::fstream WriteIt ("C:\Datacab.dat", ios::in | ios:ut | ios::binary );
    but i can explain why that does work.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    .bin, .dat, .foo, .bar, .xxx - doesn't make a bean of difference to the code.

    The only thing which counts is ios::binary

    Why are you still using a single \ ?

    Just because \D isn't defined just makes you lucky if you're seeing "Datacab.dat" in the file system. Just try "C:\newfile.dat" and enter the land of fun.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  2. Writing and modifying data in a file
    By Micko in forum C Programming
    Replies: 2
    Last Post: 02-17-2005, 03:42 AM
  3. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM