简体   繁体   中英

Reading In Formatted Data with Multiple Delimiters C++

I'm trying to read in data from command prompt with multiple delimiters, for example:

data 1, data2, data3.

I'd like the code to read in the data before the first comma, the data after that and before the second comma, and finally the data after that but before the period. I have been using this:

getline(cin, VAR, ',');
cin.get();
getline(cin, VAR2, ' ');
getline(cin, VAR3, ',');
cin.get();
getline(cin, VAR4, '.');

And it does what I want, provided the information is entered correctly. However, how can I check and see if the information wasn't? Because if the user doesn't enter two commas and a period, the program gets stuck as I'm assuming the cin stream gets broken after not finding the delimiter as specified in the getline() read. I've tried this:

if (cin.fail()) {
        cout << "failure" << endl;
}

After a getline, but it never runs if the delimiter isn't input. What's the best way to do this, check for errors and see if data wasn't entered in the desired format?

You have to parse the input yourself.

For starters, your input does not consist of "data with multiple delimiters". Your input consists of a single line of text, that was entered, according to your description, at a command prompt of some kind.

If so, then the line of text should be read with a single std::getline() library function, since, by default, std::getline() uses \\n as a default delimiter.

std::string str;

std::getline(std::cin, str);

Now, you've read your input into str . Now that this task is complete you can get down to business verifying whether str contains properly formatted input with the appropriate delimiters, or not.

To do that, you will use the rich set of methods that are available from your std::string object. For example one such method, find() , searches the string for a particular character, such as your first comma:

auto n=str.find(',');

The full documentation for find() , whose description you will find in your C++ book, will explain how to determine whether find() actually found the comma, or not. Based on that your program can either take the appropriate action (if the comma was not found) or use the substr() method, on this str , to extract the part of the string before the comma. That would be your first VAR. Then, use substr() again to extract the rest of the entered text, after the first comma.

You will also find a complete description of substr() in your C++ book.

After using substr() to extract the rest of the entered text, after the first comma, you will simply repeat the same overall process for extracting data2 , and the following item of input, this time using a period.

And, at each step of the way you will have all the information your program will need to determine whether or not your input was formatted correctly.

You could read in the entire line into a std::string , then use std::istringstream to parse the string:

#include <iostream>
#include <string>
#include <sstream>
#include <cstdlib>

int main(void)
{
    const std::string   input_text = "data1, data2, data3\n";
    std::istringstream  input_stream(input_text);

    std::string data1;
    std::string data2;
    std::string data3;

    std::getline(input_stream, data1, ',');
    std::getline(input_stream, data2, ',');
    std::getline(input_stream, data3, ',');

    std::cout << "data1: " << data1 << '\n';
    std::cout << "data2: " << data2 << '\n';
    std::cout << "data3: " << data3 << '\n';

    return EXIT_SUCCESS;
}

If the std::getline fails, that means there is an error in the data.

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