简体   繁体   中英

How can i read string and integer, on same line from a text file?

I have this example,

On my .txt file i have “Jorge Saraiva 1321312”

my .cpp

string line, nome;
int number;

ifstream ifi("nameOfFile.txt");

 if(!ifi.is_open()){
   cout << "Error opening file" << ends;
 }
 {
  else{
    while( getline(ifi,line) ){
      istringstream is(line);

       is >> nome; 
       is >> number; 
    }
    ifi.close();
  }
 cout << nome  << endl << number << ends;

With this i only got first name ("Jorge"), I'm not sure how can i tell compiler when the name/string ends.

You are trying to put string (2nd one) into a number, that is the cause of unexpected output. Directing it to a string should fix that. You can discard the string, doesn't matter.

string line, name, dummy;
int number;
while(getline(ifi, line)){  
    istringstream iss1(line);
    iss1 >> name; 
    iss1 >> dummy;
    iss1 >> number; 
}

There's one solution I can see for your problem. If you want to have multiple names (no matter the size) and then a number on front you can take advantage form the isdigit function.

In your while cycle when reading from the file you can add something like:

String temp;
is >> name; 

    while (is){
    is >> temp; 

    if(isdigit(temp[0])){
            int number = atoi(temp.c_str()); } 
    else {
          name += " " + temp;
       }
    }

That way you can have multiple sized names.

I'm writing on a phone so it's harder but you can get an idea of what I'm talking about. Yet don't forget to include the library.

Mostly, when dealing with csv files, your data is delimited with semicolon or some other character. In you case you dont have it, so you must do more advanced parsing. You may use regexps for that:

#include <regex>

// ...

// Pattern
std::regex pattern("([^\\d]+)\\s*([\\d ]+)" );

// ...

// And instead of istringstream
std::smatch sm;
if (std::regex_match(line, sm, pattern)) {
  nome = sm[1];
  number = std::stoi(sm[2]);
}

You can choose to keep extracting as an integer, unless successful

int number;
string name;

istringstream record(line);

// keep trying till a number is found.
record >> number;
while (record.fail() && !record.eof()) {
    record.clear();

    string temp;
    record >> temp;
    name.append(temp);

    record >> number;
}

cout << "Name: "   << name   << endl;
cout << "Number: " << number << endl;

Complete Code:

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

using namespace std;

int main ()
{
    string line;
    while (getline(cin, line)) {
        int number;
        string name;

        istringstream record(line);

        // keep trying till a number is found.
        record >> number;
        while (record.fail() && !record.eof()) {
            record.clear();
            string temp;
            record >> temp;
            name.append(temp);
            record >> number;
        }

        cout << "Name: "   << name   << endl;
        cout << "Number: " << number << endl;
    }


    return 0;
}

Try this code

string line, firstname,lastname;
int number;

ifstream ifi("nameOfFile.txt");

if (!ifi.is_open()) 
{
cout << "Error opening file" << ends;
}
else
{
while (ifi>>firstname>>lastname>>number) {}
ifi.close();
}
cout << firstname <<endl<<lastname <<endl << number << ends;

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