Read All File Contents Into A Dynamically Allocated String | C Programming Example

แชร์
ฝัง
  • เผยแพร่เมื่อ 2 ก.ย. 2022
  • How to read and store all the contents of a file into a dynamically allocated string using C. An optimized solution that only requires reading the file once is created, after first creating a suboptimal but easier to understand solution that requires reading the file twice. A simple performance test is conducted to demonstrate the improved performance of the optimized solution.
    Source code for the optimized solution: github.com/portfoliocourses/c...
    Source code for the suboptimal solution: github.com/portfoliocourses/c...
    Check out www.portfoliocourses.com to build a portfolio that will impress employers!

ความคิดเห็น • 36

  • @olalha7198
    @olalha7198 ปีที่แล้ว +7

    Some of the best, most straightforward C tutorials on TH-cam!

    • @PortfolioCourses
      @PortfolioCourses  ปีที่แล้ว +1

      Thank you for the very positive feedback, that's much appreciated! :-)

  • @liolio1230
    @liolio1230 ปีที่แล้ว +1

    Thank you so much for the explanation and simple, easy to follow examples you provide. I managed to score A on my final exam only because of your videos. Immensely appreciated! Easy SUB!

    • @PortfolioCourses
      @PortfolioCourses  ปีที่แล้ว +1

      Wahoo congratulations on scoring an A on your final exam!!! 🙌 🥳 And you're very welcome, I'm so glad to hear the videos have been helpful for you. Thank you for the sub and welcome aboard! :-)

  • @Brock-Landers
    @Brock-Landers ปีที่แล้ว +1

    Instant file size result even on an old XT 8088:
    fseek(file, 0L, SEEK_END);
    total = ftell(file);
    rewind(file);
    But it's nice that you gave the viewers a memory management bonus lesson. :)

    • @PortfolioCourses
      @PortfolioCourses  ปีที่แล้ว

      Yep, that'll work too. :-)

    • @PortfolioCourses
      @PortfolioCourses  ปีที่แล้ว

      That said... I'm trying to find documentation that fseek() and ftell() will give us an "instant" result but I can't find anything (yet). It seems like the performance may be dependent on the implementation of the functions, but again I can't find much yet.

  • @gerdsfargen6687
    @gerdsfargen6687 ปีที่แล้ว +5

    Your content is incredible.

    • @PortfolioCourses
      @PortfolioCourses  ปีที่แล้ว +1

      Thank you very much for the kind feedback! :-D

    • @gerdsfargen6687
      @gerdsfargen6687 ปีที่แล้ว +1

      @@PortfolioCourses Not at all, I mean I basically spent all night, no sleep and excited at being able to finally understand and apply true C with greater confidence than ever. It's the way you deliver the content that makes it so engaging and addictive to follow.
      This morning I've been able to tackle the problem I've been sitting on for a while. I really didn't think I had it in me to tackle the problem but binging on your videos has changed this.
      May your subs keep rising!

    • @PortfolioCourses
      @PortfolioCourses  ปีที่แล้ว

      @@gerdsfargen6687 That's really awesome to hear that, thanks so much for sharing that with me. I'm super glad these videos are helping you out, and I hope you continue on your journey as a programmer with lots of success! 😀

    • @maorberenstein2540
      @maorberenstein2540 ปีที่แล้ว

      @Portfolio Courses your videos really are something else! Thank you 😊

  • @airrsongs
    @airrsongs ปีที่แล้ว +1

    I think using stat, calloc, and fread might be even more optimized.
    But I like the way you take us through this tutorial. Just subscribed to the channel.
    👍

    • @PortfolioCourses
      @PortfolioCourses  ปีที่แล้ว

      Maybe I can make a video where we do it that way instead! Thank you for subscribing, and welcome aboard! :-)

  • @D_Ladybug
    @D_Ladybug ปีที่แล้ว +1

    Weldone boss. I love you.
    kindly consider do an explanatory video about bit manipulations, their uses and how.. especially with number.. odd numbers, even numbers and other applications

    • @PortfolioCourses
      @PortfolioCourses  ปีที่แล้ว

      Haha thanks! And maybe I can do more videos on bit manipulations. Right now I have this video: th-cam.com/video/WBim3afbYQw/w-d-xo.html. :-)

    • @D_Ladybug
      @D_Ladybug ปีที่แล้ว +1

      Waw.. I love you...
      Please do more on the various applications.

    • @PortfolioCourses
      @PortfolioCourses  ปีที่แล้ว

      @@D_Ladybug Cool, I'll add that to my list of video ideas. 🙂

  • @technicalgamer2565
    @technicalgamer2565 ปีที่แล้ว

    bro you are so underrated came to your channel and got answers for those questions which i think that was none sense and finally getting 5th heart......

    • @PortfolioCourses
      @PortfolioCourses  ปีที่แล้ว +1

      That’s awesome I’m glad these videos are helping you out! :-) Any help spreading the word about the channel in Facebook groups or whatever other channels is definitely appreciated, if you can think of other people that would benefit from it too.

    • @technicalgamer2565
      @technicalgamer2565 ปีที่แล้ว

      Yeh tommorow i go to college and tell all my friends about you bro

    • @PortfolioCourses
      @PortfolioCourses  ปีที่แล้ว

      Awesome, thanks so much! :-)

  • @atiyahussain3837
    @atiyahussain3837 ปีที่แล้ว +2

    Hi, I wanted to ask if you have a char array, where we have stored lines from a file, is there a way to check the type of the elements stored? So, for example if you iterate through the char array, that contains lines from a file, you come across a digit , convert it to a double. I'm working on a calculator project in c , that requires me to push elements of type double onto a stack and perform operations on them, once we come across a string in the file. However, I'm having difficulties converting the lines to ints and pushing them onto the stack. Is there anyway you could help? Thanks.

    • @PortfolioCourses
      @PortfolioCourses  ปีที่แล้ว +2

      Great question Atiya! :-) Do you know the exact format of the file? Does each line contain one int? It would depend on the format of the file.

    • @atiyahussain3837
      @atiyahussain3837 ปีที่แล้ว +1

      @@PortfolioCourses Hi, the user has provides the input. So each line contains either an int or a string. The string is an operation: add, multiply, divide etc and the numbers are what the operations are performed on. So the example input that will be added to the file is: 1-new line-2-newline-add-newline-3-newline-4-newline-subtract. I've stored the values in a char array and have made a dynamic stack. The idea is I read each line from the file and if it is an integer, I push this onto the stack and if I come across an operation I perform that operation on the digits in the stack, pop off those digits and push the answer to the top. I need help with converting the digits to a double type as my stack is of this type. Thanks.

    • @PortfolioCourses
      @PortfolioCourses  ปีที่แล้ว +1

      @@atiyahussain3837 Hmmm this might help with the conversion: th-cam.com/video/UQOlTdArCRo/w-d-xo.html. 🙂 I don't think I can do a video on this soon but it sounds like an interesting problem, I've added it to my video idea list.

  • @funnymonkeypicture5487
    @funnymonkeypicture5487 ปีที่แล้ว

    This video helped a lot so thank-you. However i'm trying to do this exact thing except store each line of the file into a 2D string array so each line is it's own string. Would it be better to just use the method you have shown then use strtok() with \0 as the delimiter or is there a better way to go about this? Thanks Subscribed

    • @PortfolioCourses
      @PortfolioCourses  ปีที่แล้ว +1

      Great question! Hmm, I made this video on reading the lines of a file into an array of strings: th-cam.com/video/X-1qodkHCHo/w-d-xo.html. Maybe that video can help you? And welcome aboard! :-)

  • @asiatradelink7789
    @asiatradelink7789 ปีที่แล้ว

    you can do what you want!

  • @yigitbulut4972
    @yigitbulut4972 ปีที่แล้ว

    How do we make a string array for each line instead of just one big string?

    • @PortfolioCourses
      @PortfolioCourses  ปีที่แล้ว

      This video may help you with that: th-cam.com/video/X-1qodkHCHo/w-d-xo.html. :-)

  • @eonryan8491
    @eonryan8491 ปีที่แล้ว

    9:02

    • @PortfolioCourses
      @PortfolioCourses  ปีที่แล้ว

      Is there something at this point in the video I should know about? 🙂

  • @ivanmartic6765
    @ivanmartic6765 ปีที่แล้ว

    #include
    #include
    #include
    typedef struct studenti
    {
    char ime[20],prezime[20];
    int ocena;
    double prosek;
    struct studenti *sledeci;
    }STUDENTI;
    STUDENTI *napravi(char *ime, char *prezime, int ocena)
    {
    STUDENTI *novi=(STUDENTI*)malloc(sizeof(STUDENTI));
    strcpy(novi->ime,ime);
    strcpy(novi->prezime,prezime);
    novi->ocena=ocena;
    novi->sledeci=NULL;
    return novi;
    }
    STUDENTI *dodaj(STUDENTI *glava, char *ime, char *prezime, int ocena)
    {
    STUDENTI *novi=napravi(ime, prezime, ocena);
    if (glava==NULL)
    {
    glava=novi;
    return glava;
    }
    novi->sledeci=glava->sledeci;
    glava->sledeci=novi;
    }
    STUDENTI *ucitaj(STUDENTI *glava,char*fajl)
    {
    FILE *fp=fopen(fajl,"r");
    char linija[150], ime[25], prezime[25];
    int ocena;
    int pr=0;
    int suma=0;
    while(fgets(linija,sizeof(linija),fp))
    {
    char *token=strtok(linija," ");
    int br=0;
    while(token!=NULL)
    {
    if(br==0) strcpy(ime,token);
    if(br==1) strcpy(prezime,token);
    if(br==2)
    {
    ocena=atoi(token);
    pr++;
    suma=suma+ocena;
    }
    br++;
    token=strtok(NULL," ");
    }
    glava=dodaj(glava,ime, prezime, ocena);
    }
    return(glava);
    }
    void printaj(STUDENTI *glava)
    {
    if (glava==NULL)
    {
    printf("KRAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAJ");
    return;
    }
    printf("%s %s %d
    ",glava->ime,glava->prezime,glava->ocena);
    printaj(glava->sledeci);
    }
    void prosek(STUDENTI *glava)
    {
    double suma=0.0;
    double prosek=0.0;
    int br=0;
    STUDENTI *tmp=glava;
    while(tmp!=NULL)
    {
    br++;
    suma+=tmp->ocena;
    tmp=tmp->sledeci;
    }
    prosek=suma/br;
    printf("Prosek je: %f",prosek);
    }

    • @PortfolioCourses
      @PortfolioCourses  ปีที่แล้ว

      Did you have a question Ivan, or are you just wanting to share this code? :-) That is totally OK if that is the case, thank you for sharing the code with others.