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...
***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! 🙂
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!
You’re welcome, I’m glad to hear you enjoyed the explanation! :-)
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.
You're welcome Firas! :-)
i spent hours figuering out how to do that unsuccessfully until watching this. thanx!
You're welcome! I'm glad to hear it helped! :-)
Thank you so so so much for your channel !!!
You’re welcome! :-)
that was really helpful thanks
You're very welcome! :-)
Thank you very much!
You’re very welcome! :-)
You are a god send. You made this 100x easier than my professor
Thank you for the kind feedback Emanuel! I’m very glad to hear this video helped you out. :-)
@@PortfolioCourses no problem! Do you by chance have a video like this but using dynamic memory allocation instead of a 2d array?
@@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.
Hell, he didn't even explain it. Just introduces all the informations about functions and give bunch of exercises as homework.
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 🌷
you would have to use a struct
Thank you
You’re welcome Stacy! :-)
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?
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. :-)
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.
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! :-)
@@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!
@@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!
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?
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. 🙂
Great video. Have you got a link to the dynamically allocated example?
Thank you! :-) And no, I haven't been able to make that video yet.
I just posted it here now: ruclips.net/video/vQno9S3yF80/видео.html . :-)
what if I want to store the string "Line" in an array and the number "1" in a separate array?
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. :-)
how would you count the number of chars in each of the individual words
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.
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
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. 🙂
@@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
@@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.
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
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! :-)
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!
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]);
@@PortfolioCourses Thank you very much for the answer to my question, keep it up!
You're welcome! :-)
Do you know how to put a file with multiple lines sentences and place each words
Like do you have to 3 array
Sorry I'm not sure I understand the question Sansayan. :-( Do you mean how do we write to a file content of some kind?
how do you create the file.txt part
How do we create the file.txt text file? We could use a text editor to do that, like Visual Studio Code. 🙂
[Error] 'MAX_LINES' undeclared (first use in this function)
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.
how would you do this in c++?
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.
Do you need to free the array?
Great question! :-) The strings are not dynamically allocated, so we don't need to free the array.
hdf
Sorry I don’t know what hdf means. :-)