如何从文本文件中读取文件名列表并在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
等等。让我们调用我的字符串数组
$ b
filelist [i]
假设我正在尝试打开列表中的第一个文件:
inputFile 。开(文件列表[0] .c_str()); //不能打开文件
文件无法打开。如果我把文件的名字放在引号中,一切正常:
inputFile.open(04_02_1310.csv);如果我打印filelist [i]的内容,那么它工作正常,因为它的作品完美的
well:
cout<< filelist [0]<< ENDL; //在屏幕上输出04_02_1310.csv。
有人可以告诉我上面的方法有什么问题吗?在过去的两天里,这让我疯狂,而且我正在手动输入所有内容(100个以上的文件)。
我是
谢谢!!!
编辑:我是这样简单的任务添加代码的相关部分,如果你想看看它是如何实现的:
#include< cstdlib>
#include< iostream>
#include< time.h>
#include< string>
#include< sstream>
#include< fstream>
#include< vector>
#include< algorithm>
#include< iterator>
使用namespace std;
// I / O文件的声明
ifstream inputFile;
//声明其他变量(忘了在我以前的编辑中添加这些变量,对不起)
int number_of_files;
字符串行;
string * filelist = NULL;
//打开文件列表并计算文件数量
inputFile.clear();
inputFile.open(filelist.txt,ios :: in);
//如果文件无法打开,退出并提示错误消息
if(!inputFile){
cerr<< 文件列表无法打开<< ENDL;
exit(1);
} // end if
//计算数据文件中的行数和屏幕提示
number_of_files = 0;
while(getline(inputFile,line))
number_of_files ++;
cout<< 要分析的文件的数量:<< number_of_files<< ENDL;
filelist = new string [number_of_files];
inputFile.close();
//重新打开文件列表并将文件名保存在字符串数组中
inputFile.clear();
inputFile.open(filelist.txt,ios :: in);
//如果文件无法打开,退出并提示错误消息
if(!inputFile){
cerr<< 文件列表无法打开<< ENDL;
exit(1);
} //结束如果
//存储文件名
i = 0;
while(getline(inputFile,line)){
filelist [i] = line;
// cout<< filelist [i]<< ENDL;
i = i + 1;
}
inputFile.close();
//打开列表中的第一个文件,我删除了这个循环,现在把注意力集中在第一个元素
inputFile.clear();
inputFile.open(filelist [0] .c_str(),ios :: in);
//如果文件无法打开,退出并提示错误消息
if(!inputFile){
cerr<< 数据文件无法打开<< ENDL;
exit(1);
} // end if
输出结果是:
无法打开数据文件
再次感谢!
可能还有'\ n'char(或EOF,'\ 0'),你应该尝试检查字符串是否为干净。
I have a list of files stored in a text file. I read the file line by line and store them in a string array. The file list looks like this:
04_02_1310.csv
04_03_1350.csv
04_04_0421.csv
04_05_0447.csv
and so on. Let's call my string array
filelist[i]
Assuming I am trying the open the first file in the list:
inputFile.open(filelist[0].c_str()); // This cannot open file
the file cannot be opened. If i place the file name in quotation marks, everything works out fine:
inputFile.open("04_02_1310.csv"); // This works perfectly
if i print the contents of filelist[i], then it works fine as well:
cout << filelist[0] << endl; // This outputs 04_02_1310.csv on screen.
Can somebody tell me what is wrong with the approach above? This is driving me crazy for the last 2 days, and I am about the enter everything manually just to get it done (100+ files one after another).
I am also open to any other way to do this simple task.
Thanks!!!
EDIT: I am adding a relevant portion of the code if you would like to see how it is implemented:
#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
The output is:
Data file could not be opened
Thanks again!
It is possible that there still is the '\n' char (or EOF,'\0' ) from your textfile in that string, you should try checking if the strings are "clean".
这篇关于如何从文本文件中读取文件名列表并在C ++中打开它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!