简体   繁体   中英

How to run a c++ program multiple times with different input files?

I'm new to C++ and writing my master thesis and would really appreciate any help I can get!

I have a program that reads a txt file, then does a bunch of calculations, and returns a new txt file. The thing is that I want to run this program for 100+ different input files. Now I have to change the name of the input file in the code, but I would like to have it run for all the input files in my folder by itself.

I am using Visual Studio, but with little C++ experience.

Thanks :)

使用bash,您可以使用以下命令运行它们:

$ for file in /Data/*.txt; do /path/your_program $file; done

See this snippet. Since you are using MSCV, you need to enable MFC in configuration for this console application. Also add #include "afx.h" in #include "stdafx.h" where CFileFind is defined. PopulateFromFolder() should auto load the files into the vector files .

#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>
#include <vector>   

using namespace std;

void PopulateFromFolder(string path, vector<string>& files)
{
    CFileFind   finder;
    CString fileName;
    fileName.Format(L"%s*.*", CString(path.c_str()));
    BOOL bOk = finder.FindFile(fileName);
    while (bOk)
    {
        bOk = finder.FindNextFile();

        if (finder.IsDots())
        {
            continue;
        }
        if (!finder.IsDirectory())
        {
            CString strFileName = finder.GetFileName();
            files.push_back(CStringA(strFileName).GetString());
        }
    }
    finder.Close();
}

int main()
{
    vector<string> files;
    string path = "D:\\MyFolder\\";
    PopulateFromFolder(path, files);

    auto a = path + files[0];
    int i = 0;
    while (i< files.size()-1)
    {
        cout << "processing " << files[i + 1] << endl;
        ifstream fs(path+files[i++]);
        if (fs.is_open())
        {
            //do something
        }
        fs.close();
    }
    return 0;
}

Output:

在此处输入图片说明

You can define format for your input files names and put then into some directory. For example,

Input1.txt
Input2.txt
...
Input111.txt

Then use some kind of for loop:

for(int i = 1; i <= 111; ++i)
{
    ifstream file("Input" + std::to_string(i) + ".txt");
    if (file.is_open())
        Operate(file);
}

If you don't know the exact number of files, you can check whether the file was openen by is_open() method. This way files with some numbers can be absent. You just loop for some max possible input file id.

This was a solution which doesn't require any dependencies. But if you don't mind it, you actually may consider Boost.Filesystem. Here is an example .

You can try to use std::experimental::filesystem ( http://en.cppreference.com/w/cpp/experimental/fs ). I guess that directory_iterator from this library can be useful for you - it allows you to iterate over all files in a given directory. Have a look at the example provided in the documentation: http://en.cppreference.com/w/cpp/experimental/fs/directory_iterator . However, you have to make sure that you are compiling your code with a new standard (C++ 17).

Another way is to make for example a separate file containing a list of the names of all files that you want to work on. Then, you can read this list and for every file do what you need.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM