ifstream打开由cin命名的文件,但从数组中提取时不打开.cin和常规字符串定义之间的任何区别
ifstream opens files named by cin but not when pulled from an array. Any difference between cin and a regular string definition?
TL;DR在数组中以字符串形式存储的文件名(使用new(-ifstream不会打开它们(perror返回"No such File or directory"(。交换数组变量并调用用户来命名文件(使用cin(-ifstream打开文件。为什么?如何使数组工作?
须知
- 所有文件都存在于命名方案为run20###的文件夹中,其中
- 所有文件都命名为S20###。ABC,其中###与父目录相同,ABC可以从001-999开始。这些都是可以通过ifstream和getline打开的文本文件(没有.txt扩展名(
我正在编写一个程序,它将从多达150个文件中提取信息。我编写的早期版本让用户输入文件名(使用cin(。ifstream采用存储的名称,每次都能成功打开文件。显然,我不想键入150个文件名,所以程序将所有文件名作为字符串存储在一个数组中,供程序从中提取。然而,当它打开文件(路径正确,文件名和扩展名正确(时,我从perror得到的错误返回"No such file or directory"。如果我只是快速交换变量,使文件名来自cin,文件就会打开。为什么cin有效,而数组版本无效?有什么方法可以让数组工作吗?
在没有数组的地方,我也尝试过类似的方法。相反,在从数组中提取文件的for循环中,每次都会对文件进行命名。
以下是代码(很抱歉有标题,无法正确格式化(:
#include <iostream>
#include <array>
#include <string>
#include <ctime>
#include <cstring>
#include <fstream>
#include <sstream>
using namespace std;
int main() {
//--------------------------Initial setup----------------------------------
cout << "Please give the full name of the folder you would like to open in the /Users/lucas/HPS/TDCData directory" << endl << endl;
string sFolderName;
cin >> sFolderName;
// Create path. I have mine here but you'll have to change it to something you'll
// use if you want to run the code
string sPathName = "/Users/lucas/HPS/TDCData/" + sFolderName;
//----------------Create file name array------------------------------------
// Get naming base from the folder name given
string sFileBase = "S20";
for (int i = 5; i <= sFolderName.length(); i++){
sFileBase = sFileBase + sFolderName[i];
}
//Specify range since different directories have different numbers of files
cout << "Files must be named S20###.ABC" << endl;
cout << "Specify a range for ABC" << endl;
int iFloor;
int iCeiling;
cout << "Floor: " << endl;
cin >> iFloor;
cout << "Ceiling: " << endl;
cin >> iCeiling;
// Define an array to store names and then store them
string *aFiles;
int iFilesSize = iCeiling - iFloor + 1;
aFiles = new string [iFilesSize];
cout << "Array created" << endl;
for (int i = iFloor; i <= iCeiling; i++){
string name = sFileBase;
if (i < 10){
name = name + ".00" + to_string(i);
}
else if (i < 100) {
name = name + ".0" + to_string(i);
}
else {
name = name + '.' + to_string(i);
}
aFiles[i-1] = name;
}
//----------------Open each file in aFiles----------------------
for (int i = 0; i < iFilesSize; i++){
// There are two important lines of code here. The first gets sFileName from
// aFiles. The second gets sFileName from user input using cin (this is commented out).
// Obviously, none of the last section of code is needed for the second line to work.
// The first line does not work for me. The second does.
string sFileName;
//First
sFileName = aFiles[i];
//Second
//cin >> sFileName
string sFullPath = sPathName + "/" + sFileName;
cout << "Searching ... " << sFullPath << endl << endl;
//Open file
ifstream inputFile(sFullPath);
//Check that the file opened
if (! inputFile.is_open()) {
cout << "Error reading" << sFullPath << endl;
perror("Error is: ");
return 0;
}
else {
cout << "File opened successfully..." << aFiles[i] << endl << endl;
}
}
cout << "All files opened..." << endl << endl;
return 0;
}
这里还有一个链接,指向某人可能想要运行的任何测试的一个目录的zip。感谢您的帮助!
看起来像是从索引iFloor
开始填充aFiles
,而从索引0
开始读取aFiles
。
将aFiles[i-1] = name;
更改为aFiles[i-iFloor] = name;
如何
"TL;DR文件名存储为数组中的字符串(使用新的(">
不要这样做。请改用像std::vector<std::string>
这样的动态容器。
"-ifstream不会打开它们(perror返回"No such file or directory"(。">
使用调试器检查实际传递给的内容
ifstream inputFile(sFullPath);
用CCD_ 8。
"通过调用用户来命名文件来交换数组变量(使用cin(-ifstream打开文件。为什么?我如何让数组工作?">
当您尝试使用数组时,不能替换流获取值的行为。
使输入流源透明的最佳方法是简单地使用std::istream
引用,而不关心它是std::cin
还是std::istringstream
引用。
初始化上述std::istringstream
所需的std::string
实例可以例如使用std::ostringstream
来构建,并将str()
属性传递给std::istringstream
构造函数。
- C++中的cin.ignore()函数不适用于整个流
- 在while循环中输入带有std::cin的字符串后,控制台会输出大量胡言乱语
- Problems with std::cin.fail()
- 为什么与常规GCC不同,即使有"学究性错误",MinGW-GCC也能容忍丢失的返回类型
- 由cin中的字符串中未捕获空白引起的分割错误
- 在C++中使用Cin,我如何在1行中输入
- 将 cin 限制为只有一个
- cin >> int 给定一个字符串将 int 赋值为 0
- istream std::cin如何修改自定义istream缓冲区
- C++ 将 CIN 值存储到任何类型的数组中
- 为什么无论你输入什么,这"while(cin.get(str,3))"只运行一次?
- cin 的十进制输入验证?
- Turbo C++ cin() 不能与 gets() 一起使用
- 使用 cin 时接受小数点后的 2 位数字
- 在 std::getline 和 std::cin 期间卡在循环中
- 我无法在Visual Studio代码中使用CIN输入答案,它说输入您的年龄,但它说只读文本编辑器如何解决这个问题?
- 为什么我的两个 cin 语句没有在程序结束时运行?
- C++ cin.ignore() 跳过循环
- CIN是逻辑1还是0?
- ifstream打开由cin命名的文件,但从数组中提取时不打开.cin和常规字符串定义之间的任何区别