简体   繁体   中英

reading in specific columns from a text file

I'm working with someone else's code and don't have a lot of c++ experience. I need to either edit the code to read in columns 2,3,4 and not 1 or 5, or change it to read CSV files instead of txt files. Here's the current read in code:

fin.getline(temp,200);

cout << temp << end1;

sscanf(temp, "%f %f %f %*f\n",&temp1,&temp2,&temp3);

This code works to read in older txt files as they didn't have the column one I'm trying to exclude. Is it something simple like adding another %* ?

Thanks

The easiest and still reasonable way is to change your line to:

sscanf(temp, "%f %f %f %f %*f\n",&unused, &temp1,&temp2,&temp3);

First col value goes to 'unused' and all after 4th is skipped.

Also you may need to increase size od 'temp'.

Adding %*f, at the beginning should also do the job, but I hadn't use it for some time, and don't have possibility to check now, but both solutions looks ok.

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