更新时间:2023-12-04 13:23:28
/ *将数据写入文件* /
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