How do you read 6 characters from a file at a time?

Hey guys, I'm writing from my fresh new account. I'm a beginner so please, excuse my ignorance. I would like to read 6 bytes at a time from a text file using ifstream. For example, the file contains this information:

 
  73647364863242384625347632543243434234234


I want to read the first 6 numbers, then the second 6, and so on. Anyone care to help me on that?
http://www.cplusplus.com/reference/istream/istream/get/

or

http://www.cplusplus.com/reference/istream/istream/getline/

They can be used to do the same thing. The only difference being getline removes the delimiter if it finds it, get just stops getting.
Last edited on
It always says no matching member function for call to get when I try to use it. That is why I didn't. How do I fix that?
I'v been getting this message since forever "no matching member function for call to get". That's why I couldn't use it.

The reason why you couldn't use it was probably because the wrong parameters were specified. You'd need to show the actual code which didn't work for a more detailed explanation.
I kind of know my mistake now :P However, this
1
2
ifstream infile;
word=infile.get()

will read the next character in the file right? How do I make it read 6 characters? (specifically, 6 numbers in a sequence of numbers).
Hey guys, thanks a lot for your help, I used Yay295's advise and used the get function. I looped it to get 6 characters and saved them in an array, then converted it into an int. Again, thanks a lot.
You could do that without a loop. Just specify the number of characters in the call.
The example on the reference page shows how to read 255 characters into a buffer. Just change 256 to 7.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <fstream>
#include <cstdlib>

using namespace std;

int main()
{
    ifstream fin("input.txt");
    
    char buf[7];

    while (fin.get(buf, 7))
    {
        int numchars = fin.gcount(); // optional, see how many chars were read.
        int num = atoi(buf);
        
        cout << "length: " << numchars 
             << "  buf: "  << buf 
             << "  int: "  << num << endl;

    }    
  
} 


output:
length: 6  buf: 736473  int: 736473
length: 6  buf: 648632  int: 648632
length: 6  buf: 423846  int: 423846
length: 6  buf: 253476  int: 253476
length: 6  buf: 325432  int: 325432
length: 6  buf: 434342  int: 434342
length: 5  buf: 34234  int: 34234


You could also bypass the C-string and just use the extraction operator>> into a string, then use either a stringstream to process the string or something like stoi(), atoi().

#include <iostream>
#include <sstream>
#include <iomanip>

using namespace std;

int main()
{
std::string input = "73647364863242384625347632543243434234234";
std::string output;
int num;

std::istringstream ins(input);

while(ins >> setw(6) >> output) // Extract 6 characters from the stream.
{
std::istringstream sin(output);
sin >> num;
cout << "output:" << setw(7) << output
<< " length: " << output.length()
<< " num:" << setw(7) << num << endl;
}
}


Topic archived. No new replies allowed.