Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用fread()将文件内容加载到C中字符串数组的第二个元素中?_C_Arrays_File_Pointers_Fread - Fatal编程技术网

如何使用fread()将文件内容加载到C中字符串数组的第二个元素中?

如何使用fread()将文件内容加载到C中字符串数组的第二个元素中?,c,arrays,file,pointers,fread,C,Arrays,File,Pointers,Fread,我很难理解指针在特定情况下是如何工作的。我的困惑的细节概述如下 我已经创建了一个名为buffer的8字节字符串字符数组,并尝试使用fread()将文件中的8字节加载到这个8字节字符串数组的一个元素中 我位于/c/file.txt的文件的内容很简单:TESTTEST 我希望程序将TESTTEST加载到缓冲区[1]中,而不是值ijklmnop,仅此而已 我知道缓冲区实际上是一个指针数组,在本例中,它只是三个连续的内存地址,其中包含三个连续字节数组的第一个元素的地址,这些数组不是以null结尾的 我将

我很难理解指针在特定情况下是如何工作的。我的困惑的细节概述如下

我已经创建了一个名为buffer的8字节字符串字符数组,并尝试使用fread()将文件中的8字节加载到这个8字节字符串数组的一个元素中

我位于/c/file.txt的文件的内容很简单:TESTTEST

我希望程序将TESTTEST加载到缓冲区[1]中,而不是值ijklmnop,仅此而已

我知道缓冲区实际上是一个指针数组,在本例中,它只是三个连续的内存地址,其中包含三个连续字节数组的第一个元素的地址,这些数组不是以null结尾的

我将其可视化为一行中的24个内存地址,其中包含ABCDEFGHIjklmnopqrstuvxx的二进制值,末尾没有\0

缓冲区[0]的地址指向A的地址,缓冲区[1]的地址指向i的地址,缓冲区[2]的地址指向Q的地址

我将fread()到&buffer[1]的过程视为将8个字节读入存储在buffer[1]中的地址处的位置

我尝试过使用&buffer+1、buffer+1、(buffer+1)等各种方法来实现这个fread(),但大多数方法都会给我带来分段错误&缓冲区[1]似乎几乎满足了我的要求

正在尝试执行printf(“缓冲区[1],因为%%s是%s\n”,缓冲区[1]);在fclose()之前的末尾,我遇到了一个分段错误

我已经接近理解了,但有些东西不太正确,我没有掌握一些我需要掌握的基本概念

我不确定终止(buffer+1)[8]是否溢出到buffer[2]的第一个地址,或者这到底有什么影响

总的来说,我只是有点困惑,任何帮助都将不胜感激

谢谢大家!

以下是我的整个计划:

#include <stdio.h>
#include <string.h>

int main()
{
    FILE *fp;
    char c[] = "OFFONOFF";
    char *buffer[] = {"ABCDEFGH", "ijklmnop", "QRSTUVWX"};
    char *t;

    printf("the value in file /c/file.txt is TESTTEST, only those 8 bytes\n");
    printf("the ls listing for /c/file.txt shows 9, I am not sure where the other character comes from\n");

    printf("the value as %%s of buffer[0] is %s\n", buffer[0]);
    printf("the value as %%s of buffer[1] is %s\n", buffer[1]);
    printf("the value as %%s of buffer[2] is %s\n", buffer[2]);
    printf("the sizeof(buffer[2]) is %d\n", sizeof(buffer[2]));

    fp = fopen("/c/file.txt", "r");
    fseek(fp, SEEK_SET, 0);

    printf("the value as %%c of buffer[1][3] is %c\n", buffer[1][3]);
    printf("the memory address as %%p for  buffer+1 is %p\n", buffer+1);

    fread(&buffer[1], strlen(c), 1, fp);

    /* I am attempting to terminate the fread into &buffer[1] with a \0, removing this doesn't change much */
    (buffer+1)[8] = "\0";

    printf("the memory addreses below are all in %%p format\n");
    printf("the memory address of buffer is %p\n", buffer);
    printf("memory address of buffer[1] is %p\n", buffer[1]);
    printf("memory address of &buffer is %p\n", &buffer);
    printf("memory address of &buffer+1 is %p\n", &buffer+1);
    printf("memory address of &buffer[1] is %p\n", &buffer[1]);
    printf("memory address of buffer is %p\n", buffer);
    printf("memory address of buffer[15] is %p\n", buffer[15]);
    printf("memory address of buffer+15 is %p\n", buffer+15);
    printf("memory address of *(buffer+1) is %p\n", *(buffer+1));
    printf("the sizeof(buffer[0]) is %d\n", sizeof(buffer[0]));
    printf("the sizeof(*(buffer+1)) is %d\n", sizeof(*(buffer+1)));
    printf("the sizeof(buffer) is %d\n", sizeof(buffer));
    printf("the value of *(buffer+0) as %%s is %s\n", *(buffer+0));
    printf("the value of *buffer, the first element of the array of strings, as %%s is %s\n", *buffer);
    printf("the value of buffer+1, the first element of the array of strings, as %%s is %s\n", buffer+1);
    printf("buffer[0] as %%s is %s\n", buffer[0]);
    printf("buffer+1 as %%s is %s\n", buffer+1);
    printf("buffer[2] as %%s is %s\n", buffer[2]);
    printf("buffer is %s\n", buffer+1);
    printf("sizeof(*buffer) is %d\n", sizeof(*buffer));

    fclose(fp);

    return(0);
}
fread(&buffer[1],strlen(c),1,fp)似乎覆盖了指针本身

似乎您想要的是一个二维数组,而不是一个指向字符串文本的指针数组(在大多数现代平台上都是只读的:
char*abc=“abc”;abc[0]=“d”
通常是segfaults):

the value in file /c/file.txt is TESTTEST, only those 8 bytes
the ls listing for /c/file.txt shows 9, I am not sure where the other character comes from
the value as %s of buffer[0] is ABCDEFGH
the value as %s of buffer[1] is ijklmnop
the value as %s of buffer[2] is QRSTUVWX
the sizeof(buffer[2]) is 8
the value as %c of buffer[1][3] is l
the memory address as %p for  buffer+1 is 0x7ffff7b23a28
the memory addreses below are all in %p format
the memory address of buffer is 0x7ffff7b23a20
memory address of buffer[1] is 0x5453455454534554
memory address of &buffer is 0x7ffff7b23a20
memory address of &buffer+1 is 0x7ffff7b23a38
memory address of &buffer[1] is 0x7ffff7b23a28
memory address of buffer is 0x7ffff7b23a20
memory address of buffer[15] is 0x400674
memory address of buffer+15 is 0x7ffff7b23a98
memory address of *(buffer+1) is 0x5453455454534554
the sizeof(buffer[0]) is 8
the sizeof(*(buffer+1)) is 8
the sizeof(buffer) is 24
the value of *(buffer+0) as %s is ABCDEFGH
the value of *buffer, the first element of the array of strings, as %s is ABCDEFGH
the value of buffer+1, the first element of the array of strings, as %s is TESTTEST

@
buffer[0] as %s is ABCDEFGH
buffer+1 as %s is TESTTEST

@
buffer[2] as %s is QRSTUVWX
buffer is TESTTEST

@
char buffer[][9] = {"ABCDEFGH", "ijklmnop", "QRSTUVWX"};