How to use C++ to read csv files | Read a csv file with C++ using the fstream library

แชร์
ฝัง
  • เผยแพร่เมื่อ 11 ธ.ค. 2024

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

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

    How can i read this kind of file when it has the '
    ' at the end of the line, i tried to do getline but somehow it skip that and return error.

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

    can you do a tutorial about how to write/ edit text from a specific column in a file. csv?

  • @user-ot2mn4wp8z
    @user-ot2mn4wp8z 10 หลายเดือนก่อน

    newbie here why is it necessary to return 1 after the if statement

    • @MirukuVevo
      @MirukuVevo 9 หลายเดือนก่อน

      Sorry for late responce: Since the code is within int main(), its looking to be returned an integer. Return 0 generally is used if the code is successful. If there are any issues with the code you can use Return # with any other number to see where the code stopped due to an error and quickly find where the error is.

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

    How could I read a normal txt file with c++ that was also written with some sort of standard (like say you have three values in each row of the txt file separated by a space, not with commas and " " like in csv)? And would it be possible to create an output txt file?

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

      @Ian, Thanks for your comment and question. Your question has two parts. The first part of it is about how to read a normal text file or .txt, and the second one is about how to write to a .txt file. I will address the first one here and the other one will be part of my next video series.
      I would like to begin by saying that a csv file is not always separated by commas (I mentioned this before in one of my previous videos on csv the file topics). It can also be separated by other characters. However, to keep to the name "comma separated values" csv, it is best to use a comma for that purpose.
      The piece of code in this video can read a .txt file. We just need to make some minor changes to it. Read about it below. For example,
      1) Create a file named countryinfo and save it as countryinfo.txt in the same cppandcsv folder.
      2) Enter the details below for country, capital and currency separate the text with a space and not a comma.
      United Kingdom London Pound Sterling
      Germany Berlin Euro
      Nigeria Abuja Naira
      United States of America Washington DC Dollar
      France Paris Euro
      Japan Tokyo Yen
      Cuba Havana Peso
      India New Delhi Rupee
      South Africa Cape Town Rand
      China Beijing Yuan
      4) Now, let's make some changes to the c++ file. In this case, the students.cpp file in the same folder.
      5) Change the line string myFilePath = "undergrads.csv"; TO string myFilePath = "countryinfo.txt";
      6) Change the line getline(allStudents, records, ','); To getline(allStudents, records);
      7) Compile and run your code. It should print out the content of the .txt file without commas.
      Hope that answers the first part of your question.

    • @ianthehunter3532
      @ianthehunter3532 2 ปีที่แล้ว

      @@zebracode thank you for the detailed answer!

  • @jrippee05
    @jrippee05 2 ปีที่แล้ว

    this is good stuff
    thanks for posting
    where's the clean up video at?

    • @zebracode
      @zebracode  2 ปีที่แล้ว

      That video will be uploaded soon.

    • @jrippee05
      @jrippee05 2 ปีที่แล้ว

      @@zebracode thank you...this is one of the best videos. Do you know how to work structures and vectors - that is putting the data into vectors from csv