Simple file i/0 question

hi guys so i have to read a file the file is formatted as:

0101010101010101 ; this line is blah blah
0101010101010101 ; 2nd line

and so forth,so what i wana do is read in the 0's and 1's and ignore everything after the " ; " including the " ; " (semi-colon).
im reading the numbers into bitsets.
i also want to include the possibility that after the semi-colon there couldn't be a line description, that is semi-colons are constant in all of the lines but line comments may or may not be present.
i can read in the 0's and 1's just fine without ";" and line comments, so i guess what i really am asking is how do i ignore the ; and line comments , remember line comments may not be present in all lines
so guys how do i implement that?? thanks so much for ur time!
Last edited on
Your sample data shows a space between the digits and the semicolon, if the space is always present, you could just use the normal >> extraction operator to get the binary digits as a string, then use ignore() to ignore the remainder of each line.

Or maybe use getline with a semicolon as the delimiter like this:
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
#include <iostream>
#include <fstream>
#include <string>

    using namespace std;

int main()
{
    ifstream fin("bitdata.txt");
    if (!fin)
    {
        cout << "could not open file" << endl;
        return 1;
    }
    
    string bitstring;
    
    while (getline(fin, bitstring, ';'))  // note semicolon delimiter
    {
        cout << bitstring << endl; // show the important part 
        fin.ignore(1000,'\n');     // discard rest of line
    }

    return 0;
} 
Last edited on
yea i tried your method but unfortunately getline() asks for string where u have used

bitstring but im using " bitsets<16> mybits[100] " and not strings, i tried your method when i

started writing this code but since i couldn't accommodate the semi colon and the line

description i just deleted that from my text file temporarily so that i can continue on coding

rest of my code which is done now really but i now have to figure out what to do with

the semi colon and line descriptions. btw they are not really important since i dont do

anything with them in my code but they maybe/are present and i have to overcome them

Thanks for replying btw!
Well, just use the string to initialise the bitset.
1
2
3
4
5
6
7
8
9
    string bitstring;
    
    while (getline(fin, bitstring, ';'))
    {
        fin.ignore(1000,'\n');     
                
        bitset<16> mybits(bitstring);
        cout << mybits.to_ulong() << " " << mybits.to_string() << endl; 
    }


Or if you are using an array,
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
32
33
34
#include <iostream>
#include <fstream>
#include <string>
#include <bitset>

    using namespace std;

int main()
{
    bitset<16> mybits[100];
    int count = 0;
  
    ifstream fin("bitdata.txt");
    if (!fin)
    {
        cout << "could not open file" << endl;
        return 1;
    }
    
    string bitstring;
    
    while (getline(fin, bitstring, ';'))
    {
        fin.ignore(1000,'\n');     
        mybits[count++] = bitset<16>(bitstring);
    }
    
    for (int i=0; i<count; i++)
    {    
        cout << mybits[i].to_ulong() << " " << mybits[i].to_string() << endl; 
    }

    return 0;
}



Or another way:
1
2
3
4
5
    bitset<16> mybits[100];
    int count = 0;

    while (fin >> mybits[count++])
        fin.ignore(1000,'\n');

Last edited on
Topic archived. No new replies allowed.