size_t read_size;
size_t reality_size;
if(reality_size!= (read_size = fwrite(model, sizeof(T), read_size, out_fp))){
}
http://blog.csdn.net/liangxanhai/article/details/8026496
fscanf函数详解:
fscanf()函数(有点像正则表达式):
功 能: 从一个流中执行格式化输入,fscanf遇到空格和换行时结束,注意空格时也结束。
用 法:int fscanf(FILE *stream, char *format,[argument...]);
file.txt 内容为:
10 10
16 2
以下为读取内部文件内容方法:
char *FilePath = "test/file.txt";
char Table[1024];
sprintf(Table, "%s", FilePath);
FILE* file = fopen(Table, "r");
if (!fscanf(file, "%d %d", &num_bin, &colorspace)) {
printf("Error in Reading txt: %s %d %s
", __FILE__, __LINE__, Table);
}
又如:
首先我有一个data.txt的文件里面的数据格式如下:
2,50,41,w,20.585828
4,52,51,r,52.012547
.........................
许多条类似的记录,都是以","来分隔的
我实现的功能就是把上面文件中的数据的五个字段赋值给相应的五个变量,并且输出这些变量的值
#include<stdio.h>
#include<stdlib.h>
int main()
{
int fd;
long dev;
long offset;
long length;
char ch;
double ts=0.000000;
if((fd=fopen("/home/haixian/ceshi/data.txt","r"))<0)
{
printf("open the file is error!
");
return -1;
}
fseek(fd,0,SEEK_SET);
while(5==fscanf(fd,"%ld,%ld,%ld,%c,%lf
",&dev,&offset,&length,&ch,&ts))
{ //在这里就是第二个参数指定分隔参数的格式,在这里使用的是,来分隔。
//这样就很容易的获取了记录的各个字段的值并不需要自己编写函数来进行解析什么的。
printf("%ld,%ld,%ld,%c,%lf
",dev,offset,length,ch,ts);
return -1;
}
close(fd);
return 0;
}