Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ FILE readInt function? (from a binary file)

Tags:

c++

file

binary

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);
}
like image 849
William Avatar asked May 05 '26 12:05

William


2 Answers

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]

like image 184
dirkgently Avatar answered May 08 '26 00:05

dirkgently


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](http://java.sun.com/j2se/1.4.2/docs/api/java/io/DataOutputStream.html#writeInt(int)).

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.

like image 21
Logan Capaldo Avatar answered May 08 '26 01:05

Logan Capaldo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!