-2

I have a function:

void ReadInput(const char * name)
{
    ifstream file(name);
    cout << "read file " << name << endl;
    size_t A0, A1, A2;
    file >> A0 >> A1 >> A2;
}

Now i want to read two files: INPUT1.txt and INPUT2.txt in a loop, such as:

int main ()
{
    for (int i = 1; i < 3; i++){
        ReadInput(INPUT$i);
    }
    return 0;    
}

The question is how do i define the loop correctly.

Thanks for your time in advance.

Here is the whole code:

#include <iostream>
#include <string>



using namespace std;


void ReadInput(const string& _name){
    ifstream file(_name);
    size_t A0, A1, A2;
    file >> A0 >> A1 >> A2;
}


int main ()
{


    for (int i = 1; i < 3; ++i) {
        string file_name = "INPUT" + to_string(i) + ".txt";
        ReadInput(file_name);
    }

    return 0;
}

OK, all good, now i can compile in c++98 by converting string to const char and stringstream instead of to_string. My goal was to run an automated program with input files all in the same directory. The suggestions about possible duplicate of the question does not achieve that as i have to pass on the input file number as i execute, as i understand it, which is impractical for 3,000 something files.

user147813
  • 67
  • 2
  • 8

3 Answers3

1

If you have a number of files (up to n_max+1), with names of the form "INPUTn.txt", where a loop would be warranted, then the following would be a potential solution:

for (int i = 1; i < n_max; ++i) {
    std::string file_name = "INPUT" + std::to_string(i) + ".txt";
    ReadInput(file_name);
}

This requires altering ReadInput to:

void ReadInput(const std::string& _name);

instead of using const char*.

If you don't have C++11 access, then use this in-place of std::to_string:

#include <sstream>
//...
template<typename T> std::string to_string(const T& x) {
    return static_cast<std::ostringstream&>((std::ostringstream() << std::dec << x)).str();
}
sjrowlinson
  • 3,297
  • 1
  • 18
  • 35
  • Thanks. However, when compiling i get two errors: 'name' was not declared in this scope and 'to_string' was not declared in this scope. – user147813 May 15 '16 at 22:04
  • @user147813 You probably need to `#include ` – sjrowlinson May 15 '16 at 22:10
  • I added it but the same error still persist. I added the code to my question. What am i doing wrong? – user147813 May 15 '16 at 22:45
  • 1
    @user147813 There's an underscore in argument `_name`, whereas there is no underscore in the `name` within the function body - either change argument to `name` or use `_name` in body of function – sjrowlinson May 15 '16 at 23:08
1

Corrected a few things in your code below. Keep in mind, for std::to_string to work you need to at least compile with the flag -std=c++11

#include <iostream>
#include <string>
#include <fstream> // you need this for std::ifstream

using namespace std;

void ReadInput(const string& _name){
    // ifstream file(name); should be '_name'
    ifstream file(_name);
    // size_t A0, A1, A2 - what if your file contains characters?
    string A0, A1, A2; 
    file >> A0 >> A1 >> A2;

    // output
    cout << "File: " << _name << '\n';
    cout << A0 << " " << A1 << " " << A2 << '\n';
}

int main ()
{
    for (int i = 1; i < 3; ++i) {
        string file_name = "INPUT" + to_string(i) + ".txt";
        ReadInput(file_name);
    }

    return 0;
}

Or if the file is longer, you may want to read using std::getline

void ReadInput(const string& _name){
    ifstream file(_name);

    string line;
    cout << "File: " << _name << '\n';
    while (getline(file, line)) {
        cout << line << '\n';
    }
}
Andreas DM
  • 10,685
  • 6
  • 35
  • 62
  • Thanks Andreas DM but i have trouble getting the right answer. Say i have a file named INPUT1.txt with 3 values: 50 50 50. The code as is doesn't real 50 50 50 but gives me 6422476 6422296 435480. Any ideas why? – user147813 May 16 '16 at 02:03
  • @user147813 works correctly for me. I am not sure what you do differently – Andreas DM May 16 '16 at 02:19
  • how would i can I do away with to_string in c++98? I know i have to use stringstream but not sure exactly how. – user147813 May 16 '16 at 12:31
  • I can compile and run with no problem. But on a cluster, i can't compile. This is my g++ version:g++ (GCC) 4.4.7 20120313 (Red Hat 4.4.7-4) Copyright (C) 2010 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. -std=c++11 or -std=c++0x results in recognized command line. – user147813 May 16 '16 at 13:03
  • @user147813 Check out the update to my answer for an alternative to `to_string` in pre-c++ 11. – sjrowlinson May 16 '16 at 13:43
0

I guess what you want is:

int main (int argc, char* argv[])
{
    using namespace std;
    for (int i = 1; i < argc; ++i) {
        string file_name = string(argv[i]) + to_string(i) + ".txt";
        ReadInput(file_name);
    }

    return 0;
}

For further information see: https://stackoverflow.com/a/3024202/3537677

Community
  • 1
  • 1
Superlokkus
  • 4,731
  • 1
  • 25
  • 57