Search code examples
c++filefilenames

creating files, checking if another one with the same name exists


Code(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;
  }

My problem is following.

Whenever I run this and enter, lets say 53 as for the amount, in the end it'll never create the full amount of files. It's always scaled.

Here's an example.

Defined Amont: 300 -> What I Get: 240

Defined Amount: 20 -> What I get: 15

Defined Amount: 600 -> What I get: 450

Thanks in advance.


Solution

  • Based on the logic of your code, you are creating a file if your ifstream object is not 'good()'. If some files aren't being created, then the error lies here.

    With some digging, you'll find that the constructor for an ifstream object does not take a string, but instead a char *.

    Adding a c_str() to your 'finalPath' variable should take care of this issue.

    Some things to note:

    • You've forgotten to include fstream and iostream.
    • When digging into problems like this, don't use random numbers as your first test case. It was easier for me to replicate your issue by just trying to create files in numerical order.
    • Also don't forget 'close()' your ifstreams!

    My adaptation of the code:

    #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;
    }