已知文件指针file *,如何把文件内容输出到屏幕上,求帮助!
发表于 : 2013-04-06 20:55
大家好,我现在只知道一个文件的文件指针,比如file*,如何把该文件内容输出到屏幕上,应该调用哪个函数啊?求帮助!!!
Fermat618 写了:请拿出一本C语言入门书,翻到文件那一章。
大概就是
[c]
while ((ch = fgetc(file)) != EOF) {
putchar(ch);
}
[/c]
zero_hsy 写了:我知道以字符串的方式输出,但是我在文件里放的是int型的一堆数据,都是计算出来的数字,如何输出到屏幕上去啊!!!试过楼上说的getc,结果出来:核心栈已转移,没有输出任何东西!!
代码: 全选
/* fread example: read an entire file */
#include <stdio.h>
#include <stdlib.h>
int main () {
FILE * pFile;
long lSize;
char * buffer;
size_t result;
pFile = fopen ( "myfile.bin" , "rb" );
if (pFile==NULL) {fputs ("File error",stderr); exit (1);}
// obtain file size:
fseek (pFile , 0 , SEEK_END);
lSize = ftell (pFile);
rewind (pFile);
// allocate memory to contain the whole file:
buffer = (char*) malloc (sizeof(char)*lSize);
if (buffer == NULL) {fputs ("Memory error",stderr); exit (2);}
// copy the file into the buffer:
result = fread (buffer,1,lSize,pFile);
if (result != lSize) {fputs ("Reading error",stderr); exit (3);}
/* the whole file is now loaded in the memory buffer. */
// terminate
fclose (pFile);
free (buffer);
return 0;
}