我有一個簡單的程序,讀取所有文本文件的字符和閱讀過程中,它排除一些字符。如何比較C中文字的字符?
下面一個例子,以明確
這是我的txt文件的內容:
a b c d e e
f g d h i j
d d d e e e
我想刪除字符「d」和空間之後,它得到這樣的結果:
a b c e e
f g h i j
e e e
我的程序讀取後未刪除字符'd'及其空格。
這是我用來打開和閱讀txt文件的代碼:
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[])
{
if(argc==2)
{
FILE *file;
file = fopen(argv[1], "r");
int c;
char x = ' ';
if (file == NULL)
{
printf("Error\n");
return 1;
}
while(x != 'd')
{
c = fgetc(file);
if(feof(file))
{
break ;
}
printf("%c", c);
}
fclose(file);
}
return 0;
}
您沒有在while循環中設置'x'值。 –