テキストファイルの編集をするプログラムを書いています。
テキストファイル(sample.txt)の内容を一部変更して、もう一つのテキストファイル(samplecopy.txt)に出力する、という手順です。
while文の冒頭に、
printf("%lu\n",strlen(temp));
を追加したところ、おかしいことが起こりました。
テキストファイル(sample.txt)の内容は
ABCDEF
ABCDE
ABCD
です。
これを実行すると
0
5
4
となり、whileループの1周目でtempに"ABCDEF"が入っていないようです。(2周目以降は入る)
これはなぜなのですか?
#include <string.h> int main(){ char str[256]; char temp[256]; FILE *input,*output; input = fopen("sample.txt","r"); output = fopen("samplecopy.txt","w"); printf("String you want to delete:"); scanf("%s",str); while(fscanf(input,"%s",temp)!=EOF){ printf("%lu\n",strlen(temp)); //ここです!! if(strcmp(str,temp)==0) continue; fprintf(output,"%s",temp); } fclose(output); fclose(input); return 0; } コード
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/07/18 08:49