2

I am reading a file of the following form:

c h a r a c t e r s

with fscanf:

fscanf(file, "%c", address);

in a loop. Will it read spaces as well? I want to ignore spaces. Should I use

fscanf(file, "%c ", address);

instead? However, there is no space after the last character in line, so I'm thinking it wouldn't read the last char as it is not in "x " for but "x".

I cannot check because the program is not finished enough yet.

4

2 に答える 2

4

フォーマットの前にスペースを置きます。

scanf(" %c", address)

スペースは実際には「ゼロ以上の空白を読み取る」ことを意味するため、" %c"「オプションの空白を無視して文字を読み取る (必ずしも空白ではない)」ことを意味します。

于 2013-03-28T17:05:39.920 に答える
0

あなたが言ったように、文字列形式にスペースを追加しますが、文字列形式の先頭に" %c"

int main()
{
    FILE *file = fopen("file.txt","r");
    char address;
    while(fscanf(file ," %c", &address)>0) {
        printf("_%c", address);
    }
    printf("_\n");
}
于 2013-03-28T17:03:55.927 に答える