将文本文件复制到char*数组中。

时间:2022-03-27 19:36:00
FILE *fp;
char *f_array[256];
int f_length;
int *a = 0;

fp = fopen("test.txt", "r");
fseek(fp, 0, SEEK_END);
f_length = ftell(fp);
printf("%d\n\n", f_length);
int n = 1, i = 0;
while (n > 0)
{
    n = fscanf(fp, "%s", f_array[i]);
    i++;
}

I am trying to copy the contents of a .txt file into a char* array. Much like what would happen with InternetReadFile and lpbuffer. However, I cannot seem to get this right. I need my array to be filled with the contents of the .txt file character by character. Any suggestions? I need the array to be single-dimensional

我正在尝试将.txt文件的内容复制到一个char*数组中。很像InternetReadFile和lpbuffer会发生什么。然而,我似乎做不到这一点。我需要我的数组填充。txt文件字符的内容。有什么建议吗?我需要这个数组是单维的。

2 个解决方案

#1


0  

You are calculating the length of the file but you don't use this information in any way.

您正在计算文件的长度,但是您不会以任何方式使用这些信息。

char *f_array[256];

This is an array of dimension 256 of pointers to char. f_array[i] is uninitialized and it's pointing into the digital nirvana, passing it to fscanf yields undefined behaviour. You would need to declare it as something like this:

这是一个指向char的256维数组。f_array[i]是未初始化的,它指向数字nirvana,将其传递给fscanf生成未定义的行为。你需要声明如下:

char f_array[255][255];

But then you are limiting yourself to max 255 strings, you are not storing it into a single string. Also you are storing max. 255 words. Use fgets or fread to get the whole content at once.

但是你将自己限制在最大255个字符串中,而不是把它存储在一个字符串中。你也在存储max。255个单词。使用fgets或fread一次获得整个内容。

char file[f_length + 1];

rewind(fp);

fread(file, f_length, 1, fp);

file[f_length] = 0; // terminate the string

printf("Whole file is: %s\n", file);

Here you are storing the whole file in an array of chars. Also after setting the file at the end, you'll need to rewind the file to the beginning, otherwise you are not reading anything.

在这里,您将整个文件存储在一个chars数组中。在最后设置完文件之后,您需要将文件重新卷到开头,否则您将不会读取任何内容。

#2


0  

The issue is mainly to do with your data types. You want to store the file in memory. That would be a character (byte) array, but you have created an "array of pointers":

这个问题主要与您的数据类型有关。您希望将文件存储在内存中。这将是一个字符(字节)数组,但您已经创建了一个“指针数组”:

char *f_array[256];

When you probably wanted just:

当你想要的时候:

char f_array[256];

Then, to do as you asked and read character by character into that array, use fgetc. Note that fgetc will be far less efficient that just reading the whole file in a single function call with fread. The kernel:

然后,按照您所要求的,将字符按字符读入该数组,使用fgetc。注意,fgetc在使用fread的单个函数调用中读取整个文件的效率要低得多。内核:

while ( EOF != (c = fgetc( fp )) && ++i < MAX_LEN )
    f_array[ i ] = c;

In context of a working example:

在一个工作示例的上下文中:

#include <stdio.h>
int main ( ) {
    const size_t MAX_LEN = 255;
    FILE * fp;
    char f_array[ MAX_LEN +1];
    int c;
    size_t i = -1;
    f_array[ MAX_LEN +1] = 0;

    fp = fopen("test.txt","r");

    if ( NULL == fp )
        perror("Error opening file");
    else {
        while ( EOF != (c = fgetc( fp )) && ++i < MAX_LEN )
            f_array[ i ] = c;

        fclose (fp);
    }
    f_array[ i ] = 0;
    printf("%zu bytes read\n\n", i);
    printf("Content read:\n%s\n", f_array);

    return 0;
}

#1


0  

You are calculating the length of the file but you don't use this information in any way.

您正在计算文件的长度,但是您不会以任何方式使用这些信息。

char *f_array[256];

This is an array of dimension 256 of pointers to char. f_array[i] is uninitialized and it's pointing into the digital nirvana, passing it to fscanf yields undefined behaviour. You would need to declare it as something like this:

这是一个指向char的256维数组。f_array[i]是未初始化的,它指向数字nirvana,将其传递给fscanf生成未定义的行为。你需要声明如下:

char f_array[255][255];

But then you are limiting yourself to max 255 strings, you are not storing it into a single string. Also you are storing max. 255 words. Use fgets or fread to get the whole content at once.

但是你将自己限制在最大255个字符串中,而不是把它存储在一个字符串中。你也在存储max。255个单词。使用fgets或fread一次获得整个内容。

char file[f_length + 1];

rewind(fp);

fread(file, f_length, 1, fp);

file[f_length] = 0; // terminate the string

printf("Whole file is: %s\n", file);

Here you are storing the whole file in an array of chars. Also after setting the file at the end, you'll need to rewind the file to the beginning, otherwise you are not reading anything.

在这里,您将整个文件存储在一个chars数组中。在最后设置完文件之后,您需要将文件重新卷到开头,否则您将不会读取任何内容。

#2


0  

The issue is mainly to do with your data types. You want to store the file in memory. That would be a character (byte) array, but you have created an "array of pointers":

这个问题主要与您的数据类型有关。您希望将文件存储在内存中。这将是一个字符(字节)数组,但您已经创建了一个“指针数组”:

char *f_array[256];

When you probably wanted just:

当你想要的时候:

char f_array[256];

Then, to do as you asked and read character by character into that array, use fgetc. Note that fgetc will be far less efficient that just reading the whole file in a single function call with fread. The kernel:

然后,按照您所要求的,将字符按字符读入该数组,使用fgetc。注意,fgetc在使用fread的单个函数调用中读取整个文件的效率要低得多。内核:

while ( EOF != (c = fgetc( fp )) && ++i < MAX_LEN )
    f_array[ i ] = c;

In context of a working example:

在一个工作示例的上下文中:

#include <stdio.h>
int main ( ) {
    const size_t MAX_LEN = 255;
    FILE * fp;
    char f_array[ MAX_LEN +1];
    int c;
    size_t i = -1;
    f_array[ MAX_LEN +1] = 0;

    fp = fopen("test.txt","r");

    if ( NULL == fp )
        perror("Error opening file");
    else {
        while ( EOF != (c = fgetc( fp )) && ++i < MAX_LEN )
            f_array[ i ] = c;

        fclose (fp);
    }
    f_array[ i ] = 0;
    printf("%zu bytes read\n\n", i);
    printf("Content read:\n%s\n", f_array);

    return 0;
}