Read And Store Each Line Of A File Into An Array Of Strings | C Programming Example

Поделиться
HTML-код
  • Опубликовано: 1 июн 2022
  • How to read and store all the lines of a file into an array of strings in C. Source code: github.com/portfoliocourses/c.... Check out www.portfoliocourses.com to build a portfolio that will impress employers!
    ***Dynamically allocated array version of reading and storing all the lines of a file into a (dynamically allocated) array of strings: • Read All File Lines In...

Комментарии • 59

  • @PortfolioCourses
    @PortfolioCourses  Год назад +3

    ***DYNAMICALLY ALLOCATED ARRAY VERSION OF THE SAME PROBLEM: ruclips.net/video/vQno9S3yF80/видео.html. Want to learn how to read and store all the lines of a file into a dynamically allocated array of strings? Check that video out! 🙂

  • @1Nunok
    @1Nunok 2 года назад +3

    wow the explanation was super good, I'll stay tuned for the dynamic version, and thank you very much for leaving the source code explained!

    • @PortfolioCourses
      @PortfolioCourses  2 года назад +1

      You’re welcome, I’m glad to hear you enjoyed the explanation! :-)

  • @fifaham
    @fifaham Год назад +1

    This code can be used in tons of applications in embedded design, it is amazing how software and hardware can talk to each others and achieve great things. Thank you Kevin.

  • @adto251
    @adto251 2 года назад +2

    i spent hours figuering out how to do that unsuccessfully until watching this. thanx!

    • @PortfolioCourses
      @PortfolioCourses  2 года назад +1

      You're welcome! I'm glad to hear it helped! :-)

  • @thebluephoenix4219
    @thebluephoenix4219 2 года назад

    Thank you so so so much for your channel !!!

  • @youngcoders1437
    @youngcoders1437 Год назад

    that was really helpful thanks

  • @deinemama6303
    @deinemama6303 Год назад

    Thank you very much!

  • @emanuelmaya6069
    @emanuelmaya6069 Год назад +2

    You are a god send. You made this 100x easier than my professor

    • @PortfolioCourses
      @PortfolioCourses  Год назад

      Thank you for the kind feedback Emanuel! I’m very glad to hear this video helped you out. :-)

    • @emanuelmaya6069
      @emanuelmaya6069 Год назад

      @@PortfolioCourses no problem! Do you by chance have a video like this but using dynamic memory allocation instead of a 2d array?

    • @PortfolioCourses
      @PortfolioCourses  Год назад +2

      @@emanuelmaya6069 I have one on reading a file into a single dynamically allocated string: ruclips.net/video/Oa_ji3dDFew/видео.html. And I have this video on dynamically allocating a 2D array: ruclips.net/video/ZLc_OpzND2c/видео.html. I want to make a video on reading a file into a dynamically allocated 2D array, but I haven't been able to make it yet.

    • @DaiMoscv
      @DaiMoscv Год назад

      Hell, he didn't even explain it. Just introduces all the informations about functions and give bunch of exercises as homework.

  • @muhamedhashim6694
    @muhamedhashim6694 Год назад +1

    Is there a way i can differenciate between %s and %d like in fscanf(), i mean like in your example so i can then take "Line" and "1" and put them in another variables to use the somewhere else instead of the whole line as %s?
    Thanks for the great explanation 🌷

  • @stacyhackney6100
    @stacyhackney6100 2 года назад

    Thank you

  • @luciano15869
    @luciano15869 Год назад

    hi, I have to do this but with a file with a typedef struct(contains all variables int) and store the records from the file to the array wich is the same struct of the file, could be same but with fread instead of fgets?, or I need something else?

    • @PortfolioCourses
      @PortfolioCourses  Год назад

      Is the data in the file comma separated? This video may be helpful: ruclips.net/video/rbVt5v8NNe8/видео.html. Even if the data is not comma separated, that video may give you some ideas as it involves reading the data on each line of a file and storing it into a struct. :-)

  • @TheHartemisPvP
    @TheHartemisPvP Год назад

    What would you do if you wanted to input this data in a struct that has properties of char[] textbeforelinenumber, int linenumber? I understand how to get the strings from a file but I have a file that has ints I want to do something with afterwards.

    • @PortfolioCourses
      @PortfolioCourses  Год назад

      Is the data in the file comma-separated? If so this video might help you out: ruclips.net/video/rbVt5v8NNe8/видео.html. atoi() can be used to convert a string to an int, and that's covered in this video: ruclips.net/video/UQOlTdArCRo/видео.html. Hopefully those links help! :-)

    • @TheHartemisPvP
      @TheHartemisPvP Год назад

      @@PortfolioCourses I’m gonna check those videos out. I ended up getting it to work using fscanf and then atoi. I made a little short on my youtube channel on the project i was making if you wanna check it out!

    • @PortfolioCourses
      @PortfolioCourses  Год назад

      @@TheHartemisPvP That's great to hear you were able to get it to work! 🙂 Normally I reply to comments quicker than this but my wife and I have a new child this month and things have been hectic for me. And that's cool, I checked out the short, congratulations on figuring it out!

  • @artaghorbani5116
    @artaghorbani5116 Год назад

    Hi..Great video..but if we want to do this for struct of for example name and age ..do you have a video for that?

    • @PortfolioCourses
      @PortfolioCourses  Год назад

      I have this video on reading CSV data and storing it into a struct, it might be helpful for you: ruclips.net/video/rbVt5v8NNe8/видео.html. 🙂

  • @Yanner14
    @Yanner14 Год назад

    Great video. Have you got a link to the dynamically allocated example?

    • @PortfolioCourses
      @PortfolioCourses  Год назад

      Thank you! :-) And no, I haven't been able to make that video yet.

    • @PortfolioCourses
      @PortfolioCourses  Год назад

      I just posted it here now: ruclips.net/video/vQno9S3yF80/видео.html . :-)

  • @omarwalid8813
    @omarwalid8813 Год назад

    what if I want to store the string "Line" in an array and the number "1" in a separate array?

    • @PortfolioCourses
      @PortfolioCourses  Год назад

      Great question Omar! :-) You could read each line into a buffer char array, and then use strtok() to identify the first word and then the second word, and then copy those into separate char arrays:
      strtok: ruclips.net/video/nrO_pXGZc3Y/видео.html.
      strcpy: ruclips.net/video/RQ8O13utXQw/видео.html
      Or you could use something like: fscanf(file, "%s %s", array1[line], array2[line])
      Where fscanf() behaves a lot like scanf except it reads from a file. We could read each word and store them into separate arrays. :-)

  • @AnnaScott-bw2bx
    @AnnaScott-bw2bx Год назад

    how would you count the number of chars in each of the individual words

    • @PortfolioCourses
      @PortfolioCourses  Год назад

      Great question Anna. :-) If it were me, I would split the string (or strings) that contain the words into each individual word, using an algorithm like this: ruclips.net/video/Vp6OELK4gmo/видео.html. Then an algorithm like this could be used to count the occurrences of each character in each word/string: ruclips.net/video/PdiLIj943NY/видео.html.

  • @qwackyy1327
    @qwackyy1327 Год назад

    Hi, this doesn't work for me. Copied code line for line. It will print all the lines, except for the last line where it outputs "Segmentation fault (core dumped)". Any ideas

    • @PortfolioCourses
      @PortfolioCourses  Год назад

      The code in the video is available here: github.com/portfoliocourses/c-example-code/blob/main/file_lines_to_string_array.c. As long as the number of lines in the file do not exceed MAX_LINES, and as long as the number of characters on any line do not exceed MAX_LEN, then that error should not occur. That error would occur if memory that "did not belong to the program" was accessed, for example if there are more strings to store then there are rows available in the 2D char array, that error may occur. Hopefully this feedback and link to the original code will help you out. 🙂

    • @qwackyy1327
      @qwackyy1327 Год назад

      @@PortfolioCourses Thanks for the speedy reply, the file i am trying to read in is as follows
      "10 11
      11 11 X
      5 3 X
      8 3 P
      4 6 G"
      There are only 5 lines, even when running with the code provided on github, I am still getting that segmentation fault on the last line. I am running this code on a virtual machine provided by my university and i believe it runs with C89. Would that affect anything? Thanks in advance

    • @PortfolioCourses
      @PortfolioCourses  Год назад

      @@qwackyy1327 I copied those file contents into a file and then tried to run the program on GitHub as it is and it worked for me, so I'm not sure what's going on. This is a small thing, but in C89 you can't declare a variable inside a for loop like this:
      for (int i = 0; i < line; i++)
      printf("%s", data[i]);
      You would need to have something like:
      int i = 0;
      for (i = 0; i < line; i++)
      printf("%s", data[i]);
      I doubt that is the cause of the issue, but given you've mentioned you're using C89 I figured I should point that out.

  • @torakadira8938
    @torakadira8938 Год назад

    So if i want to use the array, what should i do to use the array? I mean like the variable to call the array

    • @PortfolioCourses
      @PortfolioCourses  Год назад

      I'm not sure I understand the question Tora. Do you want to create an array that is able to store strings? If that's the case you would do it like we do in this video, the code is available here: github.com/portfoliocourses/c-example-code/blob/main/file_lines_to_string_array.c. If you want to store the data in the file into a single 1D array, then you might find this video helpful: ruclips.net/video/CzAgM5bez-g/видео.html. Hopefully this helps! :-)

  • @naokigonzalez2450
    @naokigonzalez2450 Год назад

    Hello, thank you for your explanation! But there is one thing that is not clear to me.... How can I read letter by letter each of the sentences that have been read from the file? Thanks!

    • @PortfolioCourses
      @PortfolioCourses  Год назад +1

      Great question Naoki! :-) If we included string.h at the top of the file then at the bottom we could have:
      for (int i = 0; i < line; i++)
      for (int j = 0; j < strlen(data[i]); j++)
      printf("data[%d][%d] = %c", i, j, data[i][j]);

    • @naokigonzalez2450
      @naokigonzalez2450 Год назад

      @@PortfolioCourses Thank you very much for the answer to my question, keep it up!

    • @PortfolioCourses
      @PortfolioCourses  Год назад

      You're welcome! :-)

  • @sansayangajanithy242
    @sansayangajanithy242 Год назад

    Do you know how to put a file with multiple lines sentences and place each words
    Like do you have to 3 array

    • @PortfolioCourses
      @PortfolioCourses  Год назад

      Sorry I'm not sure I understand the question Sansayan. :-( Do you mean how do we write to a file content of some kind?

  • @nujerooz1480
    @nujerooz1480 Год назад

    how do you create the file.txt part

    • @PortfolioCourses
      @PortfolioCourses  Год назад

      How do we create the file.txt text file? We could use a text editor to do that, like Visual Studio Code. 🙂

  • @8dmusic251
    @8dmusic251 Год назад

    [Error] 'MAX_LINES' undeclared (first use in this function)

    • @PortfolioCourses
      @PortfolioCourses  Год назад

      We defined it at the very beginning of the video. If it's not there, the program won't work. 🙂 The code for the video is available here: github.com/portfoliocourses/c-example-code/blob/main/file_lines_to_string_array.c.

  • @uwais9498
    @uwais9498 Год назад

    how would you do this in c++?

    • @PortfolioCourses
      @PortfolioCourses  Год назад

      I will need to make a video on this topic to really answer it. :-) But in the meantime these answers here might be helpful: stackoverflow.com/questions/43435147/reading-text-file-into-string-array.

  • @catsithgaming
    @catsithgaming Год назад

    Do you need to free the array?

    • @PortfolioCourses
      @PortfolioCourses  Год назад

      Great question! :-) The strings are not dynamically allocated, so we don't need to free the array.

  • @ilaci5544
    @ilaci5544 Год назад +1

    hdf