我有一個名爲myfile.txt
的文本文件,它列出了驅動器D:\
的內容。在我的程序中,我有一個函數,它將讀取myfile.txt
。它將從.txt
擴展中提取文件名。我不太瞭解C++,所以你可以讓它「簡單」嗎?我對子字符串的起始位置感到困惑,因爲我將如何知道它將從何處開始。如何從文本文件中提取帶.txt擴展名的文件名?
#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;
}
另一點 - 如果你在unix系統上,你甚至可以在你進入你的程序之前做過濾。即'find/-type f -iname'* .txt「> textFiles.txt' – I82Much 2011-04-07 18:40:47
或者,如果你已經有了'grep'txt $'< files.txt > textfiles.txt'的列表。 – 2011-04-07 18:59:34