tags:

views:

1424

answers:

3

Is there a function for FILE (fopen?) that allows me to just read one int from a binary file?

So far I'm trying this, but I'm getting some kind of error I can't see cause the program just crashes without telling me.

void opentest()
{
    FILE *fp = fopen("dqmapt.mp", "r");
    int i = 0;
    int j = 0;
    int k = 0;
    int * buffer;
    if (fp)
    {
        buffer = (int *) (sizeof(int));

        i = (int) fread(buffer,1, (sizeof(int)), fp);
        fscanf(fp, "%d", &j);
        fclose(fp);
    }

    printf("%d\n", i);
    printf("%d\n", j);
}
+2  A: 

Do you want to read a textual representation of an int? Then you can use fscanf, it's sort of the opposite of printf

int n;
if( fscanf(filePointer, "%d", &n) == 1 )
  // do stuff with n

If you want to read some binary data and treat it as an int, well that's going to depend how it was written in the first place.


I am not a Java programmer, so this is just based on what I've read in the docs.

That said, it says

Writes an int to the underlying output stream as four bytes, high byte first. If no exception is thrown, the counter written is incremented by 4.

So it's a big endian four byte integer. I don't know if it's two's complement or not, but that's probably a safe assumption (and can probably be found somewhere in the java docs/spec). Big endian is the same as network byte order, so you can use ntohl to convert it the endianness of your C++ platform. Beyond that, you just need to read the four bytes, which can be done with fread.

Logan Capaldo
binary, written using Java's writeInt() method. The file is "’ wl "
William
A: 

Int represented as text or binary?

For text, use fscanf; for binary, use fread.

oefe
+6  A: 

Now that you have changed your question, let me ask one. What is the format of the file you are trying to read?

For a binary file there are some changes required how you open the file:

/* C way */
FILE *fp = fopen("text.bin", "rb"); /* note the b; this is a compound mode */

/* C++ way */
std::ifstream ifs("test.txt", ios::in | ios::binary);

Reading in the contents is easy. But remember, your file has 2 integers at the begining -- width, height which determine how many more to read i.e. another width * height number of integers. So, your best bet is to read the first two integers first. You will need to use two buffers -- one for the width and height and then depending on their value another one to read the rest of the file. So, lets read in the first two integers:

char buf[ 2 * sizeof(int) ]; /* will store width and height */

Read in the two integers:

/* C way */
fread(buf, sizeof(int), 2, fp); /* the syntax changes, FILE pointer is last */

/* C++ way*/ 
ifs.read(buf, sizeof buf);

Now, the tricky part. You have to convert the stuff to double. This again depends on your system endianness -- whether a simple assignment works or whether a byte swapping is necessary. As another poster has pointed out WriteInt() writes integers in big-endian format. Figure out what system you are on. And then you can proceed further.

FILE is a C datastructure. It is included in C++ for C compatibility. You can do this:

/* The C way */
#include <stdio.h>
#include <stdlib.h>

int main(void) {
   FILE *fp = fopen("test.txt", "r");
   int i = 0;
   if (fp) {
      fscanf(fp, "%d", &i);
      fclose(fp);
   }
   printf("%d\n", i);
}

You can use the std::ifstream thing to open a file for reading. You have to read in the contents using some other incantation to read the file contents and extract the desired information out of it yourself.

/* The C++ way */
#include <fstream>
#include <iostream>

int main() {
   std::ifstream ifs("test.txt");
   int i = 0;
   if (ifs.good()) {
      ifs >> i;
   }
   std::cout << i << std::endl;
}

Note you can use the C style functions in C++ as well, though this is the least recommended way:

/* The C way in C++ */
#include <cstdio>
#include <cstdlib>

int main() {
   using namespace std;
   FILE *fp = fopen("test.txt", "r");
   int i = 0;
   if (fp) {
      fscanf(fp, "%d", &i);
      fclose(fp);
   }
   printf("%d\n", i);
}

[Note: Both examples assume you have a text file to read from]

dirkgently
Could you tell me how to do this with a binary file?
William
You need to specify the format of the binary file :)
dirkgently
It's from Java's writeInt() method. It's just a simple map file. width, height, than a number of ints equal to (width * height)
William
I've made a lot of progress thanks to you, but it looks like I'll need to byte swap. I'm on windows XP home, if that helps.
William
Intel processors are typically little endian. So, yes a swap looks inevitable.
dirkgently