【发布时间】:2011-04-04 11:42:58
【问题描述】:
我需要读取一个文件,其中包含其他文件的路径、类型和有关它们的其他数据。 该文件看起来像,
LIST OF SUB DIRECTORIES:
Advanced System Optimizer 3
ashar wedding and home pics
components
Documents and Settings
khurram bhai
media
new songs
Office10
Osama
Program Files
RECYCLER
res
Stationery
System Volume Information
Templates
WINDOWS
LIST OF FILES:
.docx 74421
b.com 135168
ChromeSetup.exe 567648
Full & final.CPP 25884
hgfhfh.jpg 8837
hiberfil.sys 267964416
myfile.txt.txt 0
pagefile.sys 402653184
Shortcut to 3? Floppy (A).lnk 129
Thumbs.db 9216
vcsetup.exe 2728440
wlsetup-web.exe 1247056
我只需要提取文件的路径名并将它们保存在一个数组中,但我坚持下去。这是我的代码,
// read a file into memory
#include <iostream>
#include <fstream>
using namespace std;
int main () {
int length;
char str[600];
ifstream is;
is.open ("test.txt", ios::binary );
// get length of file:
is.seekg (0, ios::end);
length = is.tellg();
is.seekg (0, ios::beg);
// read data as a block:
is.read (str,length);
//**find the path of txt files in the file and save it in an array...Stuck here**
is.close();
return 0;
}
我很困惑下一步该怎么做。即使我使用 strstr() 来查找 .txt ,我如何获得它的整个路径?
【问题讨论】:
-
你会总是在每一行的末尾都有
Text file吗?您是否保证文件名不会嵌入换行符?文件名是否“安全”——只有字母数字和一些简单的符号?或者文件名可以有 ascii 控制字符吗?您必须找出一些机制来分解可能不明确的数据,并且尽可能多地了解会有所帮助。 -
现在这个文件布局不是很直观或远程帮助,包含所有这些文件的根目录是什么?你到底想要做什么现在离我的想法很远,也许有更好的解释?