【问题标题】:creating files, checking if another one with the same name exists创建文件,检查是否存在另一个同名文件
【发布时间】:2014-08-16 14:04:31
【问题描述】:

代码(main.cpp)(C++):

  #include <string>
  #include <sstream>
  #include <stdio.h>
  #include <stdlib.h>
  #include <ctime>


  //general vars
  std::ofstream ofs;
  std::ifstream ifs;
  std::stringstream ss;

  //spamFiles vars
  std::string defPath;
  int defAmt;

  void spamFiles(std::string paramPath);

  int main(int argc, const char * argv[])
  {
      srand(time_t(NULL));
      std::cout << "Enter the amount of files: ";
      std::cin >> ::defAmt;

      std::cout << "Now enter the target path: ";
      std::cin >> ::defPath;
      ::spamFiles(::defPath);
       std::cout << defAmt << " files were created." << std::endl;
      return 0;
  }

  void spamFiles (std::string paramPath){
//system("open -a Terminal .");
for(int i = 0; i < ::defAmt; i++){

    std::string tempS;
    int ranNum = rand() % 501;
    ss << ranNum;
    std::string ssResult = ss.str();
    std::string finalPath = ::defPath + ssResult + ".txt";
    ifs.open(finalPath);
    if(ifs.good()){
    finalPath += "dupe.txt";
        while(ifs.good()){
        finalPath += "dupe.txt";
        ifs.open(finalPath);
    }
    }
    ofs.open(finalPath);
    ofs << "";
    ofs.close();
    ss.str(std::string());
      }
      return;
  }

我的问题如下。

每当我运行并输入时,假设数量为 53,最终它永远不会创建完整数量的文件。它总是按比例缩放的。

这是一个例子。

定义的 Amont:300 -> 我得到的:240

定义数量:20 -> 我得到的:15

定义数量:600 -> 我得到的:450

提前致谢。

【问题讨论】:

  • 证明它正在发生。重现问题不需要这些文件内容。用std::cout 或其他东西进行演示。即写一个TESTCASE

标签: c++ file filenames


【解决方案1】:

根据您的代码逻辑,如果您的 ifstream 对象不是“good()”,则您正在创建一个文件。如果某些文件没有被创建,那么错误就在这里。

通过一些挖掘,您会发现 ifstream 对象的 constructor 不采用字符串,而是采用 char *。

将 c_str() 添加到“finalPath”变量应该可以解决这个问题。

注意事项:

  • 您忘记包含 fstream 和 iostream。
  • 在深入研究此类问题时,不要使用随机数作为您的第一个测试用例。通过尝试按数字顺序创建文件,我更容易复制您的问题。
  • 也不要忘记 'close()' 你的 ifstreams!

我对代码的改编:

#include <string>
#include <sstream>
#include <stdio.h>
#include <stdlib.h>
#include <ctime>

#include <fstream>
#include <iostream>

//general vars
std::ofstream ofs;
std::ifstream ifs;
std::stringstream ss;

//spamFiles vars
std::string defPath;
int defAmt;

void spamFiles(std::string paramPath);

int main(int argc, const char * argv[])
{
    srand(time_t(NULL));
    std::cout << "Enter the amount of files: ";
    std::cin >> ::defAmt;

    std::cout << "Now enter the target path: ";
    std::cin >> ::defPath;
    ::spamFiles(::defPath);
    std::cout << defAmt << " files were created." << std::endl;
    return 0;
}

void spamFiles (std::string paramPath){
    //system("open -a Terminal .");

    for(int i = 0; i < ::defAmt; i++){     
        std::string tempS;
        int ranNum = rand() % 501;
        ss << ranNum;
        std::string ssResult = ss.str();
        std::string finalPath = ::defPath + ssResult + ".txt";
        ifs.open(finalPath.c_str());

        while(ifs.good()){
            finalPath += "dupe.txt";
            ifs.open(finalPath.c_str());
        }
        ifs.close();
        std::cout << finalPath << std::endl;

        ofs.open(finalPath.c_str());
        ofs << "";
        ofs.close();

        ss.str(std::string());
    }
    return;
}

【讨论】:

  • 我正在使用 (ifs.good()) 因为如果找不到文件,它将不会返回 true。因此,我正在检查文件是否存在。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-18
  • 1970-01-01
  • 1970-01-01
  • 2013-04-20
  • 2018-08-30
  • 2020-04-30
相关资源
最近更新 更多