How to do parsing istringstream C++? - c++

I need to print some data from stream - istringstream ( in main () ).
example:
void Add ( istream & is )
{
string name;
string surname;
int data;
while ( //something )
{
// Here I need parse stream
cout << name;
cout << surname;
cout << data;
cout << endl;
}
}
int main ( void )
{
is . clear ();
is . str ( "John;Malkovich,10\nAnastacia;Volivach,30\nJohn;Brown,60\nJames;Bond,30\n" );
a . Add ( is );
return 0;
}
How to do parsing this line
is.str ("John;Malkovich,10\nAnastacia;Volivach,30\nJohn;Brown,60\nJames;Bond,30\n");"
to name;surname,data?

This is somewhat fragile, but if you know your format is exactly what you posted, there's nothing wrong with it:
while(getline(is, name, ';') && getline(is, surname, ',') && is >> data)
{
is.ignore(); // ignore the new line
/* ... */
}

If you know the delimiters will always be ; and ,, it should be fairly easy:
string record;
getline(is, record); // read one line from is
// find ; for first name
size_t semi = record.find(';');
if (semi == string::npos) {
// not found - handle error somehow
}
name = record.substr(0, semi);
// find , for last name
size_t comma = record.find(',', semi);
if (comma == string::npos) {
// not found - handle error somehow
}
surname = record.substr(semi + 1, comma - (semi + 1));
// convert number to int
istringstream convertor(record.substr(comma + 1));
convertor >> data;

Related

How to separate data from getline each time there is a space

int main() {
ifstream billFile;
billFile.open("bill.in"); //opening file
if( billFile.is_open() ){
string line1;
getline(billFile, line1);
cout << line1 << endl;
}
The bill.in file contains:
12 100
How can I separate 12 and 100 into two different variables?
You can use std::istringstream to parse line1:
int main()
{
ifstream billFile("bill.in"); //opening file
if( billFile.is_open() )
{
string line1;
if( getline(billFile, line1) )
{
istringstream iss(line1);
int value1, value2;
if( iss >> value1 >> value2 )
cout << value1 << ' ' << value2 << endl;
}
}
}
Of course, if your file has only 1 line, then you can just omit getline(), eg:
int main()
{
ifstream billFile("bill.in"); //opening file
if( billFile.is_open() )
{
int value1, value2;
if( billFile >> value1 >> value2 )
cout << value1 << ' ' << value2 << endl;
}
}
I use the following split function to divvy up each line based on some set of delimiters (a space in your case).
std::vector<std::string> split(const std::string& str,
std::string delims = " \t") {
std::vector<std::string> strings;
size_t start;
size_t end = 0;
while ((start = str.find_first_not_of(delims, end)) != std::string::npos) {
end = str.find_first_of(delims, start);
strings.push_back(str.substr(start, end - start));
}
return strings;
}
then you can split each line into a vector of strings:
int main() {
ifstream billFile;
billFile.open("bill.in"); //opening file
if( billFile.is_open() ){
string line;
getline(billFile, line);
std::vector<std::string> str = split(line, " ");
std::string line1 = str[0];
cout << line1 << endl;
}
}
You use this method to split up strings using any delimiter you want.
If you input file only contains one line and your keyboard is in very poor working order and you really need to minimize keystrokes you can write:
int main(){
int v1, v2;
if(std::ifstream("bill.in") >> v1 >> v2){
// here you have valid v1 and v2.
}
}
The >> "formatted input" is appropriate here because it is convenient to understand the contents of the text file as numbers.
Function std::getline() takes delimitation as claimed in C++ reference. So just use getline(istream, str, ' ');.
Modify your codes as
int main() {
ifstream billFile;
billFile.open("bill.in"); //opening file
if( billFile.is_open() ){
string line1;
getline(billFile, line1, ' ');
cout << line1 << endl;
}

C++ (Segmentation fault / Bus error / Memory limit exceeded / Stack limit exceeded)

This is my school program that crashes on me, probably something from (Segmentation fault / Bus error / Memory limit exceeded / Stack limit exceeded). I can't figure out where the mistake is. I tried to comment on the code and reduce it a bit.
Retrieving information from the file in the form [name] [surname] [number]
Martin Jeff 123456789
Tomas Adam 234567890
This is followed by a blank line [\ n]
And then I search by the entered name, surname or both
Martin
Thomas
Adam
Martin Jeff
...
Thank you in advance for your advice.
class Uzivatel
{
public:
string name;
string surname;
string number;
};
void alocation(int &velPole, Uzivatel *arr)
{
velPole = 2*velPole;
Uzivatel *tmp = new Uzivatel[velPole];
arr = tmp;
delete []tmp;
}
void getString(const string & fileName, ostream &strStream)
{
ifstream ifs;
ifs.open(fileName);
if(ifs.is_open())
{
strStream << ifs.rdbuf();
ifs.close();
}
}
void finding(int pocet, Uzivatel *uzivatele, ostream &out, string &line)
{
string name = ""; string surname = ""; string number = "";
int matches = 0;
stringstream s(line);
s >> name >> surname >> number;
if(!(surname.compare("")))
surname = name;
for(int i=0; i<pocet; i++)
{
if( (!name.compare(uzivatele[i].name)) || (!surname.compare(uzivatele[i].surname)) )
{
out << uzivatele[i].name << " " << uzivatele[i].surname << " " << uzivatele[i].number << endl;
matches++ ;
}
}
out << "-> " << matches <<endl;
}
bool isValid(string &jmeno, string &prijmeni, string &cislo)
{
int x = cislo.find_first_not_of("0123456789");
int y = cislo.length();
if((!x) || cislo[0] == '0' || y != 9 || jmeno=="" || prijmeni=="" || cislo=="" )
return false;
return true;
}
bool report ( const string & fileName, ostream & out )
{
ifstream ifs;
stringstream strStream;
getString(fileName, strStream);
int arrLen = 200;
Uzivatel *uzivatele = new Uzivatel[arrLen];
int arrElem = 0;
string line;
bool hledani = false;
while (getline(strStream, line))
{
if(hledani)
{
finding(arrElem, uzivatele, out, line);
}
else
{
stringstream s(line);
string konec = "";
s >> uzivatele[arrElem].name >> uzivatele[arrElem].surname >> uzivatele[arrElem].number >> konec;
/* If there was anything else at the entrance*/
if(konec!="")
{
delete []uzivatele;
return false;
}
/* Realloc */
if(arrElem == arrLen)
alocation(arrLen, uzivatele);
arrElem++;
/* Enter'\n' */
if(!line.compare(""))
{
hledani = true;
arrElem--;
/* Validation enter */
for(int i=0; i<arrElem;i++)
{
if(!(isValid(uzivatele[i].name, uzivatele[i].surname, uzivatele[i].number)))
{
delete []uzivatele;
return false;
}
}
}
}
}
if(!hledani)
{
delete []uzivatele;
return false;
}
delete []uzivatele;
return true;
}
int main ()
{
ostringstream oss;
oss . str ( "" );
assert ( report( "tests/test0_in.txt", oss ) == true );
assert ( oss . str () ==
"John Christescu 258452362\n"
"John Harmson 861647702\n"
"-> 2\n"
"-> 0\n"
"Josh Dakhov 264112084\n"
"Dakhov Speechley 865216101\n"
"-> 2\n"
"John Harmson 861647702\n"
"-> 1\n" );
oss . str ( "" );
assert ( report( "tests/test1_in.txt", oss ) == false );
return 0;
}
Test0 (Work correct):
John Christescu 258452362
Peter Herreran 716973426
Josh Dakhov 264112084
John Harmson 861647702
Dakhov Speechley 865216101
John
Martin
Dakhov
Harmson
You code as posted with the input data you gave (those 2 lines) works, except that the second assert fails. The reason being that you never write to the out stream in 'report'.
The reason it fails with a larger data set is due to this function
void alocation(int& velPole, Uzivatel* arr)
{
velPole = 2 * velPole;
Uzivatel* tmp = new Uzivatel[velPole];
arr = tmp;
delete[]tmp;
}
This function does nothing. It allocates a new larger array of Uzi objects, then deletes it. I assume its actually trying to delete the old one thats too small
You should return the new pointer after deleting the old one.
Much better would be std::vector which will do this all for you
Also why read the whole file into memory as a stringstream then read the stringstream, why not use the file stream directly?

Read columns from a comma delimited data file c++

I have been trying to read following data table and create an object for the HUBs(rows) and another object for continent (columns). Since I am not a C++ experienced user I have been facing some difficulties. The data is in following. The number after HUB and the dash shows the order from the hub. The other numbers under each continent are the corresponding cost and tariffs between a HUB and continent. I would like to be able to cout for instance following and get the result which would be 73.
cout << hub(1)->cont(USA)->transport() << endl;
,USA,EUROPE,ASIA
HUB1-12000,,,
Transportation Cost,73,129,141
Tariffs,5,5,1
ShippingType,a,b,c
OtherFees,0.6,0.3,0.8
HUB2-11000,,,
Transportation Cost,57,101,57
Tariffs,7,7,5
ShippingType,b,b,d
OtherFees,0.7,0.3,0.6
Really appreciate your help. Here is what I have tried so far:
void Hub()
{
string file = "/hubs.csv";
// 1-First read the first line and save the continent name
string str, field;
getline( fin, str );
vector<string> contList;
stringstream linestr( str );
while ( linestr.good() )
{
getline( linestr, field, ',' );
string contname;
contList.push_back(contname);
}
// 2-Then read the rest
getline( fin, str );
while ( !fin.eof() ) // Read the whole file
{
stringstream linestr( str );
string contname, order;
if ( qstr[0] == 'HUB1' || qstr[0] == 'HUB2')
{
// Read the name of the hub
getline( linestr, hubname, ',' ); // Read the hub name
getline( linestr, order, ',' ); // Read the order quantityity
int quantity;
istringstream orderstream( order);
orderstream >> quantity;
// Find the hub and add the order to the hub
Hub* hub = glob->FindHubName( hubname ); // this returns a pointer
if ( glob->FindHubName( hubname ) == nullptr )
{
hubNotFound.push_back( hubname );
getline( fin, qstr );
continue;
}
hub->TotalOrder( quantity );
}
else if ( qstr[0] != 'HUB1' || qstr[0] != 'HUB2')
{
// Read costs and tariffs
cout << hub(1)->cont(ASIA)->transport()
}
getline( fin, qstr );
}
fin.close();
}
Something like this:
#include <iostream>
#include <fstream>
#include <boost/tokenizer.hpp>
#include <string>
int main() {
using namespace std;
using namespace boost;
string line, file_contents;
fstream file("test.csv");
if (!file.is_open()) {
cerr << "Unable to open file" << endl;
return 1;
}
getline(file, line);
tokenizer<> tok_head(line);
int n_columns = 0;
for (tokenizer<>::iterator beg=tok_head.begin(); beg!=tok_head.end(); ++beg) {
cout << *beg << '\t';
n_columns++;
}
cout << endl;
while (getline(file, line)) {
file_contents += line;
}
file.close();
tokenizer<> tok(file_contents);
int i = 0;
for (tokenizer<>::iterator beg=tok.begin(); beg!=tok.end(); ++beg, ++i) {
cout << *beg;
if (i % n_columns) {
cout << '\t';
} else {
cout << endl;
}
}
return 0;
}
Makefile
all: t
t: csv.cpp
g++ -I /usr/include/boost csv.cpp -o t
It looks like you must parse each line using different logic, so you should check first column first and using it apply appropriate logic, below is some pseudocode for that:
std::fstream fs("test.txt");
std::string line;
//
// Read line by line
while (std::getline(fs, line)) {
std::istringstream str(line);
std::string rec_type;
// Read record type (your first two lines looks like are of no type?)
if ( !std::getline(str, rec_type, ',') )
continue;
// Decide type of record, and parse it accordingly
if ( rec_type == "Transportation Cost") {
std::string val;
// Read comma delimited values
if ( !std::getline(str, val, ',') )
continue;
int ival1 = std::stoi(val);
if ( !std::getline(str, val, ',') )
continue;
int ival2 = std::stoi(val);
// ...
}
if ( rec_type == "Tariffs") {
std::string val;
if ( !std::getline(str, val, ',') )
continue;
int ival = std::stoi(val);
// ...
}
}
One method is to consider each line as a separate record and object.
Let the objects read their data.
For example:
class Tariff
{
int values[3];
public:
friend std::istream& operator>>(std::istream& input, Tariff& t);
};
std::istream& operator>>(std::istream& input, Tariff& t)
{
// Read and ignore the label "Tariff"
std::string name;
std::getline(input, name, ','); // Read until ',' delimiter.
input >> t.value[0];
// Note: the ',' is not a digit, so it causes an error state,
// which must be cleared.
input.clear();
input >> t.value[1];
input.clear();
input >> t.value[2];
input.clear();
}
Another method is to read the label first, then delegate to a function that reads in the row.
std::string row_text;
std::getline(text_file, row_text); // Read in first line and ignore.
while (std::getline(text_file, row_text))
{
std::istringstream text_stream(row_text);
std::string label;
std::getline(text_stream, label, ','); // Parse the label.
// Delegate based on label.
// Note: can't use switch for strings.
if (label == "Tariffs")
{
Input_Tariff_Data(text_stream);
}
else if (label == "ShippingType")
{
Input_Shipping_Type_Data(text_stream);
}
//...
} // End-while
The if-else ladder can be replaced by a lookup table that uses function pointers. Sometimes the table is easier to read.
typedef void (*P_Input_Processor)(std::istringstream& text_stream);
struct Table_Entry
{
char const * label;
*P_Input_Processor input_processor;
};
//...
const Table_Entry delegation_table[] =
{
{"Tariffs", Input_Tariff_Data},
{"ShippingType", Input_Shipping_Type_Data},
};
const unsigned int entry_quantity =
sizeof(delegation_table) / sizeof(delegation_table[0]);
// ...
std::string row_text;
std::getline(input_file, row_text); // Read and ignore first line.
while (std::getline(input_file, row_text))
{
// Create a stream for parsing.
std::istringstream text_stream(row_text);
// Extract label text
std::string label;
std::getline(text_stream, label, ',');
// Lookup label in table and execute associated function.
for (unsigned int index = 0; index < entry_quantity; ++index)
{
if (label == delegation_table[index].name)
{
// Execute the associated input function
// by derferencing the function pointer.
delegation_table[index](text_stream);
break;
}
}
}
An alternative to the lookup table is to use:
std::map<std::string, P_Input_Processor>
or
std::map<std::string, void (*P_Input_Processor)(std::istringstream&)>

Reading from file in C++, dealing with blanks

I am tying to read from a text file in C++98. It has a pattern, but sometimes a field is empty:
ID Name Grade level
1 a 80 A
2 b B
3 c 90 A
How can I read from file such that I can ignore the blanks?
( I wish I could simply use Regex: \d*)
Is there any simple way of doing that?
You need to use what knowledge you have about the input to make assumptions about what is missing. You can use std::stringstream to parse individual terms from a text line. In other words std::stringstream deals with blanks by ignoring spaces and getting a complete term only, for example std::stringstream("aaa bbb") >> a >> b will load strings a with "aaa" and b with "bbb".
Here is an example program that parses the input, making a robust parser from scratch can be difficult, but if your input is strict and you know exactly what to expect then you can get away with some simple code:
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
//-----------------------------------------------------------------------------
// holds a data entry
struct Entry {
int id;
std::string name;
int grade;
std::string level;
Entry() {
// default values, if they are missing.
id = 0;
name = "Unknown";
grade = 0;
level = "?";
}
void ParseFromStream( std::stringstream &line ) {
std::string s;
line >> s;
if( s[0] >= '0' && s[0] <= '9' ) {
// a number, this is the ID.
id = atoi( s.c_str() );
// get next term
if( line.eof() ) return;
line >> s;
}
if( s[0] >= 'a' && s[0] <= 'z' || s[0] >= 'A' && s[0] <= 'Z' ) {
// a letter, this is the name
name = s;
// get next term
if( line.eof() ) return;
line >> s;
}
if( s[0] >= '0' && s[0] <= '9' ) {
// a number, this is the grade
grade = atoi( s.c_str() );
// get next term
if( line.eof() ) return;
line >> s;
}
// last term, must be level
level = s;
}
};
//-----------------------------------------------------------------------------
int main(void)
{
std::ifstream input( "test.txt" );
std::string line;
std::getline( input, line ); // (ignore text header)
while( !input.eof() ) {
Entry entry;
std::getline( input, line ); // skip header
if( line == "" ) continue; // skip empty lines.
entry.ParseFromStream( std::stringstream( line ));
std::cout << entry.id << ' ' << entry.name << ' ' <<
entry.grade << ' ' << entry.level << std::endl;
}
return 0;
}

Reading a line with integers and a string with spaces

I've an input formatted like this:
integer multi-word-string integer
I know the maximum lenght of multi-word-string, however I don't know how many words it contains. How can I read it ?
I'd read the line first and convert the first and last word to integers. Loosely:
std::string line;
std::getline(infile, line);
size_t ofs_front = line.find(' ');
size_t ofs_back = line.rfind(' ');
int front = std::strtol(line.substr(0, ofs_front).c_str(), NULL, 0);
int back = std::strtol(line.substr(ofs_back).c_str(), NULL, 0);
std::string text = line.substr(ofs_front, ofs_back - ofs_front);
You'll have to do some modifications to get rid of spaces (e.g. increment the offsets to gobble up all spaces), and you should add lots of error checking.
If you want to normalize all the interior spaces inside the text, then there's another solution using string streams:
std::vector<std::string> tokens;
{
std::istringstream iss(line);
std::string token;
while (iss >> token) tokens.push_back(token);
}
// process tokens.front() and tokens.back() for the integers, as above
std::string text = tokens[1];
for (std::size_t i = 2; i + 1 < tokens.size(); ++i) text += " " + tokens[i];
Read the first integer. Jump to back of the string and skip digits. Then read an int from this point. The part in the middle is the string. May not be 100% correct but:
char buf[256], *t = buf, *p, str[256];
fread(buf, 1, 256, file);
int s,e;
t += sscanf(buf, "%d", &s);
*p = buf + strlen(buf);
while (isdigit(*p)) p--;
sscanf(p, "%d", &e);
strncpy(str, p, p - t);
This is not as efficient as #KerrekSB's solution, but another way to do this is to extract the first integer, then loop through the rest of the string until you find the second integer.
#include <iostream>
#include <sstream>
int main()
{
std::istringstream ss( "100 4 words to discard 200" );
int first, second;
char buf[1000] = {0};
if( !( ss >> first ) ) {
std::cout << "failed to read first int\n";
return 1;
}
while( !( ss >> second ) || !ss.eof() ) {
if( ss.eof() ) {
std::cout << "failed to read second int\n";
return 1;
}
ss.clear();
if( ss.getline( buf, 1000, ' ' ).bad() ) {
std::cout << "error extracting word\n";
return 1;
}
}
std::cout << "first = " << first << "\n";
std::cout << "second = " << second << "\n";
return 0;
}