在做cgi编程的时候,遇到用fread()函数读取数据。于是查阅了Linux c函数库。遇到些问题,一并总结。
1.fread()
首先,fread()原型:
/*fread():read datas from file-stream.--------------------------------------------------------
size_t fread(void *ptr, size_t size, sizt_t nmenb, FILE *stream);
stream:the open-file pointer.
ptr:point to the space that want to store the read-date.
size:the type size,like int, char or struct.
nmemb*size:the number char that read.
Note:fread() returns the actual number that read,nmemb.While IF the actual number is smaller than the parameter 'nmemb', maybe it means it reads to the file-end or error happens.In this case, we must use feof() or ferror() to see what happened.
-----------------------------------------fread()----------------------------------------------
关于fread()函数例子:
- #include <stdio.h>
- #include <stdlib.h>
- #define nmem 3
- struct test
- {
- char name[20];
- int size;
- }s[nmem];
- int main()
- {
- FILE *stream;
- stream = fopen("/test.c", "r");
- fread(s, sizeof(struct test), nmem, stream);
- fclose(stream);
- for(int i=0; i<nmem; i++)
- printf("name[%d]=%-20s: size[%d]=%d\n", i, s[i].name, i, s[i].size);
- return 0;
- }
在VC6.0中编译,产生错误:
D:\Visual C++ 6.0 Enterprise Setup\MSDev98\MyProjects\fread.c(18) : error C2143: syntax error : missing ';' before 'type'
D:\Visual C++ 6.0 Enterprise Setup\MSDev98\MyProjects\fread.c(18) : error C2143: syntax error : missing ';' before 'type'
D:\Visual C++ 6.0 Enterprise Setup\MSDev98\MyProjects\fread.c(18) : error C2143: syntax error : missing ')' before 'type'
D:\Visual C++ 6.0 Enterprise Setup\MSDev98\MyProjects\fread.c(18) : error C2143: syntax error : missing ';' before 'type'
D:\Visual C++ 6.0 Enterprise Setup\MSDev98\MyProjects\fread.c(18) : error C2065: 'i' : undeclared identifier
D:\Visual C++ 6.0 Enterprise Setup\MSDev98\MyProjects\fread.c(18) : warning C4552: '<' : operator has no effect; expected operator with side-effect
D:\Visual C++ 6.0 Enterprise Setup\MSDev98\MyProjects\fread.c(18) : error C2059: syntax error : ')'
D:\Visual C++ 6.0 Enterprise Setup\MSDev98\MyProjects\fread.c(19) : error C2146: syntax error : missing ';' before identifier 'printf'
Error executing cl.exe.
fread.obj - 7 error(s), 1 warning(s)
但从句法分析,虽说是语法错误,但貌似没有啊 :(
后来在Linux下编译,擦擦,错误信息很明了啊,如下:
fread.c: In function ‘main’:
fread.c:17: error: ‘for’ loop initial declaration used outside C99 mode
原来问题出在C99语法与C89语法的问题。顺便在网上搜
了下,看这里。
所以,将i在main函数开始定义即可。也可以这样:gcc -std=c99 fread.c -o fread,使支持c99标准。
貌似这种定义以前一直用,今天换了下产生这个问题。还是编译器的原因吧。
附:
左对齐:"-" "%-20s"
右对齐:"+" "%+20s"
顺便搜集一下常用到的输入输出格式,在另外一篇日记里(网上检索的)。
2.fwrite():
size_t fwrite(const void *ptr, size_t size, size_tnmemb, FILE *stream);
同read类似,只不过是“写”。
例子:
- #include
- #incldue
- #define set_s (x, y) {strcpy(s[x].name, y); s[x].size = strlen(y);}
- struct test
- {
- char name[20];
- int size;
- }s[nmem];
- main()
- {
- FILE *stream;
- set_s(0, "Linux");
- set_s(1, "Windows");
- set_s(2, "Macintosh");
- stream = fopen("./test.c", "w");
- fwrite(s, sizeof(struct test), nmem, stream);
- fclose(stream);
- }
阅读(1057) | 评论(2) | 转发(0) |