如何从文本文件中读取文件名列表并用C++打开它们
How to read a list of filenames from a text file and open them in C++?
我有一个存储在文本文件中的文件列表。我逐行读取文件,并将它们存储在字符串数组中。文件列表如下所示:
04_02_1310.csv
04_03_1350.csv
04_04_0421.csv
04_05_0447.csv
等等。让我们调用我的字符串数组
filelist[i]
假设我正在尝试打开列表中的第一个文件:
inputFile.open(filelist[0].c_str()); // This cannot open file
无法打开该文件。如果我把文件名放在引号里,一切都会很好:
inputFile.open("04_02_1310.csv"); // This works perfectly
如果我打印filelist[i]的内容,那么它也可以正常工作:
cout << filelist[0] << endl; // This outputs 04_02_1310.csv on screen.
有人能告诉我上面的方法出了什么问题吗?在过去的两天里,这让我抓狂,我即将手动输入所有内容,只为完成它(一个接一个地输入100多个文件)。
我也愿意接受任何其他方式来完成这项简单的任务。
谢谢!!!
编辑:如果你想看看它是如何实现的,我会添加代码的相关部分:
#include <cstdlib>
#include <iostream>
#include <time.h>
#include <string>
#include <sstream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <iterator>
using namespace std;
//Declarations for I/O files
ifstream inputFile;
//Declare other variables (forgot to add these in my previous EDIT, sorry)
int number_of_files;
string line;
string *filelist = NULL;
//Open file list and count number of files
inputFile.clear();
inputFile.open("filelist.txt", ios::in);
//exit and prompt error message if file could not be opened
if (!inputFile){
cerr << "File list could not be opened" << endl;
exit(1);
}// end if
// count number of lines in the data file and prompt on screen
number_of_files = 0;
while (getline(inputFile, line))
number_of_files++;
cout << "Number of files to be analyzed: " << number_of_files << endl;
filelist = new string[number_of_files];
inputFile.close();
//Re-open file list and store filenames in a string array
inputFile.clear();
inputFile.open("filelist.txt", ios::in);
//exit and prompt error message if file could not be opened
if (!inputFile){
cerr << "File list could not be opened" << endl;
exit(1);
}// end if
// store filenames
i = 0;
while (getline(inputFile, line)){
filelist[i] = line;
//cout << filelist[i] << endl;
i = i + 1;
}
inputFile.close();
//open first file in the list, I deleted the loop to focus on the first element for now
inputFile.clear();
inputFile.open(filelist[0].c_str(), ios::in);
//exit and prompt error message if file could not be opened
if (!inputFile){
cerr << "Data file could not be opened" << endl;
exit(1);
}// end if
输出为:
Data file could not be opened
再次感谢!
如果字符串中仍然存在文本文件中的'\n'字符(或EOF,'\0'),则应尝试检查字符串是否"干净"。
我也愿意接受任何其他方式来完成这项简单的任务。
#include <cstdlib>
#include <iostream>
#include <time.h>
#include <string>
#include <sstream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <iterator>
using namespace std;
int main () {
std::ifstream inputFile("filelist.txt");
std::vector<std::string> fileList;
std::string line;
if(!inputFile) {
std::cerr << "File list could not be openedn";
return 1;
}
while(std::getline(inputFile, line)) {
fileList.push_back(line);
}
std::cout << "Number of files to be analyzed: " << fileList.size() << "n";
std::vector<std::string>::const_iterator it(fileList.begin());
std::vector<std::string>::const_iterator end(fileList.end());
for(;it != end;++it) {
std::ifstream inputTxt(it->c_str());
if(!inputTxt) {
std::cerr << "Data file could not be opened:" << *it << "n";
return 1;
}
while(std::getline(inputTxt, line)) {
std::cout << line << "n";
}
}
}
我建议使用更"C++风格"的方法,尽管与Blood:发布的优雅解决方案不同
int main()
{
std::vector<std::string> fileList;
// ...
std::ifstream inputFile("filelist.txt");
// ...
std::string line;
while(inputFile >> line)
filelist.push_back(line);
inputFile.close();
// ...
for(size_t t = 0; t < filelist.size(); t++)
{
std::ifstream dataFile(filelist[t].c_str());
// ...
dataFile.close();
}
}
相关文章:
- Pybind11:将元组列表从Python传递到C++
- 从链接列表c++中删除一个项目
- 如何(从固定列表中)选择一个数字序列,该序列将与目标数字相加
- C++如何通过用户输入删除列表元素
- 读取文件的最后一行并输入到链接列表时出错
- 复制列表初始化的隐式转换的等级是多少
- LNK2038、MSVS2017 MAGMA的原因列表
- 不能在初始值设定项列表中将非常量表达式从类型 'int' 缩小到'unsigned long long'
- 没有为自己的结构调用列表推回方法
- 使用简单类型列表实现的指数编译时间.为什么
- 一对向量构造函数:初始值设定项列表与显式构造
- 标准是否使用多余的大括号(例如 T{{{10}}})定义列表初始化?
- 通过for循环使用用户输入填充列表
- C++:如何使函数只返回作为列表一部分的字符串
- 概念中的cv限定符需要表达式参数列表
- 下面是我为检测链接列表中的循环而制作的代码
- 建议在运行时将带有类实例的列表从c++导入qml
- 如何维护资源管理器项目视图中当前可见的项目列表
- 在卡萨布兰卡形成编码参数的列表
- 在没有参数列表的情况下使用模板名称"Event"无效,模板问题