Why does this code give warning: format '%s

2019-09-27 16:40发布

#include <stdio.h>
#include <stdlib.h>
#define RIG 5
#define COL 11

int main()
{
    FILE *fp;
    fp=fopen("swamp.txt","r");
    if((fp=fopen("swamp.txt","r"))==NULL)
    {
        puts("ERROR!");
        return -1;
    }
    char *swamp[RIG][COL];
    while(fscanf(fp,"%s",swamp)!=EOF)
    {
        printf("%s\n",swamp);
    }

    fclose(fp);

    return 0;
}

I'm working with files and I'm getting 2 warnings for the fscanf inside the while. Can somebody explain to me why?

标签: c file
2条回答
【Aperson】
2楼-- · 2019-09-27 17:07

Let's assume swamp.txt contains:

marsh
bog
quagmire
morass
fen

and that you want to read these lines into the array swamp in your program. Then you might revise your code along these lines. Notice that this avoids opening the file twice, amongst other cleanup operations.

#include <stdio.h>

#define RIG 5
#define COL 11

int main(void)
{
    const char filename[] = "swamp.txt";
    FILE *fp = fopen(filename, "r");
    if (fp == NULL)
    {
        fprintf(stderr, "failed to open file '%s' for reading\n", filename);
        return -1;
    }
    char swamp[RIG][COL];
    int i = 0;
    while (i < RIG && fscanf(fp, "%10s", swamp[i]) == 1)
        i++;

    fclose(fp);

    for (int j = 0; j < i; j++)
        printf("%d: %s\n", j, swamp[j]);

    return 0;
}

The output is:

0: marsh
1: bog
2: quagmire
3: morass
4: fen

Note that the code protects against overflow from a long file by counting words as they're read. You already checked fopen() — that was good. I improved the error message, though. In my opinion, you should never call fopen() with a literal string for the file name because when you report an error on opening the file, you need the file name in the error message, so you'd have to repeat yourself. I fixed the type of the array so it is a 2D array of char and not a 2D array of (uninitialized) char pointers. I arranged to pass each row of the array to fscanf() in turn. I limited the length of the input for each word to prevent overflows there, too.

查看更多
迷人小祖宗
3楼-- · 2019-09-27 17:17

For test.txt as below:

aaaaaaa bbbbbbb ccccccc
ddddddd eeeeeee fffffff
ggggggg hhhhhhh iiiiiii
jjjjjjj kkkkkkk lllllll

you can obtain data with this code (it can process words up to 8 characters in 4 rows and 3 columns):

#include <stdlib.h>
#include <stdio.h>

int main(void)
{
    enum {
        ROWS = 4,
        COLS = 3
    };

    FILE *fp;
    char test[ROWS][COLS][9];
    int counter = ROWS * COLS;

    if ((fp = fopen("test.txt", "r")) == NULL) {
        perror("fopen()");
        return EXIT_FAILURE;
    }

    for (int i = 0; i < ROWS * COLS; i++) {
        if (fscanf(fp, "%8s", test[i / COLS][i % COLS]) == EOF) {
            counter = i;
            break;
        }
    }

    fclose(fp);

    for (int i = 0; i < counter; i++) {
        printf("%s ", test[i / COLS][i % COLS]);
        if ((i + 1) % COLS == 0) {
            printf("\n");
        }
    }

    return EXIT_SUCCESS;
}
查看更多
登录 后发表回答