How di I use I/O redirection to load the input into an array ?

% a.out < inputfile

That's what I found. I don't know if that's all you need or if there's more..

Recommended Answers

All 7 Replies

Well depending on what you think when you're talking about input redirection. This can be either putting the input of a file into an array, or putting the input given from the keyboard into an array?
You got that

% a.out < inputfile

off this site:
http://www.cs.bu.edu/teaching/cs111/spring-2000/file-io/
but did you read the whole page? It exaplins really well.

No, it dosen't explain it well in my opinion. Input from a file.

Like:

Name < input_file

There are plenty examples on this forum on file input redirection:

#include <fstream>
#include <string>
#include <vector>
#include <iostream>
using namespace std;

int main(){
    string filename;
    cout<<"Filename: ";
    cin>>filename;
    vector<string> file_input;
    ifstream fin(filename.c_str(), ios::in);
    if (fin.is_open()){
        string line;
        while(getline(fin, line)){
            file_input.push_back(line);
        }
    }
    else cout<<"Invalid filen.\n";
    for (int i=0;i<(int)file_input.size();i++){
        cout<<"Line ["<<i<<"] from the file is: "<<file_input[i]<<endl;
    }
    return (0);
}

Here's a quick example of how you can store the content of a file into a vector (stl vecor).

Name < input_file

that might be a pseudocode language...

Ok, thanks.

How do I split the input, though. Like, how do I put the words into the string array and the numbers into the other one? Do I have somehow loop the input before it goes intot the array or is there any other way?

Depending on your arrangement in the file. If for example you have every character till the comma an assigned number, you can tokenize the line. Say you have a text file like this:

1, blablabla
2, blablabla
3, blablabla
4, blablabla
5, blablabla

the corresponding token would be like this:

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

int main(){
    cout<<"Filename: ";
    string filename;
    cin>>filename;
    vector<int> numbers;
    vector<string> lines;
    ifstream fin(filename, ios::in);
    if (fin.is_open()){
        string line, splitline;
        int number;
        while (getline(fin, line)){
            istringstream token(line);
            getline(token, splitline, ',');
            istringstream toInt(splitline);
            toInt>>number;
            numbers.push_back(number);
            geline(token, splitline);
            lines.push_back(splitline);
        }
    }
    else{
        cout<<"Invalid file.\n";
    }       
    return (0);
}

and you'll get a vector<int> with only the numbers, and the rest of the line in the lines vector.

Er, I don't think that any of these examples do what you want them to do. If you take Lucaci Andrew's code for example. If you have a file in the format shown in the example, then the program will try to open a file called "1," and then fail, since you almost certainly don't have a file with that name!

Basically, you can think of the < as a way of getting a program that would normally take its input from std::cin to get that input from a file instead. So

a.out < test.txt

will take the contents of test.txt and try and stuff it into any offers for user input from a.out. If test.txt contains some complicated content, then a.out would have to have equivalently complex set of requests from the user. This type of input can be related to pipes: |. So that the above is equivalent to:

cat test.txt | a.out

Now, to read in a whole array like this, you'd have to have a series of calls to std::cin, each of which gets a value from the user and puts it into an array. So something like:

std::vector< int > v;
int n;
std::string input;
while ( true )
{
    // Read in a number from the user
    std::cin >> input;

    if ( input == "quit" || input.empty() )
        break;  // Get out if the file says to quit, or you find an empty line

    // Convert the string to a number
    std::stringstream ss;
    ss << input;
    ss >> n;

    // Add the number to the vector
    v.push_back( n );
}

Note that there's no mention of asking for filenames or anything in this code. The program doesn't know about files, that's what we're going to use < for. Then you'd have to make a file that looks something like:

12
34
3
62
95
quit

You can run the program on its own and type numbers into the command line and they will all be put into the vector. Or, you can run it using input redirection and a file as above:

a.out < arrayFile.dat

What you do with the stuff that's in the file is up to you. I hope that helps :)

Er, I don't think that any of these examples do what you want them to do. If you take Lucaci Andrew's code for example. If you have a file in the format shown in the example, then the program will try to open a file called "1," and then fail, since you almost certainly don't have a file with that name!

Not quite... C++ will try to open the file inserted by the user. But, I will like to point out two mistakes from the second example:
line14:

ifstream fin(filename, ios::in);

it's

 ifstream fin(filename.c_str(), ios::in);

and line24:

geline(token, splitline);

it's

getline(token, splitline);

Here's the corrected version:

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

int main(){
    cout<<"Filename: ";
    string filename;
    cin>>filename;
    vector<int> numbers;
    vector<string> lines;
    ifstream fin(filename.c_str(), ios::in);
    if (fin.is_open()){
        string line, splitline;
        int number;
        while (getline(fin, line)){
            istringstream token(line);
            getline(token, splitline, ',');
            istringstream toInt(splitline);
            toInt>>number;
            numbers.push_back(number);
            getline(token, splitline);
            lines.push_back(splitline);
        }
        for (int i=0;i<(int)numbers.size();i++){
            cout<<numbers[i]<<" "<<lines[i]<<"\n";
        }
    }
    else{
        cout<<"Invalid file.\n";
    }
    return (0);
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.