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?

本文关键字:cin 常规 字符串 之间 区别 任何 提取 定义 文件 数组 ifstream      更新时间:2023-10-16

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构造函数。