views:

1444

answers:

3

I'm trying to write a 2d array into an output file, it's all working fine except in creating the .getline function to draw the array back out of the file. My issue is putting the string length. My current code for the line is;

inputFile.getline(myArray, [10][10], '\n');

but it doesn't like having the string length in square brackets it seems, what should I do?

thanks in advance

+1  A: 

2d array of what? It would be helpful to see the code that outputs the file.

tfinniga
+2  A: 

For that to compile, myArray must be an array of char, or a char*. In particular, it is a one-dimensional array. To read multiple dimensions, you'll need to read each row separately. The second parameter to istream::getline is the maximum number of chars to read and store in the array, minus one. I like to use the documentation from Dinkumware, but any C++ library documentation should describe this sort of thing.

To begin reading something from a file, you should start by knowing how the file was written to in the first place. You know how the file was created, I hope, but you haven't described that in your question. Knowing how the file was written is essential for knowing how to read from it. Show some code, please.

Rob Kennedy
+1 for dinkumware!
e.James
A: 

With all due respect to Rob Kennedy, he's misrepresenting how C/C++ handle multidimensional arrays.

Consider int foo[10][10] and int foo[100]. In both cases foo is a pointer to a block of memory containing sizeof(int)*100 bytes.

There are subtle differences between int *pointer and int foo[10][10]. Especially with respect to sizeof() and certain compiler-specific error checking. But, when desired, we can treat them interchangeably.


That said, leachrode can't pass argument 2 of getline() as "[10][10]". He can pass "10*10*sizeof(foo)", where foo is myArray's type. He could also pass sizeof(myArray), if it's actually declared as an array.

The variable myArray does NOT necessarily need to be of type char. However, none of the bytes in the myArray data block can have the value '\n' if that getline() invocation is going to work.

Personally, I'd go with binary storage. E.g.: (Assuming inputFile is an iostream object.)

 inputFile.write( (const char *)myArray, sizeof(myArray) );
 inputFile.read( (char *)myArray, sizeof(myArray) );

*HOWEVER*, that does assume that you are reading and writing on binary-compatible systems. We are making assumptions about how your compiler stored that array in memory. Different compilers may do things differently. Padding and alignment issues can create cross-platform nightmares.

It would be better, from a portability & future maintenance perspective, to read/write each array element in/out individually! Codewise it's not much more complicated. Speed wise, iostreams are buffered and you're bound by disk io anyway...

Reference: http://www.cplusplus.com/reference/iostream/

Afterthought: - IF myArray in not an elementary type (int[], double[]), but instead represents an array of objects (with virtual members) or pointers, you'll need a means of serializing. Pointers won't point to valid memory when they are read back in.

Mr.Ree