By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,033 Members | 1,020 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,033 IT Pros & Developers. It's quick & easy.

Understanding fwrite()

P: n/a
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[i][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

Oct 14 '06 #1
Share this Question
Share on Google+
10 Replies


P: n/a
Sheldon wrote:
/* 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

Oct 14 '06 #2

P: n/a

Tom St Denis skrev:
Sheldon wrote:
/* 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
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.

Oct 14 '06 #3

P: n/a
Sheldon wrote:
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

Oct 14 '06 #4

P: n/a

Tom St Denis skrev:
Sheldon wrote:
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
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

Oct 14 '06 #5

P: n/a

Sheldon skrev:
Tom St Denis skrev:
Sheldon wrote:
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

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

Oct 14 '06 #6

P: n/a
"Sheldon" <sh******@gmail.comwrites:
Sheldon skrev:
>Tom St Denis skrev:
Sheldon wrote:
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

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.
So for future googlers, what was the problem?
Oct 14 '06 #7

P: n/a
On Sat, 2006-10-14 at 04:17 -0700, Sheldon wrote:
Tom St Denis skrev:
Sheldon wrote:
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

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.

--
Andrew Poelstra <http://www.wpsoftware.net/projects/>

Oct 14 '06 #8

P: n/a
"Tom St Denis" <to********@gmail.comwrote:
Sheldon wrote:
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.
Or rather, don't, since it is not ISO C, but POSIX.

Richard
Oct 16 '06 #9

P: n/a

Richard Bos skrev:
"Tom St Denis" <to********@gmail.comwrote:
Sheldon wrote:
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.

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

Oct 16 '06 #10

P: n/a
rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
"Tom St Denis" <to********@gmail.comwrote:
[...]
>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.
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.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Oct 16 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.