CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jun 2002
    Posts
    3

    Writing/reading struct arrays

    I've got a simple struct (myStruct), with a few char arrays, and I'm trying to write it an array of type myStruct to a file, in order to later be able to retrieve it from that file, back into an array of type myStruct. What would be the best way to go about this? I've experimented a bit with ifstream/ofstream, and a bit with CreateFile, WriteFile, ReadFile, etc.. in winbase.h, and I can't get it to work properly...Any help would be greatly appreciated.

  2. #2
    Join Date
    May 2000
    Location
    Washington DC, USA
    Posts
    715
    #include <stdio.h>
    #include <memory.h>

    typedef struct MYSTRUCT
    {
    char szArray1[10];
    char szArray2[10];
    } MYSTRUCT, *PMYSTRUCT;

    void main( void )
    {
    FILE *stream;
    int numread, numwritten;

    MYSTRUCT myStruct[10];



    for ( int z = 0; z < 10; z++ )
    {
    memset ( (void *)(&myStruct[z]), z, 20);
    }

    /* Open file in text mode: */
    if( (stream = fopen( "fread.out", "w+t" )) != NULL )
    {
    numwritten = fwrite( &myStruct, sizeof( MYSTRUCT ), 10, stream );
    printf( "Wrote %d items\n", numwritten );
    fclose( stream );

    }
    else
    printf( "Problem opening the file\n" );


    memset ( (void *)&myStruct, '\0', sizeof(MYSTRUCT)*10);


    if( (stream = fopen( "fread.out", "r+t" )) != NULL )
    {
    /* Attempt to read in 25 characters */
    numread = fread( &myStruct, sizeof( MYSTRUCT ), 10, stream );
    printf( "Number of items read = %d\n", numread );
    fclose( stream );
    }
    else
    printf( "File could not be opened\n" );
    }

  3. #3
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Writing/reading struct arrays

    Originally posted by Zorander
    I've got a simple struct (myStruct), with a few char arrays, and I'm trying to write it an array of type myStruct to a file, in order to later be able to retrieve it from that file, back into an array of type myStruct. What would be the best way to go about this? I've experimented a bit with ifstream/ofstream, and a bit with CreateFile, WriteFile, ReadFile, etc.. in winbase.h, and I can't get it to work properly...Any help would be greatly appreciated.
    Code:
    #include <iostream>
    #include <fstream>
    struct Test
    {
        char a[10];
        char b[10];
    
    };
    
    std::ostream& operator << (std::ostream& str, const Test& x)
        {
            str << x.a << '\n' << x.b;
            return str;
        }
    
    std::istream& operator >> (std::istream& str, Test& x)
        {
            str >> x.a >> x.b;
            return str;
        }
    
    // Test
    using namespace std;
    
    int main()
    {
        Test A;
        strcpy(A.a,"Test1");
        strcpy(A.b,"Test2");
    
        // Test to cout / cin
        cout << A << endl;
        cout << "Input ";
        cin >> A;
        cout << A << endl;
    
        // Test to file
        ofstream f("c:/test");
        f << A;
        f.close();
        ifstream f2("c:/test");
        f2 >> A;
    }
    Regards,

    Paul McKenzie

  4. #4
    Join Date
    Jun 2002
    Posts
    3
    Thanx a lot, both of you. This really helps. Unfortunately, it didn't quite work. I'm not exactly sure why. I used Paul's suggestion, and it worked if I changed the include statements from <iostream> and <fstream> to <iostream.h> and <fstream.h>, except that every reference to "std" came up with " 'std' : is not a class or namespace name ". When I used <iostream> and <fstream>, it came up with 46 errors, a lot having to do with the fact that it completely didn't recognize simple streams like 'cout' and 'cin' (" 'cout' : undeclared identifier ", etc.) I'm using MS Developer Studio 97, with Visual C++ 5.0... Anyone have any ideas on how to get this to work?
    Last edited by Zorander; June 24th, 2002 at 02:06 PM.

  5. #5
    Join Date
    Jun 2002
    Posts
    3
    Nevermind, I figured it out. It works perfectly. Thanks a lot!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured