|
 |
|
Dev Articles Community Forums
> Programming
> C/C++ Help
|
File IO linux and solaris
Discuss File IO linux and solaris in the C/C++ Help forum on Dev Articles. File IO linux and solaris C/C++ Help forum discussing building and maintaining applications in C/C++. Find out why these languages are the foundation on which other languages are built.
|
|
 |
|
|
|
|

Dev Articles Community Forums Sponsor:
|
|
|

July 24th, 2007, 09:17 AM
|
|
Registered User
|
|
Join Date: Jul 2007
Posts: 2
Time spent in forums: 40 m 44 sec
Reputation Power: 0
|
|
|
File IO linux and solaris [SOLVED]
Hi,
I am having some trouble making some code work in Solaris. After debugging for a while I found out that the problem was in File IO. Namely a simple program such as this produces entirely different results in Linux and Solaris:
/*
Compile commands
gcc -Wall -O2 -o test.exe test.c //linux
cc -o test.exe test.c //solaris
*/
#include <stdio.h>
#include <stdlib.h>
void testRead (char* filename);
int main(int argc, char *argv[]) {
testRead("/home/everolth/images/image5.bmp");
return EXIT_SUCCESS;
}
void testRead (char* filename) {
FILE* f;
f = fopen(filename,"r");
if (fseek(f, 0, SEEK_SET)) {
printf("Error while accessing the file \n");
exit(1);
}
unsigned short int a,c,d;
unsigned int b,e;
//Read the headers
fread((void*)&a,sizeof(unsigned short int),1,f);
fread((void*)&b,sizeof(unsigned int),1,f);
fread((void*)&c,sizeof(unsigned short int),1,f);
fread((void*)&d,sizeof(unsigned short int),1,f);
fread((void*)&e,sizeof(unsigned int),1,f);
printf("%li %li \n",sizeof(unsigned int),sizeof(unsigned short int));
printf("%hu %u %hu %u %u \n",a,b,c,d,e);
fclose(f);
}
/*
*
* Outputs:
* Solaris:
4 2
16973 905970432 0 0 905969664
* Linux:
4 2
19778 196662 0 0 54
*/
Does anyone know why?
By the way, obviously the files are supposed to be exactly the same file and I know for sure that the output I get when I run this code in linux is the correct one.
Thanks in advance
Carlos
|

July 26th, 2007, 09:23 AM
|
|
Registered User
|
|
Join Date: Jul 2007
Posts: 2
Time spent in forums: 40 m 44 sec
Reputation Power: 0
|
|
Hi again,
I found the solution to this problem. Basically Solaris' fread reads the bytes in the "wrong" order. So when I read a short int, the second byte was actually the first one and the first byte was actually the second one. If I swap them around then I obtain the same results as in my computer. I know this is probably correct, and it is probably documented somewhere (not in the man page though), but I think that one should be able to expect that the same function produces the same results in different implementations... And if not at least one should expect these kind of things to be properly documented (not a single word about this in the man page of fread...)
Quote: | Originally Posted by everolth Hi,
I am having some trouble making some code work in Solaris. After debugging for a while I found out that the problem was in File IO. Namely a simple program such as this produces entirely different results in Linux and Solaris:
/*
Compile commands
gcc -Wall -O2 -o test.exe test.c //linux
cc -o test.exe test.c //solaris
*/
#include <stdio.h>
#include <stdlib.h>
void testRead (char* filename);
int main(int argc, char *argv[]) {
testRead("/home/everolth/images/image5.bmp");
return EXIT_SUCCESS;
}
void testRead (char* filename) {
FILE* f;
f = fopen(filename,"r");
if (fseek(f, 0, SEEK_SET)) {
printf("Error while accessing the file \n");
exit(1);
}
unsigned short int a,c,d;
unsigned int b,e;
//Read the headers
fread((void*)&a,sizeof(unsigned short int),1,f);
fread((void*)&b,sizeof(unsigned int),1,f);
fread((void*)&c,sizeof(unsigned short int),1,f);
fread((void*)&d,sizeof(unsigned short int),1,f);
fread((void*)&e,sizeof(unsigned int),1,f);
printf("%li %li \n",sizeof(unsigned int),sizeof(unsigned short int));
printf("%hu %u %hu %u %u \n",a,b,c,d,e);
fclose(f);
}
/*
*
* Outputs:
* Solaris:
4 2
16973 905970432 0 0 905969664
* Linux:
4 2
19778 196662 0 0 54
*/
Does anyone know why?
By the way, obviously the files are supposed to be exactly the same file and I know for sure that the output I get when I run this code in linux is the correct one.
Thanks in advance
Carlos |
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|