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
C 用stdiolibary将文件内容读入字符串_C_Linux_File_Reader - Fatal编程技术网

C 用stdiolibary将文件内容读入字符串

C 用stdiolibary将文件内容读入字符串,c,linux,file,reader,C,Linux,File,Reader,试图将文件内容读入字符串(char*)时,我在挣扎。 我只需要使用stdio.h库,因此无法使用malloc分配内存 如何读取文件的所有内容并将其返回为字符串?我将尝试回答您的问题。但是请记住——我对C还是相当陌生,所以可能会有更好的解决方案,在这种情况下,请让我也知道!) 编辑:还有 以下是我的看法。。。 您需要知道要存储在字符串中的文件的大小。 更准确地说,在我提供的示例解决方案中,您需要知道输入文件中有多少个字符 使用malloc所做的一切就是在“堆”上动态分配内存。我想你已经知道了。。。

试图将文件内容读入字符串(char*)时,我在挣扎。 我只需要使用stdio.h库,因此无法使用malloc分配内存


如何读取文件的所有内容并将其返回为字符串?

我将尝试回答您的问题。但是请记住——我对C还是相当陌生,所以可能会有更好的解决方案,在这种情况下,请让我也知道!)

编辑:还有

以下是我的看法。。。 您需要知道要存储在字符串中的文件的大小。 更准确地说,在我提供的示例解决方案中,您需要知道输入文件中有多少个字符

使用
malloc
所做的一切就是在“堆”上动态分配内存。我想你已经知道了。。。 因此,无论您的
字符串
在内存(堆栈或堆)中的位置如何,您都需要知道该
字符串
的大小,以便输入文件的所有内容都能放入其中

这是一个快速的伪代码和一些解决您问题的代码。。。我希望这是有益的,我愿意学习更好的方法,如果有一个,和平

伪代码:

  • 打开输入文件进行读取

  • 找出文件的大小

    • 向前搜索文件位置指示器至文件末尾

    • 获取infle中的总字节数(字符)

    • 将文件位置指示器倒回起始位置 (因为我们将再次阅读)

  • 为“\0”声明一个字符数组,大小为-infle+1中所有计数的字符 在字符串的末尾

  • 将填充的所有字符读入数组

  • 用“\0”终止字符串

  • 关闭输入文件

  • 下面是一个简单的程序,它可以执行此操作并打印字符串:

    #include <stdio.h>
    
    int main(void)
    {
        // Open input file
        FILE *infile = fopen("hello.py", "r");
        if (!infile)
        {
            printf("Failed to open input file\n");
            return 1;
        }
    
        ////////////////////////////////
        // Getting file size
    
        // seek file position indicator to the end of the file
        fseek(infile, 0L, SEEK_END);
    
        // get the total number of bytes (chars) in infile
        int size = ftell(infile); // ask for the position
    
        // Rewind file position indicator back to the start of the file
        rewind(infile);
    
        //////////////////////////////////
    
        // Declaring a char array, size of - all the chars from input file + 1 for the '\0'
        char str[size + 1];
    
        // Read all chars of infile in to the array
        fread(&str, sizeof(char), size, infile);
    
        // Terminate the string
        str[size] = '\0'; // since we are zero indexed 'size' happens to be the last element of our array[size + 1]
    
        printf("%s\n", str);
    
        // Close the input file
        fclose(infile);
    
        // The end
        return 0;
    
    }
    
    #包括
    内部主(空)
    {
    //打开输入文件
    文件*infle=fopen(“hello.py”,“r”);
    如果(!infle)
    {
    printf(“打开输入文件失败\n”);
    返回1;
    }
    ////////////////////////////////
    //获取文件大小
    //查找文件位置指示器至文件末尾
    fseek(填充,0升,搜索结束);
    //获取infle中的总字节数(字符)
    int size=ftell(infle);//询问职位
    //将文件位置指示器倒回文件的开头
    倒带(填充);
    //////////////////////////////////
    //声明字符数组,大小为“\0”的输入文件+1中的所有字符
    字符str[size+1];
    //将填充的所有字符读入数组
    fread(&str,sizeof(char),size,infle);
    //终止字符串
    str[size]='\0';//由于我们的索引为零,“size”恰好是数组[size+1]的最后一个元素
    printf(“%s\n”,str);
    //关闭输入文件
    fclose(infle);
    //结局
    返回0;
    }
    
    有多种方法可以做到这一点。您试图克服的具体问题是什么?为什么您认为您需要
    malloc
    ?提示:要获得文件的大小,请阅读此问题,如果您使用
    malloc
    在此处发布您的解决方案,并询问如何不使用它。@reByte:始终将代码放入您的问题中,除非它是简单的,简短的陈述。