I'm having a little bit of trouble using the ifstream 'get' command. When I say trouble I mean it is not returning the values expected and I need a little guidance, if you will :).

Basically what I'm doing is reading a file in through C++ and then going to a specific location and pulling out a few pieces of the file and printing them to the screen. The expected values maybe be numeric integers or alpha-characters. All data is human-readable at the end of the day.

For those curious (or maybe with experience/sample code), this a NIDS file from the NWS.

Here is my code:

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <sys/stat.h>

using namespace std;

int main()
{
        char file[] = "wx_data/2008/8/25/p19r0/kict.dat";
        struct stat results;
        char ch;

        if (stat(file, &results) == 0)
        {
                cout << "File Size: " << results.st_size << endl;
        }
        else
        {
                cout << "Error while stat'ing the file." << endl;
        }

        ifstream myFile (file, ifstream::in | ios::binary);

        myFile.seekg(50);
        for (int x = 0; x < 4; x++) {
                myFile.get(ch);
                cout << hex << (int)ch << ' ';
        }

        myFile.close();


        return 0;
}

I'm using 'c++' to compile this on a 32-bit system running Fedora. I'm assuming that c++ is actually using gcc on the backend. My compiler line is c++ -o parser parser.cpp.

When I put the file pointer to '12' and change it to pull 2 positions, which is the day of the file, I should expect '26' which prints out '32 36' which is 2 and 6 in hex. This works great. However when I change the file position to '50' which is the latitude of the radar I receive some crazy numbers. This covers four positions in the file and should be in the format of xxyy without decimal. When I execute this I receive '0 0 ffffff93 16'. When looking at the same locations with a hex viewer I see '00 00 93 16'.

My question is why in the world am I see those 'ffffff' values in there tacked onto the 93? I see this happen multiple places throughout the file. Any help would be appreciated!

I have attached a copy of the file. (rename to .dat)

Thanks
Kelly

Recommended Answers

All 2 Replies

istream.get( ) takes the address of a signed char. You're reading in values that look like they should be interpreted as unsigned char. Something like 0x93 is a negative value in terms of a signed char, thus the leading f's.

If you use the extraction operator in stead of .get( ), you can read in as unsigned char and get the result you seek.

unsigned char ch;


        ifstream myFile (file, ifstream::in | ios::binary);

        myFile.seekg(50);
        for (int x = 0; x < 4; x++) {
                myFile >> (ch);
                cout << hex << (int)ch << ' ';
        }

Thanks vmanes! What you're saying makes complete sense... I still have quite a learning curve with C++ but hopefully I'll get there! :)

Thanks again...

Kelly

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.