There is a file, a string of unknown length is written in it, how can I write it into a char array? I tried something, but nothing came of it.

Here I removed the excess from the code, I'm not sure about the double pointer.

 char **str; //вот сюда надо записать из файла char ch; FILE *fp; if ((fp = fopen("test.txt","r"))==NULL) { printf("File not found.\n"); } do { ch = getc(fp);//read //тут видимо должен быть чудо-код } while(ch!=EOF); fclose(fp); 


    2 answers 2

    Like that

     #include <stdio.h> #include <stdlib.h> int main() { FILE* f = fopen("file.txt", "r"); unsigned int N = 10, delta=10, i = 0; char* buf = (char*) malloc (sizeof(char)*N); while ((buf [i] = fgetc(f)) != EOF ) { if (++i >= N) { N += delta; buf = (char*) realloc (buf, sizeof(char)*N); } } fclose(f); buf[i] = '\0'; puts (buf); free (buf); return 0; } 
    • 2
      Only if you need a string, then while you can believe in '\\ n' and exit. And in Linux there is a library function getline for such reading. - avp
    • What if there is only one line in the file and it does not end with '\n' ? - skegg
    • getline is great, but not portable. And the TS did not indicate the platform ... And then, getline like this. Let the student write the bikes, this is useful))) - skegg
    • Then the output will be on EOF (as you have written). Just add inside while if (buf [i] == '\\ n'). But getline () is quite convenient. - avp
    • Yes. Somehow I did not commit myself to the fact that exactly one line is required ... So let the TC itself correct the code for the exercise. - skegg

    If there is only 1 line in the file, then:

    1. Open file
    2. Move to end of file via fseek(FILE*, 0, SEEK_END)
    3. Through ftell get the position, it will be the length of the line
    4. 1 time to make a malloc for the size of what ftell returned
    5. Move to the beginning of the file
    6. In a loop, пока не EOF read the contents of the file into an array
    • one
      This, perhaps, would be better for a single line. (+) - BuilderC
    • 2
      @fogbit, in order to read the whole file into memory (and you are doing just that), the loop is not needed. One read is enough. And in this case it is better to do not fopen () (well, why is there an extra level of buffering), but open () and fstat (). Those. char * get_file_contents (char * name, size_t * size) {int fd = open (name, O_RDONLY); char * buf = NULL; * size = -1; if (fd! = -1) {struct stat st; fstat (fd, & st); if ((* size = st.st_size) && (buf = (char *) malloc (st.st_size))) read (fd, buf, st.st_size); close (fd); } return buf; } Like so. - avp 2:13 pm
    • one
      @avp, TC also wrote that he had windows. True, it could be MinGW ... - skegg
    • one
      @mikillskegg and happy should write under Contiki? @avp I'm afraid that not everything from GNU. IAR, Keil, but who knows if anyone else. - alexlz
    • 2
      @alexlz, I am happy that I can choose the platform myself for work. I chose Linux, and when I have to deal with Windows, I am very unhappy. - skegg