James2007

Still having problems with printing out INT64 integers

I am still having problems with this code. This prints out a number, but it's not exactly what I want. I need to print out an INT64 number that is in sync all the way through. For example:

The hex: FF D8 FF E1 1E 63 45 78 is the first line of the file test.jpg. I would like to display its integer (unsigned) value: 18435766416983672184

But what it does it prints out is the number for hex 78 45 63 1E E1 FF D8 FF, which is 8666442042236197119.

I thought this would be ok, but later when I want to do more stuff to the program it won't work.

There has to be a simple way of reading in 64 bits of data and then output the decimal value into a .txt file. Please can someone help me!

This is the code I have now, but if you can suggest a better way, IĄŻll take it!

#include <stdio.h>

#include <stdlib.h>

int main(void)

{

unsigned __int64 Oneline;

char buf[ 8];

FILE *INfptr;

FILE *OUTfptr;

if ((fptr = fopen( "test.jpg", "rb" )) == NULL ){

printf ( "Cannot open file\n" );

exit ( 1 );

}

if ((fptr = fopen( "Kounter-report32.txt","w+"))== NULL ){

printf ( "Cannot open file\n" );

exit ( 1 );

}

do{

fread ( buf, 1, sizeof(buf), INfptr );

// fread(reinterpret_cast<char*>(&Oneline), 1, sizeof(Oneline), INfptr);

(WAS using serveral different methods of reading and writing, they worked but not the way I need it too.)

fprintf(OUTfptr,Ą±%I64u/tĄ±, Oneline);

}while (!feof (INfptr));

fclose ( INfptr );

fclose ( OUTfptr );

}

Thank you once again, and sorry for troubling you guys so much

James




Re: Visual C++ General Still having problems with printing out INT64 integers

aao123

do{

fread ( buf, 1, sizeof(buf), INfptr );

fread (&Oneline , 1, sizeof(unsigned __int64 ), INfptr );

// fread(reinterpret_cast<char*>(&Oneline), 1, sizeof(Oneline), INfptr);

Ą­Ą­





Re: Visual C++ General Still having problems with printing out INT64 integers

Marius Bancila

How I see the problem: in the file, data is in big endian layout (FF D8 FF E1 1E 63 45 78, FF being the most significant byte). When you read it, since your machine uses a little endian layout, with the least significant byte first, you have a problem.

Resolution that was already given to you: after reading the data, reverse the bytes:

std::reverse(&buf[0], &buf[7]);

Then you'll have it from big endian to little endial layout and you'll be able to cast it to an int64.

See this article for little endian vs. big endian layouts.

 

 






Re: Visual C++ General Still having problems with printing out INT64 integers

aao123

btw I assume based on "rb" that input is a binary file



Re: Visual C++ General Still having problems with printing out INT64 integers

Viorel.

 Marius Bancila wrote:

[...]

Resolution that was already given to you: after reading the data, reverse the bytes:

std::reverse(&buf[0], &buf[7]);

Probably the right call is

std::reverse(&buf[0], &buf[ 8 ])

or

std::reverse((char*)&Oneline, (char*)(&Oneline + 1)).

 





Re: Visual C++ General Still having problems with printing out INT64 integers

Marius Bancila

Yes, sure, my bad. That's why I like to put this usually this way:

std::reverse(buf, buf + sizeof(buf));






Re: Visual C++ General Still having problems with printing out INT64 integers

James2007

Thanks very much

I remember the std::reverse but I really, really don't know how it works. Could I Possibly beg one of you guys to write out the code. I tried adding in the code myself and it didn't work out for me!

Sorry about this but I am new and never had need of these functions

James