简体   繁体   中英

Reading a comma delimited file part by part

I have an assignment from school and have to read a comma delimited file and put each value in to a char*.

Ex)

File contains:

5,Justin,19,123-4567,etc..

char * firstValue = 5;

char * secondValue = Justin;

char * thirdValue = 123-4567; etc..

I cannot use std::string since we haven't learned it yet. I am supposed to do it with ifstream or other file streams. I have no idea how to do this

For homework assignments iterating over the char array in a for loop and every time a comma is encountered splitting the string will work (I won't give the code because it is a good exercise to try to work out yourself). But for real world use you should be aware that the CSV format is considerably more complex. Consider multiline entries or entries with an escaped comma and how they break the above solution. I strongly recommend using libcsv for any real world CSV parsing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM