Read And Store Each Line Of A File Into An Array Of Strings | C++ Example
HTML-код
- Опубликовано: 17 окт 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!
I love your videos! One minor issue here though is I think you would want to use getline() for the user to enter the file name, instead of cin. Otherwise, the program will ignore spaces and it will think something like "my file.txt" is just "my"
In this video, I'm definitely assuming that the user will enter a filename with no spaces. 🙂
Was this suppose to open a file that is saved on the computer? When I type the name of the file which has a list of objects to sort through it says file not found.
really helped me
apreciated bro ❣❣❣❣
Dang that was super helpful! The next step is to apply this to my hw.
Now if those numbers at the end of those strings were in different order, how would you sort them to be in order?
Like if it was a grade & I need to sort the line based on the score.
You could use a sorting algorithm, or use the sort() function that's built-in to C++: ruclips.net/video/hP7aIrTRqQw/видео.html. 🙂
hey , can you tell me how to make it start reading from the second line ? because in my case the first line is the number of lines and not intended to be read
thanks in advance
Hmm Amr, one simple way is that before the loop you could have something like:
string temp_line;
getline(file, temp_line);
to just read that first line into a string, and then never use it.
@@PortfolioCourses Another way is to do this before the loop:
getline(file, array[lines]);
The first entry in the array will get overwritten with the first iteration of the loop.
@@airrsongs Thanks for sharing that Armando! 🙂
Hi good info by any chance can you pls explain how to convert 2d array to one d array and print the values which are in 2d using 1d array .. thanks for wonderful videos and it's keeping every one of your subscriber awake and not become lazy in coding even if he's a beginner too ..... Thanks a lot buddy .... We are very happy to support you in youtube like this always and for ever and ever
Thanks Rama! And maybe one day I can do a video on converting a 2D array to a 1D array. We could do that in a couple ways... either by row or column. I'll add this to my list of ideas. :-)
Thank you so much. But i have a problem that I want to read lines of integers how can i store integers and use each of numbers in the lines
I don't have a video on that topic yet. But maybe you could use a 2D array to store the data, and you could read the data into the 2D array then? Like this answer here: stackoverflow.com/questions/36708370/reading-from-txt-file-into-two-dimensional-array-in-c. :-)
I have the same problem here, are you from hcmut?
Hey Portfolio, could you make a video about how to double the size of a dynamically allocated array of integers in c++. I just started learning about HEAP and STACK memory and it's really confusing. Id appreciate it a lot if you considered it. Thanks.
How is the array dynamically allocated? Using malloc and calloc? If it's allocated using malloc or callco this video on realloc() may help you out with that: ruclips.net/video/vr7qCQLrUt8/видео.html. :-)
Is there a way to do this with a line of characters?
I'm not sure what you mean by a line of characters. :-) Does the file have a single line made up of characters separated by spaces? And you want to read the file contents into an array of characters? e.g.
a b c d e f
would give us an array storing the chars 'a', 'b', 'c', 'd', 'e' and 'f'?
I still cant open my file.txt on xcode on macbook m1, there's st wrong...
The issue is likely that the file is not in the current working directory. You could provide an absolute path to the file as part of the C++ program. Or you could change the working directory of the program to be the same as the one your file.txt file is located in. Someone has left some instructions for doing that here: developer.apple.com/forums/thread/679367. :-)
@@PortfolioCourses gosh thanks for your contribution for community, wish you all the best!
@@nguyenlehung4608 You're welcome! 🙂
#include
#include
using namespace std;
#define MAX_Lines 1000
int main()
{
string filename;
ifstream readfile;
string arr[MAX_Lines];
cout
Hmm, if the filename has a space in it, cin won't work, you would want to use getline() in that case: ruclips.net/video/ot2FyuT5xpg/видео.html
@@PortfolioCourses now I have write getline function in place of cin but it giving me error that getline function is overloaded bcz it is also used after while loop
Hmm that's odd Hayat I wouldn't expect that kind of error in this situation. The code for the video is found here on GitHub: github.com/portfoliocourses/cplusplus-example-code/blob/main/read_file_into_array.cpp.
-if(lines == MAX_LINES)-
if(MAX_LINES == lines) 😏
Good idea! ;-)