user1459748
user1459748

Reputation: 1

How to read input string with spaces of certain length from text file c++

My problem is that I am trying to input char, string then int from a text file. I know how to input using getline(), however after using the get line() function there is no longer an option to input the rest of the integers following the string. My question is, how can I input a char, then a string (with spaces) followed by 3 ints?

data.txt looks like this

a   New York    5    7   9
b   Virginia    10   2   5
c   Los Angeles 25   15  6

Here is my code:

   int main()
   {
    string city;
    double price;
    int amt1, amt2, amt3;
    char orderStatus;

    ifstream warehouse;
    ofstream output;
    warehouse.open("data.txt");
    output.open("dataOut.txt");

    while (warehouse.good()) 
    {
        warehouse >> orderStatus;
        output << orderStatus << "\t";

        getline(warehouse, city, '\t');
        //warehouse >> city;
        output << city << endl;

        //warehouse >> amt1;
        //output << amt1 << "\t";

        //warehouse >> amt2;
        //output << amt2 << "\t";

        //warehouse >> amt3;
        //output << amt3;
    }


    warehouse.close();
    output.close();

    return 0;
   }

Thanks for your much need help.

Upvotes: 0

Views: 1447

Answers (2)

deebee
deebee

Reputation: 1747

Here is my edit on your code. I added a warehouse >> noskipws >> orderStatus >> skipws; to skip the first tab delimiter. Also, added a if(!warehouse.good()) break; after every read in case there is incomplete data. If it were C, I'd have done fscanf(file, " %c %[^\t]s %d %d %d", ...).

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
    string city;
    double price;
    int amt1, amt2, amt3;
    char orderStatus;

    ifstream warehouse;
    ofstream output;
    warehouse.open("data.txt");
    output.open("dataOut.txt");

    while (warehouse.good()) 
    {
        warehouse >> orderStatus; 
        if(!warehouse.good()) break;
        output << orderStatus << "\t";
        // to skip the tab delimiter
        warehouse >> noskipws >> orderStatus >> skipws;
        if(!warehouse.good()) break;

        getline(warehouse, city, '\t');
        if(!warehouse.good()) break;
        output << city << "\t";

        warehouse >> amt1;
        if(!warehouse.good()) break;
        output << amt1 << "\t";

        warehouse >> amt2;
        if(!warehouse.good()) break;
        output << amt2 << "\t";

        warehouse >> amt3;
        if(!warehouse.good()) break;
        output << amt3 << endl;
    }


    warehouse.close();
    output.close();

    return 0;
}

Upvotes: 1

mwilliams
mwilliams

Reputation: 61

A quick solution would be to use atoi (link to docs). This sounds like homework so I don't want to solve it for you (where's the fun in that?) but you could pull the values in as strings. If you wanted you could also manually convert characters to integers one at a time and reconstruct the numbers but atoi would handle it all. I'm guessing these are std::string, so you'll have to call c_str() on them as atoi only accepts C strings.

Upvotes: 1

Related Questions