Click to See Complete Forum and Search --> : Writing/reading struct arrays


Zorander
June 24th, 2002, 12:56 AM
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.

JMS
June 24th, 2002, 02:35 AM
#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" );
}

Paul McKenzie
June 24th, 2002, 03:47 AM
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.

#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

Zorander
June 24th, 2002, 01:29 PM
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?

Zorander
June 24th, 2002, 02:36 PM
Nevermind, I figured it out. It works perfectly. Thanks a lot! :D