don't click here

Need some C++ help.

Discussion in 'Technical Discussion' started by .hack//zero, Oct 2, 2008.

  1. .hack//zero

    .hack//zero

    Member
    3,011
    4
    18
    Working on a vulkan game engine for fun. Learning Ray-Tracing.
    When I try to the program to read all the records, it'll keep showing the same record.
    Can you see anything in AddRecord or ReadRecord that would cause this problem?

    Code (Text):
    1. void AddRecord(Records rec,int recordcounter)
    2. {
    3.     fstream outfile("persons.txt", ios::in | ios::out | ios::binary | ios::trunc);
    4.  
    5.     outfile.seekp(0,ios::end); //To get the position for the program to write to.
    6.     rec.getData();
    7.     outfile.write(reinterpret_cast<char*>(&rec),sizeof(rec));
    8. }
    9. void ReadRecord(Records rec, int recordcounter)
    10. {
    11.     fstream outfile("persons.txt", ios::in | ios::out | ios::binary | ios::trunc);
    12.     for(int a = 1; a <= recordcounter; a++)
    13.     {
    14.     outfile.seekp((recordcounter - 1) * sizeof(rec));
    15.     rec.GiveData();
    16.     outfile.read(reinterpret_cast<char*>(&rec), sizeof(rec));
    17.     }
    18. }
     
  2. Ultima

    Ultima

    Games Publisher Tech Member
    2,398
    1
    18
    London, England
    Publishing mobile games!
    Shouldn't the sizeof(rec) be the changed to give the size of a single record, rather than the size of the entire Records object? sizeof(Records) or sizeof(rec) will give the size of the entire Records set, instead of just one record.

    If this isn't the case, and the Records object really represents a single record, you're only ever writing one record with the AddRecord function, since you always write to position 0.
     
  3. .hack//zero

    .hack//zero

    Member
    3,011
    4
    18
    Working on a vulkan game engine for fun. Learning Ray-Tracing.
    Actually, it's just for debuggin purposes.


    Anyway, here's a updated version of it.
    Code (Text):
    1. void ReadRecord(Records rec, int recordcounter, fstream& outfile)
    2. {
    3.     int block;
    4.     cin >> block;
    5.     outfile.seekg(block*sizeof(rec));
    6.     outfile.read(reinterpret_cast<char*>(&rec), sizeof(rec));
    7.     rec.GiveData();
    8. }
    9.  
    10. void AddRecord(Records rec,int recordcounter,fstream& outfile)
    11. {
    12.     rec.getData();
    13.     outfile.write(reinterpret_cast<char*>(&rec), sizeof(rec));
    14.     outfile.seekg(0);
    15.     outfile.read(reinterpret_cast<char*>(&rec), sizeof(rec));
    16.     rec.GiveData();
    17.  
    18. }
     
  4. .hack//zero

    .hack//zero

    Member
    3,011
    4
    18
    Working on a vulkan game engine for fun. Learning Ray-Tracing.
    Never mind it's working now. Apprently the problem had to do with me reopening the file too much.
     
  5. Pablo

    Pablo

    Member
    Ehhh.... Nasty looking code IMHO :(

    I'm a little late to help you now matey, but being a smartass I'd like to make some recommendations with good intention:

    a) You shouldn't use fstream: Use ifstream and ofstream, it's less confusion, less flags to set and...

    b) You read an outfile? Ughhh... I normally read infiles :eng101: And of course I write to outfiles :P Naming conventions! Again... Less confusion

    c) You truncate the file upon opening and then seek to it's end? What you are seeing is actually probably unreferenced/uninitialized memory!

    I.e.:

    Code (C++):
    1. #include <iostream>
    2. int main (int argc, char * const argv[]) {
    3.     int* array = new int [3];
    4.     for(int I=0; I<3; I++)array[I]=I;
    5.     delete [] array;
    6.     int *array2 = new int [3];
    7.     for(int I=0; I<3; I++){
    8.         array2[I]=I+10;
    9.         std::cout << array[I];
    10.     }
    11.     return 0;
    12. }
    What's the output on my machine?

    [Session started at 2008-12-24 12:46:39 +0100.]
    101112
    The Debugger has exited with status 0.


    So it really isn't you trying to open the file too many times!


    Now get back to your putter and learn c++ the right way :(


    Cheers,

    Pablo


    P.S.: If I'm wrong hit me, slap me, drag me around in the dirt, ... Do what ever you feel like ^^