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!

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

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

    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"

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

      In this video, I'm definitely assuming that the user will enter a filename with no spaces. 🙂

  • @YourNameHere1000
    @YourNameHere1000 9 месяцев назад

    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.

  • @taimoorsajjad2150
    @taimoorsajjad2150 7 месяцев назад +1

    really helped me
    apreciated bro ❣❣❣❣

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

    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.

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

      You could use a sorting algorithm, or use the sort() function that's built-in to C++: ruclips.net/video/hP7aIrTRqQw/видео.html. 🙂

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

    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

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

      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.

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

      @@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.

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

      @@airrsongs Thanks for sharing that Armando! 🙂

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

    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

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

      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. :-)

  • @TrungPham-rr3jb
    @TrungPham-rr3jb Год назад

    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

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

      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. :-)

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

      I have the same problem here, are you from hcmut?

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

    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.

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

      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. :-)

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

    Is there a way to do this with a line of characters?

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

      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'?

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

    I still cant open my file.txt on xcode on macbook m1, there's st wrong...

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

      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. :-)

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

      @@PortfolioCourses gosh thanks for your contribution for community, wish you all the best!

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

      @@nguyenlehung4608 You're welcome! 🙂

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

    #include
    #include
    using namespace std;
    #define MAX_Lines 1000
    int main()
    {
    string filename;
    ifstream readfile;
    string arr[MAX_Lines];
    cout

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

      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

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

      @@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

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

      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.

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

    -if(lines == MAX_LINES)-
    if(MAX_LINES == lines) 😏