简体   繁体   中英

C++ read from formatted .txt file

I need to read in from a comma-separated .txt file where each line looks something this:

1234,0987,Bob,23.45

(ie int,int,string,double)

using the following setup code:

fstream myFile;
myFile.open("textfile.txt" , ios::in);
if (myFile.is_open()) {
    //read in characters as appropriate type until ','
}

I have tried using

myFile >> int1 ......

but I was unsure how I should deal with the commas; they may get filtered out when reading in integers, but would that work when I get to the string?

one of my classmates suggested stringstream, but i've found the documentation on cplusplus.com to be over my head.

You might want to try out the std::getline function:

istream& getline ( istream& is, string& str, char delim );
istream& getline ( istream& is, string& str );

(From http://www.cplusplus.com/reference/string/getline/ )

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