write 2d array to a file in C (Operating system)

Posted by Bobj-C on Stack Overflow See other posts from Stack Overflow or by Bobj-C
Published on 2011-01-09T09:48:29Z Indexed on 2011/01/09 9:53 UTC
Read the original article Hit count: 166

Filed under:
|
|
|
|

Hello All,

I used to use the code below to Write an 1D array to a File:

FILE *fp;
float floatValue[5] = { 1.1F, 2.2F, 3.3F, 4.4F, 5.5F };
int i;

if((fp=fopen("test", "wb"))==NULL) {
    printf("Cannot open file.\n");
}

if(fwrite(floatValue, sizeof(float), 5, fp) != 5)
    printf("File read error.");
fclose(fp);

/* read the values */
if((fp=fopen("test", "rb"))==NULL) {
    printf("Cannot open file.\n");
}

if(fread(floatValue, sizeof(float), 5, fp) != 5) {
    if(feof(fp))
        printf("Premature end of file.");
    else
        printf("File read error.");
}
fclose(fp);

for(i=0; i<5; i++)
    printf("%f ", floatValue[i]);

My question is if i want to write and read 2D array ??

© Stack Overflow or respective owner

Related posts about c

    Related posts about operating-system