Reading structure from file

This is a discussion on Reading structure from file within the C++ Programming forums, part of the General Programming Boards category; Hello, I'm learning cpp by trying to translate chunks of my VB6 programs. Of course, I have great missunderstanding of ...

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    90

    Reading structure from file

    Hello,
    I'm learning cpp by trying to translate chunks of my VB6 programs. Of course, I have great missunderstanding of cpp syntax and logic.
    Here is example which I cant get to work so please help.

    declarations:
    Code:
    struct n_row
    {
        char m_name[27];
        char m_enable[1];
        char m_pointer[6];
        char m_crlf[2];
    };
    const char *myFile = "names.dat";
    const short lenRecord = 36;
    char *mbuffer[lenRecord];
    int allRecords = 43000;
    long lof;
    int obi;
    int brr;
    int stt;
    functions:
    Code:
    bool eVFrame::reloadlist(bool gput, int stt, long lof, char mbuffer[lenRecord])
    {
        int i;
        FILE *f;
        n_row r;
    
        f=fopen(myFile,"r+b");
        if (!f)
            return true;
            fseek(f, 0, SEEK_END);
            lof = ftell(f);
    
        if (stt<1){stt=1;}
        for (i=stt;i<=stt;i++)
        {
            int sz = sizeof(struct n_row);
            fseek(f,sz*i,SEEK_SET);
         if (!gput)
            {
             fread(&r,sz,1,f);
            }
             else
            {
             fwrite(&r,sz,1,f);
            }
        }
        fclose(f);
        return false;
    }
    
    int eVFrame::myitems(int obi, int brr)
    {
        for (int i = obi; i < obi + nRows; i++)
        {
         eVFrame::reloadlist(false, i, lof, mbuffer[lenRecord]);
         wxString mystring(*mbuffer, wxConvUTF8);
         wxPuts(mystring);
        }
    return 0;
    }
    I call funcion reloadlist from mytems and i cant get data to mbuffer and mystring.
    Problem s that I dont know how to assign data to buffer in reloadlist whish I have in r.
    Can I load direct from file to buffer[]?

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    This depends on how the file was created. If it is a text file this won't work, I think you realize that.

    The other problem is this:
    Code:
    bool eVFrame::reloadlist(bool gput, int stt, long lof, char mbuffer[lenRecord])
    You are passing the value of an array (ie, a copy of the entire array) in, which means no changes to it will affect anything outside the function.

    To do that you should pass in a pointer to the array, so that changes affect that region of memory and not a copy of it.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,820
    Quote Originally Posted by MK27
    You are passing the value of an array (ie, a copy of the entire array) in, which means no changes to it will affect anything outside the function.

    To do that you should pass in a pointer to the array, so that changes affect that region of memory and not a copy of it.
    Actually, that is syntax for a parameter that is a pointer to char. lenRecord is ignored.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Mar 2010
    Posts
    90
    No, this is a binary bile.
    I try to read fixed length record to mbuffer which I would like read in myitems.
    But it is empty.
    I dont know yet manipulate with strings and pointers in cpp.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by nime View Post
    I dont know yet manipulate with strings and pointers in cpp.
    Probably easiest to take a few steps back then and write some short simple demo programs focussed on this, doing the kinds of things you want to do.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Registered User
    Join Date
    Mar 2010
    Posts
    90
    That's exaclly what I do.
    This sample works but I have data in variable r and I dont know how that put in mbuffer so I can read it in function myitems.
    Question is only how to put data (this 36 bytes) from r to mbuffer (in function reloadlist).

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,820
    Quote Originally Posted by nime
    Question is only how to put data (this 36 bytes) from r to mbuffer (in function reloadlist).
    But this sounds like something only you can decide, i.e., the format such that you convert an n_row object to a (presumably null terminated) string.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Mar 2010
    Posts
    90
    How can I decide when I dont know how to do that!
    And second, my strings (in this case) is not null terminated. In file is raw binary data. This is why I read fixed length defined in strucure. To know where is what.
    In VB6 this works great. I belive in cpp also.
    I see data in function reloadlist by reading variable r.
    But I cant get it out (dont know how) in variable mbuffer so I can be able to readi it in function myitems.
    OK, I will try further...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please help me as fast as possible
    By Xbox999 in forum C Programming
    Replies: 5
    Last Post: 11-30-2009, 05:53 PM
  2. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  3. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 04:02 AM
  4. Reading a file into a structure array
    By RazielX in forum C Programming
    Replies: 3
    Last Post: 05-02-2004, 09:30 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM

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