I have a text file named myfile.txt
which lists the contents of drive D:\
. In my program, I have a functon which will read myfile.txt
. It will extract filenames from the .txt
extension. I don't know much C++, so can you make it please "simple"? I am confused about the starting position of the substring as how would I know from where it will start.
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
int main(void)
{
system("cls");
string temp;
fstream file;
file.open("D:\\myfile.txt", ios::in);
while( file.good() )
{
file>>temp;
string str2, str3;
size_t pos;
str2 = temp.substr (4,4); // confused with this
pos = temp.find(".txt"); // position of ".txt" in temp
str3 = temp.substr (pos);
cout << str2 << ' ' << str3 << endl;
}
system("pause");
return 0;
}