Reading from 10 input files?



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
bool Open_InputFile (ifstream &fin)
{
    string file_name;
    cout << "Enter Input File Name: ";
    getline (cin, file_name);
    fin.open(file_name.c_str());
    if (fin.fail())
        return false;
    else
        return true;
}

int Read_WaitTime (ifstream &fin, string bank_name[], double w_time[], int size, int banksize)
{
    int i = 0;
    
    if (Open_InputFile(fin))
    {
        cout << "File Opened\n";
        Read_BankName(fin, bank_name, banksize);
        fin >> w_time [i];
        while (!fin.eof())
        {
            i++;
            if (i == size)
                break;
            fin >> w_time [i];
        }
        fin.close();
    }
    else
    {
        cout << "\nBAD FILE NAME!\n " << endl;
        return 0;
    }
    return i;
}

 int Read_BankName (ifstream &fin, string bank_name[], int banksize)
 {
     int k = 0;
     getline (fin, bank_name[k]);
     
     return k;
 }


I got confused on how to actually open 10 files? since in my code it could only read 1 file. Thank you.
Last edited on
before writing your code please use
[ c o d e ]
tag w/o spaces and while ending code use
[ / c o d e ]
w/o spaces. So its easily understandable and people will try to read and help you.
Thanks, I edited it.
Assuming your files are in some order, you can use a loop with following opening parameters

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include<iostream>
#include<cstdlib>
#include<fstream>
#include<string>
using namespace std;

int main()
{
ofstream out;

int i=0;

for(i=0;i<10;i++)
{
out.open("out_"+ to_string(i) +"_.txt");
out<<i;
out.close();
}

}
I only include
#include<iostream>
#include<fstream>
#include<string>

what is to_string(i) mean? could you explain a little bit.
Also, actually is not CREATING A FILE instead I supposed to READ 10 input text files given.
Thank you for your help


to_string(i) converts numerical value 'i' to string.
This function is added to <string> under C++11.
You must use
g++ filename.cpp -std=c++0x

to compile.

And this is just an example, you can set it up for your own use.

You can see more details here.
http://en.cppreference.com/w/cpp/string/basic_string/to_string
Last edited on
Topic archived. No new replies allowed.