c. Question for receiving files in a two-dimensional array of languages

Asked 2 years ago, Updated 2 years ago, 44 views

#define _CRT_SECURE_NO_WARNINGS   
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define w 5
#define h 16
pt_buffer(char buffer[w][h])//array output function
{
    int i = 0;
    while (buffer[i][0] != '\0')
    {
        printf("%s", buffer[i]);
        i++;
    }

}
int main()
{
    FILE *fp = fopen("indata.txt", "w");

    fputs("-----I-I------\n"
          "----I---I-----\n"
          "----I----IIII-\n"
          "----I----IIII-\n",
          fp);
    // Saving a String to a File

    fclose(fp);
    // Close File Pointer

    char buffer[4][14];
    // Temporary space to use when reading files
    int i = 0;
    FILE *ff = fopen("indata.txt", "r");
    // Open the hello.txt file in read mode.  
    memset(buffer, NULL, 4 * 14);
    // Return File Pointer
    while (!feof(ff))
    {
        fgets(buffer[i], 14, ff);
        i++;
    };
    // Hello, world!: Output the contents of the file
    i = 0;
    fclose(ff);
    while (buffer[i][0] != NULL)
    {
        printf("%s", buffer[i]);
        i++;
    }

    return 0;
}

I don't know why this code is running but debugged..

c

2022-09-22 19:21

1 Answers

Debug refers to actions to resolve errors in a program, not problems that occur during program operation.

In C language, the string contains \0 at the end. If it is a string with 14 characters, the array size must be 15.

And in order to determine the end of an array in such a way as while(buffer[i][0]!=NULL), the array must be one more element than it actually contains. Therefore, it should be char buffer[5][15]; instead of char buffer[4][14];.

But there's one problem here. fgets(buffer[i],14,fff); When invoked like this, it reads 13 characters, not up to 14. The second factor you enter here means the size of the first factor. Because it is a string, the last digit is used for \0. However, if you change to fgets(buffer[i], 15, ff);, you will read 14 characters, but this will also cause problems.

The file contains 14 + 1 characters per line, such as -----I-I----\n. If it is executed as below,

fgets(buffer[0], 15, ff);
fgets(buffer[1], 15, ff);

buffer[0] will be ----I-I---- and buffer[1] will be \n--I---I----. Therefore, buffer[0] will be -----I-I----\n and buffer[1] will be ----I---\n.

Reflecting on the above mentioned content, it is as follows.

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main()
{
    FILE *fp = fopen("indata.txt", "w");

    fputs("-----I-I------\n"
          "----I---I-----\n"
          "----I----IIII-\n"
          "----I----IIII-\n",
          fp);
    // Saving a String to a File

    fclose(fp);
    // Close File Pointer

    char buffer[5][16];
    // Temporary space to use when reading files
    int i = 0;
    FILE *ff = fopen("indata.txt", "r");
    // Open the hello.txt file in read mode.
    memset(buffer, 0, sizeof(buffer));
    // Return File Pointer
    while (!feof(ff))
    {
        fgets(buffer[i],  16, ff);
        i++;
    };
    // Hello, world!: Output the contents of the file
    i = 0;
    fclose(ff);
    while (buffer[i][0] != '\0')
    {
        printf("%s", buffer[i]);
        i++;
    }

    return 0;
}


2022-09-22 19:21

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.