Understanding fwrite()

S

Sheldon

Hi,

I am trying to learn C from scratch and, though I do know how to
program in Python, many things in C are hard to understand - even
after reading the examples. I guess because so many variations exists.
Can someone explain why this variation of fwrite fails:

#include <stdio.h>
#include <stdlib.h>
#define ROW 15 /* In order to have more memory and no segmentation
faults, request more memory */
#define COL 15
#define FILENAME "/data/aux/test_array.dat"

double tmp[ROW][COL];
int main(void)
{
FILE *fp; /* declare a file pointer */
int i, j, data;
float count;
printf("Rows -> %d\tCol -> %d\n",ROW, COL);
/* Assigning data to the array */
count = 2.0;
for(i = 0; i < ROW; i++)
{
for(j = 0; j < COL; j++)
{
tmp[j] = count;
count++;
/* printf("%f\n",count); */
}
}
printf("Finished writing to array\n");
printf("Opening file: %s\n",FILENAME);
if ((fp = fopen(FILENAME, "wb")) == NULL)
{
fprintf(stderr, "Error opening file %s in read mode.\n",
FILENAME);
fclose(fp);
return EXIT_FAILURE;
}
else
printf("File opened successfully.\n\n");
/* Write data to file */
if(ROW*COL != fwrite(tmp, sizeof *tmp, COL, fp)) /* Checking the byte
size of the file */
{
fprintf(stderr, "Error writing to file.\n");
fclose(fp);
return EXIT_FAILURE;
}
fclose(fp);
/* done writing to file */
return 0;
}


Thanks in advance!
Sheldon
 
T

Tom St Denis

Sheldon said:
/* Write data to file */
if(ROW*COL != fwrite(tmp, sizeof *tmp, COL, fp)) /* Checking the byte
size of the file */

with fwrite(a, b, c, d) it returns c if it wrote all the data. Not
b*c.

btw, many functions in things like Perl and Python are borrowed from
standard C.

Tom
 
S

Sheldon

Tom St Denis skrev:
with fwrite(a, b, c, d) it returns c if it wrote all the data. Not
b*c.

btw, many functions in things like Perl and Python are borrowed from
standard C.

Tom

I agree that there are many similarities but I tell you write() in
python is not as criptic. I have tried that variation of:
if(ROW*COL != fwrite(tmp, sizeof *tmp, ROW*COL, fp))

But this causes a segmentation fault.
 
T

Tom St Denis

Sheldon said:
I agree that there are many similarities but I tell you write() in
python is not as criptic. I have tried that variation of:
if(ROW*COL != fwrite(tmp, sizeof *tmp, ROW*COL, fp))

But this causes a segmentation fault.

Have you given any thought to reading the manpage for fwrite?

BTW glibc has write() for C as well. If you are more familiar with
write() use that.

Tom
 
S

Sheldon

Tom St Denis skrev:
Have you given any thought to reading the manpage for fwrite?

BTW glibc has write() for C as well. If you are more familiar with
write() use that.

Tom

Thanks for the tip on the man. I have looked up the manual on fwrite()
but still there is an error. I simply want to understand fwrite().

Is there a way I can use the core dump file to understand what I am
doing wrong?

/Sheldon
 
S

Sheldon

Sheldon skrev:
Tom St Denis skrev:


Thanks for the tip on the man. I have looked up the manual on fwrite()
but still there is an error. I simply want to understand fwrite().

Is there a way I can use the core dump file to understand what I am
doing wrong?

/Sheldon

Thanks Tom, it works now. Just me not paying attention.

/Sheldon
 
A

Andrew Poelstra

Tom St Denis skrev:


Thanks for the tip on the man. I have looked up the manual on fwrite()
but still there is an error. I simply want to understand fwrite().

Is there a way I can use the core dump file to understand what I am
doing wrong?

There are tools for reading the core dump file, although a text editor
will do fine in a pinch.

I lost your original post, but the likely cause is that tmp does not
point to ROW*COL objects, but rather to a number substantially less that
that. So, fwrite() keeps on reading past the end of tmp[], which causes
the segfault.

Try:
if(COL != fwrite(tmp, sizeof *tmp, COL, fp))
To fix your original problem.
 
R

Richard Bos

Tom St Denis said:
Have you given any thought to reading the manpage for fwrite?

BTW glibc has write() for C as well. If you are more familiar with
write() use that.

Or rather, don't, since it is not ISO C, but POSIX.

Richard
 
S

Sheldon

Richard Bos skrev:
Or rather, don't, since it is not ISO C, but POSIX.

Richard


Thanks fellows,

I had more than one fwrite and didn't see that. I now understand how
fwrite works and I appreciate everyone's advice.

Much obliged,
Sheldon
 
K

Keith Thompson

Or rather, don't, since it is not ISO C, but POSIX.

Or rather, use write() if you have a good reason to do so, but don't
expect any advice about it in this newsgroup. (For general-purpose
code, there's rarely a reason to use write() rather than fwrite(), and
fwrite() is more portable.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top