-3

Im struggleing by trying to read the files content into a string (char*). I just have to use the stdio.h libary, so I cant allocate memory with malloc.

How can I read all the content of a file and return it into a string?

reByte
  • 83
  • 7
  • 1
    There are multiple ways to do it. What specific problem are you trying to overcome? – John Bollinger May 09 '18 at 16:05
  • Why do you think you need `malloc`? – Eugene Sh. May 09 '18 at 16:05
  • sounds like homework – pm100 May 09 '18 at 16:06
  • I wanna create a simple copy & paste script. So what function do I need to get the string.. fscanf needs input format, fgetc gives me just a char.. so what do I need – reByte May 09 '18 at 16:07
  • Again. Let's assume you *can* use `malloc`. how will you implement it then ? – Eugene Sh. May 09 '18 at 16:09
  • You simple need an array of char that is bigger than the biggest file you expect to read. – Jabberwocky May 09 '18 at 16:12
  • I would use fread by referring a buffer to ptr – reByte May 09 '18 at 16:12
  • _by referring a buffer to ptr_ : what?? – Jabberwocky May 09 '18 at 16:14
  • 1
    Hint: to get the size of your file read [this](https://stackoverflow.com/questions/238603/how-can-i-get-a-files-size-in-c) – Jabberwocky May 09 '18 at 16:15
  • Yea this is using malloc what im trying to avoid – reByte May 09 '18 at 16:19
  • 2
    This question could look legit if you were posting here your solution using `malloc` and asking how to do without it. – Eugene Sh. May 09 '18 at 16:23
  • Legit ?? Why should a question look legit lol: char * content= 0; int length; fseek(file, 0, SEEK_END); length = ftell(file); fseek(file, 0, SEEK_SET); content = malloc(length); if (content){ fread(content, 1, length, file); } fclose(file); return content; – reByte May 09 '18 at 16:26
  • 2
    @reByte: ALWAYS put code into your question, never in a comment unless it is a simple, short statement. – cdarke May 09 '18 at 16:32
  • If you can't use `malloc` you need to define an array of `char` large enough to hold the file contents. If you can determine the file size and are allowed to define a variable-length array (VLA) you could define one of those. – Ian Abbott May 09 '18 at 16:37
  • If you're reading the file into a string you should `malloc` one byte extra so you have space to store the `NUL` and also add it as `fread` won't do it for you – Chris Turner May 09 '18 at 16:42
  • Please explain what exactly you're having. Do you have a pointer pointing to a memory whose size is guaranteed to be at least the file size and a NUL? – user202729 May 09 '18 at 16:55

1 Answers1

1

I will attempt to answer your question. Keep in mind though - I am still fairly new to C, so there could be a better solution out there in which case please let me know about it too! :)

EDIT: And there was...

Here is how I see it... You need to know the size of the file you want to store in a string. More precisely - in the example solution that I'm providing, you need to know how many chars are in your input file.

All you are doing with malloc is dynamically allocating memory on the 'heap'. I presume you already know that... So regardless of where your string lives in memory (stack or heap) you need to know how big that string has to be, so all of the contents of your input file would fit in to it.

Here is a quick pseudo-code and some code with a solution to your problem... I hope this is helpful and I am open to learn a better approach if there is one, peace!

Pseudo-code:

  1. open input file for reading

  2. find out the size of the file

    • forward seek file position indicator to the end of the file

    • get the total number of bytes (chars) in infile

    • rewind file position indicator back to the start (because we will be reading from it again)

  3. declare a char array, size of - all counted chars in infile + 1 for '\0' at the end of the string.

  4. read all chars of infile in to the array

  5. Terminate your string with '\0'

  6. Close input file

Here is a simple program that does just that and prints your string:

#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;

}
Arthur G
  • 26
  • 3