Skip char when reading

Hi everyone, I have a 2 part question:

1)
When reading from a file or anything for that matter, what is the best method to skip a certain character?

Lets say I have data that follows a pattern of: string int "," string int "," ... string int ","

I want to read the string, the int, and skip the comma, then read the next string, the next int, skip the next comma, until the end of file.

I know the easiest way is just create a char and buffer it in, but is there a more "industry professional" way to do it?


2) Unrelated

Lets say instead of a string and int, I have a string string ","

if I use getline to read the whole thing, how can I split the string to remove the comma?

Last edited on
> I know the easiest way is just create a char and buffer it in,
> but is there a more "industry professional" way to do it?

The "industry professional" way to do something is the simplest, the most transparent way in which it can be done - in this case (if the string does not contain embedded white space), create a char and read the comma into it (and if validation is required, verify that it indeed was a comma).


> I use getline to read the whole thing, how can I split the string to remove the comma?

We can specify comma as the delimiter and call getline repeatedly to read in each part without the comma.
For instance: std::getline( stm, str, ',' ) ;
So let's say I have the following file containing:

Test 1, Test 2, Test 3, Test 4, Test 5, .... Test n;

Where the data types are:

String int, String int, String int, .... String int;

If I use the getline(stm, str, ',') in a while loop, then str would contain after each iteration:

Test x1
Test x2
Test x3
Text x4
Test xn

However I want "Test" to be stored in a different variable than "x1" and this getline would put Test & x1 in the same variable.

If I wanted "Test" and "x1" to be stored in two seperate variables instead of one, is the best way to split str and then put the split string into the 2 variables?

I hope this makes sense
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <string>
#include <sstream>

int main()
{
    std::istringstream file( "Test 1, CPUTest 2, MemoryTest 3, GPUTest 4, Test 5, Test 6;\n" ) ;

    std::string test_name ; // does not contain white space
    int test_number ;
    char separator ;

    while( file >> test_name >> test_number >> separator && separator == ',' )
        std::cout << "test name: '" << test_name << "' #" << test_number << '\n' ;

    if( separator == ';' ) // the last one ends in ;
        std::cout << "test name: '" << test_name << "' #" << test_number << '\n' ;
}

http://coliru.stacked-crooked.com/a/e372425ce98bf226
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
using namespace std;


string blankPunctuation( string line, string punctuation )
{
   for ( char &c : line )
   {
      if ( punctuation.find( c ) != string::npos ) c = ' ';
   }
   return line;              // original passed by value, so not changed
}
 


int main()
{
   const string punctuation = ",;";
   string line, text;
   int number;

// ifstream in( "infile.txt" ); 
// getline( in, line );
   getline( cin, line );
   line = blankPunctuation( line, punctuation );    // change punctuation to white space (note: incl. at end)
   stringstream ss( line );
   while ( ss >> text >> number ) cout << text << " " << number << endl;
}


alpha 1, beta 2, gamma 3, delta 4;
alpha 1
beta 2
gamma 3
delta 4


Hmm. It (belatedly) occurs to me that blankPunctuation() could probably be replaced by judicious use of replace_if().
Last edited on
Topic archived. No new replies allowed.