【发布时间】:2015-10-21 12:29:00
【问题描述】:
所以我在一个文件中有一个文本墙,我需要识别 $ 符号之间的一些单词并将它们称为数字,然后在另一个文件中打印修改后的文本以及数字对应的内容。 此外,未定义行,列最多应为 80 个字符。
例如:
I $like$ cats.
I [1] cats.
[1] --> like
这就是我所做的:
#include <stdio.h>
#include <stdlib.h>
#define N 80
#define MAX 9999
int main()
{
FILE *fp;
int i=0,count=0;
char matr[MAX][N];
if((fp = fopen("text.txt","r")) == NULL){
printf("Error.");
exit(EXIT_FAILURE);
}
while((fscanf(fp,"%s",matr[i])) != EOF){
printf("%s ",matr[i]);
if(matr[i] == '\0')
printf("\n");
//I was thinking maybe to find two $ but Idk how to replace the entire word
/*
if(matr[i] == '$')
count++;
if(count == 2){
...code...
}
*/
i++;
}
fclose(fp);
return 0;
}
我的问题是 fscanf 无法识别 '\0',因此当我打印数组时它不会进入下一行。我也不知道如何用数字替换 $word$。
【问题讨论】:
-
fscanf("%s")只会读一个字。如果要处理行,请使用fgets()。