了解fwrite() [英] Understanding fwrite()
问题描述
我正在尝试从头开始学习C,虽然我知道如何在Python中使用
程序,但C中的很多东西都是阅读这些例子后,很难理解 - 甚至
。我猜是因为存在很多变化。
有人可以解释为什么fwrite的这种变化失败了:
#include< stdio.h>
#include< stdlib.h>
#define ROW 15 / *为了拥有更多内存而且没有分段
故障,请求更多内存* /
#define COL 15
#define FILENAME" /data/aux/test_array.dat"
double tmp [ROW ] [COL];
int main(无效)
{
FILE * fp; / *声明一个文件指针* /
int i,j,data;
浮点数;
printf(" Rows - %d \tCol - %d \ n",ROW,COL);
/ *将数据分配给数组* /
count = 2.0;
for(i = 0; i< ROW; i ++)
{
for(j = 0; j< COL; j ++)
{
tmp [i] [j] = count;
count ++;
/ * printf("%f \\ \
",计数); * /
}
}
printf(完成写入数组\ n);
printf(打开文件:%s \ n,FILENAME);
if((fp = fopen(FILENAME," wb"))== NULL)
{
fprintf(stderr,在阅读模式下打开文件%s时出错。\ n,
FILENAME);
fclose(fp);
返回EXIT_FAILURE;
}
else
printf("文件已成功打开。 \ n \\ nn;);
/ *将数据写入文件* /
if(ROW * COL!= fwrite(tmp,sizeof * tmp,COL, fp))/ *检查字节
文件大小* /
{
fprintf(stderr,"写入文件时出错。 \ n");
fclose(fp);
返回EXIT_FAILURE;
}
fclose(fp );
/ *写完文件* /
返回0;
}
提前致谢!
谢尔顿
/ *将数据写入文件* /
if(ROW * COL!= fwrite(tmp,sizeof *) tmp,COL,fp))/ *用fwrite(a,b,c)检查字节
文件大小* /
,d)如果写完所有数据,则返回c。不是
b * c。
顺便说一句,Perl和Python之类的许多函数都来自
标准C.
Tom
Tom St Denis skrev:
Sheldon写道:
/ *将数据写入文件* /
if(ROW * COL! = fwrite(tmp,sizeof * tmp,COL,fp))/ *检查字节
文件大小* /
with fwrite(a,b,c,d)如果写入了所有数据,则返回c。不是
b * c。
顺便说一句,Perl和Python之类的许多函数都来自
标准C.
Tom
我同意有很多相似之处,但我告诉你写()
python并不像老生常谈。我尝试了以下变体:
if(ROW * COL!= fwrite(tmp,sizeof * tmp,ROW * COL,fp))
但是这会导致分段错误。
Sheldon写道:
我同意有很多相似之处,但我告诉你写()在
python并不是一个问题。我尝试了以下变体:
if(ROW * COL!= fwrite(tmp,sizeof * tmp,ROW * COL,fp))
但是这会导致分段错误。
您是否考虑过阅读fwrite的联机帮助页?
BTW glibc也为C写了write() 。如果你更熟悉
写()使用它。
Tom
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
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
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.
TomI 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.
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
这篇关于了解fwrite()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!