Read string from file in C

后端 未结 3 538
春和景丽
春和景丽 2020-12-19 19:21

I have a file with multiple strings, each string on a separate line. All strings are 32 character long (so 33 with the \'\\n\' at the end).

I am trying to read all t

3条回答
  •  情话喂你
    2020-12-19 19:36

    Here is a basic approach:

    // create an line array of length 33 (32 characters plus space for the terminating \0)
    char line[33];
    // read the lines from the file
    while (!feof(fp)) {
        // put the first 32 characters of the line into 'line'
        fgets(line, 32, fp);
        // put a '\0' at the end to terminate the string
        line[32] = '\0';
        // print the result
        printf("%s\n", line);
    }
    

提交回复
热议问题