写了一个程序,可以格式化的从文件中读出自己想要的字段,然后分别显示在屏幕上,虽然方法可能傻了点,不过还算好用,后来在测试的时候,突然发现了一个问题,不知道为什么,当文件读到最后时,并没有结束,而是将最后一行字串又打了一遍,我试过将fgets换为fwrite,虽然不会再多打出一行,但是因为文件的长度不确定,结果输出的都是乱码,看了看书上的例子,好象只有定义结构体之后格式化输出才能正确读取,难道没有别的办法了么....
__________________________________________________________________
运行环境:
FC6/gcc4.0.1
程序如下:
#include
#include
void file_format_read(void)
{
FILE *filep;
char read_log[100]="\0";
char select_file_name[20]="\0";
char select_file_date[20]="\0";
char select_file_length[20]="\0";
char select_file_size[20]="\0";
filep=fopen("file_format.txt","r");
if(!filep)
printf("open file error!\n");
printf("open file ok!\n");
while(!feof(filep))
{
fgets(read_log,100,filep);
strncpy(select_file_name,read_log,16);
strncpy(select_file_date,read_log+17,16);
strncpy(select_file_length,read_log+34,strcspn(read_log,"#")-strcspn(read_log,"@")-1);
strncpy(select_file_size,strchr(read_log,'#')+1,strcspn(read_log,"$")-strcspn(read_log,"#")-1);
printf("fread = %s\n",read_log);
printf("select_file_name = %s\n",select_file_name);
printf("select_file_date = %s\n",select_file_date);
printf("select_file_length = %s\n",select_file_length);
printf("select_file_size = %s\n",select_file_size);
printf("**********************************\n");
strcpy(select_file_name,"\0");
strcpy(select_file_date,"\0");
strcpy(select_file_length,"\0");
strcpy(select_file_size,"\0");
}
fclose(filep);
}
int main()
{
file_format_read();
return 1;
}
用来读的文件:
名称:file_format.txt
内容:
200706201421.avi 2007-06-20 14:21@58分钟#45M%
200706201422.avi 2007-06-20 14:22@158分钟#45M%
200706201423.avi 2007-06-20 14:23@58分钟#145M%
200706201424.avi 2007-06-20 14:24@58分钟#45M%
200706201425.avi 2007-06-20 14:25@258分钟#245M%
200706201426.avi 2007-06-20 14:26@58分钟#45M%
200706201427.avi 2007-06-20 14:27@58分钟#45M%
200706201428.avi 2007-06-20 14:28@2258分钟#2145M%
200706201429.avi 2007-06-20 14:29@58分钟#45M%
[本帖最后由 封神 于 2007-7-3 09:10 编辑]