简体   繁体   中英

Reading formatted data from file in C++

I am trying to write a code to read data from file. The file looks like:

47012   "3101 E 7TH STREET, Parkersburg, WV 26101"
48964   "S16 W22650 W. LINCOLN AVE, Waukesha, WI 53186"
.
.
.
.

I need to store the number as an int and the address as a string.

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

int main()
{
ifstream myfile;
myfile.open("input.txt");
long int id;
string address;
myfile >> id;
cout << id << endl;
myfile >> address;
cout << address.c_str() << endl;
myfile.close();
system("pause");
return 0;
}

The output of program

47012
"3101

The output that I need is

47012
3101 R 7TH STREET, Parkersburg, WV 26101

How do I go about doing this. Thanks in advance Any help is appreciated

I'd do something like the following. Nah, just kidding, I'd use Boost Spirit in real life. However, this seems like something you could attempt with standard library approaches too:

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

using namespace std;

int main()
{
    ifstream myfile("input.txt");

    std::string line;
    while (std::getline(myfile, line))
    {
        std::istringstream linereader(line, std::ios::binary);

        long int id;

        linereader >> id;
        if (!linereader)
            throw "Expected number";

        linereader.ignore(line.size(), '"');

        string address;
        if (!std::getline(linereader, address, '"'))
            throw "Expected closing quotes";

        cout << id << endl << address << endl;
    }
    myfile.close();
}

Printing:

47012
3101 E 7TH STREET, Parkersburg, WV 26101
48964
S16 W22650 W. LINCOLN AVE, Waukesha, WI 53186

Simply use getline :

while (in >> id) {
    if (!getline(in, address)) {
        // (error)
        break;
    }

    // substr from inside the quotes
    addresses[id] = address.substr(1, address.length() - 2);
}

This doesn't work because the stream operator >> will take spaces as delimiter when trying to read strings.

You could use getline(stream, address, '\\t'); to read a string with a specific delimiter.

Or simply getline(stream, address) if there is nothing else to read on that line :

long int id;
string address;
myfile >> id;
getline(stream, address);

This is just an example, see @not-sehe 's answer for a complete solution (read the lines using getline and then parse each line using a stringstream ).

You can use cin.getline() which will read the remainder of the line.

First read the digit, then use getline() to read everything remaining.

The >> operator terminates a string at a space.I would recommend using

char temp[100];
myfile.getline(temp,max_length);

This reads a line at a time.Then you can use a loop to split the line in the way you want.

I would like to add that you might need the atoi(char *) (from module cytpe.h ) function to convert an integer string to an integer.

    getline(myfile, address, '"');//dummy read skip first '"'
    getline(myfile, address, '"');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM