我试图从文本文件中提取一行的部分,并在UNIX平台下的C编程中将其输出到一个新文件中.例如,我有file1.txt如下:
设备:ABCDE1
日期:12/12/12
运营商= John Doe
我希望它在file2.txt中显示为:
ABCDE1,12/12/12,John Doe.
这是我到目前为止的代码,我得到了开头并写入文件部分,但是无法弄清楚如何在"Device:"之后提取信息,而不显示"Divice:"它自己.我的代码只复制file1的第一行,并将其放在文件2中:
#include "stdio.h"
main()
{
FILE *fs, *ft;
char ch ;
char file[100]="" ;
char Device [100];
char Date [80];
printf ("Enter file name and directory:");
scanf ("%s",&file);
fs = fopen (file, "r") ;
if ( fs == NULL )
{
puts ( "Cannot open source file" ) ;
exit( ) ;
}
ft = fopen ( "file2.txt", "w" ) ;
if ( ft == NULL )
{
puts ( "Cannot open target file" ) ;
fclose ( fs ) ;
exit( ) ;
}
fscanf(fs, "%s %s", Device,Date);
fprintf(ft, "%s %s\n", Device,Date);
fclose ( fs ) ;
fclose ( ft ) ;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
fscanf()是一个很好的命令吗?或者是否有任何其他建议的命令来使这项工作?提前致谢.