简体   繁体   中英

How do I read Files with similar names in c++

total1_0831.txt

total2_0831.txt

total3_0831.txt

total1_0901.txt

total2_0901.txt

I want to load 3 0831 text files

my code is

fstream myTxT;
myTxT.open("total1_0831.txt");

fstream myTxT;
myTxT.open("total2_0831.txt");

fstream myTxT;
myTxT.open("total3_0831.txt");

It was ineffective, so I wrote the code in a new way.

vector<fstream> myTxT;
myTxT.open("total%d_0831.txt") // i think this part is error, but i don't know how to fix it

This code wasn't work anymore.

Any advice would be appreciated

You should have a base string, to which you then append the number of the file to read as a string, and finally append the extension.

Once you have the path string constructed, you can load the file.

All this logic should go in a loop, and you're done!

Having said that, it probably is an overkill for just 3 files, but it's a nice method when dealing with multiple files that share a logic in their naming.

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