假设我想在阅读和数字4在C时的读数混合字符和文字数字从文件
5000 49 3.14 Z 100
0322 35 9.21 X 60
乘1号目前我有,但我只能够复制信息不操纵信息
#include <stdio.h>
#include <stdlib.h>
#define FILE_1 "File1.txt"
#define FILE_2 "File2.txt"
int main (void)
{
// Local Declarations
char score;
int curCh;
int count = 0;
FILE* sp1;
FILE* sp2;
if (!(sp1 = fopen (FILE_1, "r"))) //check if file is there
{
printf ("\nError opening %s.\n", FILE_1);
return (1);
} // if open error
if (!(sp2 = fopen (FILE_2, "w")))
{
printf ("\nError opening %s.\n", FILE_2);
return (2);
} // if open error
while((curCh = fgetc(sp1)) != EOF)
{
printf ("%c", curCh); //copy the contents
count++;
} // while
return 0;
}
您标记fcsnf,但不要在您的代码中使用它。也就是说,你可能会发现fgets()和sscanf()提供了一种更简洁的方式来解析这种类型的数据。 – 2013-03-15 01:26:15
@RandyHoward也许,如果该文件是标准输入。当文件是另一个文件时,fscanf通常不是问题,因为输入不是很不规则。 – Sebivor 2013-03-15 02:10:37
你的问题在哪里? – Sebivor 2013-03-15 02:11:19