Matt Fenlon
Matt Fenlon

Reputation: 688

getline() getting incorrect characters

I'm having a problem with getline(). The first line in my text file is C C0 H:7 E:7 P:10, but printing line (which holds the string returned by getline()) yields C▒Hdtors. What is happening here?

main.cpp

int main(int argc, char* argv[]) {

    string line,tmp_holder;
    int tmp_h, tmp_e, tmp_p, counter=0;
    vector<string> info;
    ifstream finp(argv[0]);

    map<int, Circuit> circuits;
    vector<Juggler> jugglers;

    if (!finp.is_open()) {
        cerr << "Unable to open input file " << argv[0] << endl;
        exit(1);
    }

    while (getline(finp,line)) {

        tmp_holder = "";
        info.clear();

        if (line[0] == 'C') {

            cout << "Line: " << line << endl;

Upvotes: 1

Views: 94

Answers (1)

ChrisD
ChrisD

Reputation: 674

argv[0] is your program name, not the file name. Change

ifstream finp(argv[0])

to

ifstream finp(argv[1])

Upvotes: 4

Related Questions