C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old July 24th, 2007, 09:17 AM
everolth everolth is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2007
Posts: 2 everolth User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #2  
Old July 26th, 2007, 09:23 AM
everolth everolth is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2007
Posts: 2 everolth User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > File IO linux and solaris


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.

© 2003-2014 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap