How to read formatted data

I want to read an number of integers from a .txt file. The file is composed of three columns of numbers each separated by a space. So it's formatted as follows:

number1 number2 number3

and example file would look like this:

1 0x00000010 0x0000000A
0 0x00000804
1 0x00000808 0xFFFFFFFF
0 0x0000080C 0x5A5A5A5A

and so on...

I'm not sure how to read in integers from a file? I can read strings but I can't find anything that deals with these (hexadecimal numbers).

I need to read in the first number, identify the whitespace as the end of it and then read the next number on the line until a whitespace if found and then read the last number in if it exists (it will only be there if the number in the first column is a 1).

Can anyone help me with this as I'm very stuck.
closed account (o3hC5Di1)
Hi there,

You may want to try scanf or fscanf:

http://www.cplusplus.com/reference/clibrary/cstdio/scanf/
http://www.cplusplus.com/reference/clibrary/cstdio/fscanf/

Let us know if you need an example of those.

All the best,
NwN
Hi NwN, an example would be fantastic!

Could you show me an example of it reading in and store the three different numbers per line in the example I gave above?

so for the first row: 1 0x00000010 0x0000000A, it would read in

int1=1 int 2=0x00000010 and int3=0x0000000A.

That would be fantastic! :)

Nwn is there a C++ equivalent that I could use?
closed account (o3hC5Di1)
Hi there,

I've not tested this, but it should give you an idea.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string>
#include <cstdio>

int main()
{

string line = "1 0x00000010 0x0000000A"
int int1, int2, int3;
//using sscanf no because i'm using a string, scanf is for streams
//=scan string "line" for format "decimal<space>hex<space>hex" and put results in the following variables
sscanf(line, "%d %x %x", &int1, &int2, &int3);

cout << int1 << " " << int2 << " " << int3;
return 0;

}


Hope that helps you on your way :)

All the best,
NwN
Last edited on
This page has information about the manipulator functions you're looking for:

http://www.cplusplus.com/reference/iostream/istream/operator%3E%3E/

The basic idea is that you can tell the extraction operator (>>) what kind of integer representation you want you use (e.g., dec, bin, oct ...) to read in a particular value.



Last edited on
Something like this:

1
2
3
4
5
6
7
8
9
10
11
12
std::ifstream file( "whatever.txt" ) ;
std::string line ;
while( std::getline( file, line ) )
{
    std::istringstream stm(line) ;
    int i, j, k ;
    if( stm >> std::dec >> i >> std::hex >> j && ( i==1 ? stm >> k : !( k = 0 ) ) )
    {
        // use i, j, and if i==1, k
    }
    else /* error */ break ;
}
NwN I've tried it but get an error which reads
error C2664: 'sscanf' : cannot convert parameter 1 from 'std::string' to 'const char *'

Am I right in thinking this is because the numbers are defined in a string, called line, but when I call sscanf I'm looking for a decimal value and then a hexadecimal?

If so how would I fix this?
sscanf takes a C-style string as it's argument, and the C++ strings are class objects. So when you try to use std::string as the argument, sscanf gets confused and decides to quit. (Not the technically correct answer, but the same idea :-)
closed account (o3hC5Di1)
JLBorges / astropos 's idea are probably better here, I'd go for that.

Could either one of you perhaps be so kind as to explain why you prefer this method?
Is it because it directly acts on the stream and is consequently much faster than using a function to traverse the file?

All the best,
NwN
NwN wrote:
Could either one of you perhaps be so kind as to explain why you prefer this method?


I prefer manipulator functions because they are part of the C++ Standard Proper, while sscanf et al. are C library functions.
closed account (o3hC5Di1)
Thanks!

N
Topic archived. No new replies allowed.