What am I doing wrong in this function? (writing to a file)

This is a discussion on What am I doing wrong in this function? (writing to a file) within the C++ Programming forums, part of the General Programming Boards category; This function creates 20 lines in this format: Code: -2 000000000 * * -1 -2 000000000 * * -1 then ...

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    14

    What am I doing wrong in this function? (writing to a file)

    This function creates 20 lines in this format:

    Code:
    -2 000000000 *          *                    -1 
    -2 000000000 *          *                    -1
    then I attempt to overwrite one of the lines with a new record and this is what I get:

    Code:
    -2 000000000 *          *                    -1 
    -2 000000000 *          *                    -1 
    -2 000000000 *          *                    -1 
    -2 000000000 *          *                    -1 
    0 111222333 alfonsines Delarumurationvuzure 50000 
     000000000 *          *                    -1 
    -2 000000000 *          *                    -1
    Now that file is meant to be parsed. the first number tells me whether it can be deleted, follow SSN, name, etc and the last number is a the byte offset in the file of then next relevant record. All records are supposed to take 50 bytes exactly but when I overwrite one, everything goes to hell.

    I'm clearly doing something very wrong. Any help would be most appreciated.

    Thanks.






    Code:
    int i=0;
    int j = 0;
    fstream hashstream;
    long int Pointer (-1);
    ofstream hashwrite;
    hashwrite.open("hashprimary.txt");
    for (j=0; j<=20; j++)//writes the 20 lines0
    {
    	hashwrite<<-2<<' '<<"000000000"<<' '<<"*";
    	for(i=0; i<10; i++)
    		hashwrite<<' ';
    	hashwrite<<"*";
    	for(i=0; i<20; i++)
    		hashwrite<<' ';
    	hashwrite<<Pointer<<' '<<"\n";
    }
    hashwrite.close();
    
    
    hashstream.open("hashprimary.txt", std::ios::in | std::ios::out);//Writes one line at offset 200 bytes
    hashstream.seekp(200, ios::beg);
    hashstream<<0<<' '<<"111222333"<<' '<<"alfonsines";
    for(i=10; i<=10; i++)
    	hashstream<<' ';
    hashstream<<"Delarumurationvuzure";
    	for(i=20; i<=20; i++)
    		hashstream<<' ';
    Pointer = 50000;
    hashstream<<Pointer<<' '<<endl;
    hashstream.close();

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    50 bytes including or excluding newlines? Is this on a Unix/Linux or Windows machine? If it's on a Windows system, remember that the actual line is ending with "CR/LF", so two bytes.

    --
    Mats

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Try using the "tellp" to see where the position in the file is after you've written one or more records...

    --
    Mats

  4. #4
    Registered User
    Join Date
    Jan 2007
    Posts
    14
    Quote Originally Posted by matsp View Post
    Try using the "tellp" to see where the position in the file is after you've written one or more records...

    --
    Mats
    Yeah, I was made aware of that. It is 50 bytes including newline. When i add ios::binary , then tellp says 49 bytes but I had already figured as much from trial and error. So I tried with ||ios::binary with a 196 bytes offset to no avail.

    I am using windows. VS2k5.

    This is driving me crazy. I've been stuck on this for hours now.
    Last edited by gozu; 08-07-2007 at 07:03 PM.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    But your integers aren't printed fixed width, so one record may be longer than another (in fact, they are!), your filled in record is 2 bytes longer than the other ones.

    You need to use the "width()" iomanipulator to set the correct width for your number fields. Add "right" or "left" to make sure your columns are in the right place. (You may want to add a check as well to MAKE SURE that the number fits in the related with, e.g. check that if you set a 5 digit field, that the number is between -9999 and 99999). The last thing you want is to overwrite some part of the database with some rubbish from a previous field.

    --
    Mats

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 09:28 AM
  2. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 07:54 PM
  3. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 08:54 AM
  4. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21