LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 08-07-2010, 09:06 AM   #1
swatihurde
LQ Newbie
 
Registered: Aug 2010
Posts: 2

Rep: Reputation: 0
Question binary File reading in c


Hello,
I am trying to read binary file in c but my code is not working properly.
I am not getting the same output as that in file. you can view my code below.


int i=0;
void main()
{
FILE *fp;
int* buffer;
unsigned long fileLen;

int numread=0;
// int numread=0;
fp=fopen("bindata1.txt","rb"); //reading transmitted baseband data from a file
if(fp==NULL)
{
puts("cannot open file");
exit(0);
}

else
{
fseek(fp, 1, SEEK_END);
fileLen=ftell(fp);
fseek(fp, 1, SEEK_SET);

buffer=(int*) malloc(fileLen);
numread=fread(buffer,1,fileLen,fp);//1 indicate 1 byte i.e. 8 bits of data.
for(i=0;i<=2;i++)
{
printf("%b",&buffer[i]);

// printf("%d\n",numread);
}

fclose(fp);
}
getch();
}
Kindly tell me my mistake.
Thanks
 
Old 08-07-2010, 09:27 AM   #2
DJ Shaji
Member
 
Registered: Dec 2004
Location: Yo Momma's house
Distribution: Fedora Rawhide, ArchLinux
Posts: 518
Blog Entries: 15

Rep: Reputation: 106Reputation: 106
First Of all, why is main void? How will you communicate exit status?

On modern systems, binary and "normal" reading are the same. The "b" has no effect.
 
Old 08-07-2010, 11:43 AM   #3
estabroo
Senior Member
 
Registered: Jun 2008
Distribution: debian, ubuntu, sidux
Posts: 1,126
Blog Entries: 2

Rep: Reputation: 124Reputation: 124
I'm guessing your off by one? the stream starts at 0 and you started at 1
fseek(fp, 1, SEEK_SET);
should be
fseek(fp, 0, SEEK_SET);
 
Old 08-07-2010, 01:57 PM   #4
imitheos
Member
 
Registered: May 2005
Location: Greece
Posts: 441

Rep: Reputation: 141Reputation: 141
Quote:
Originally Posted by swatihurde View Post
int i=0;
void main()
As DJ Shaji said, main should be declared as
Code:
int main(void)

Quote:
Originally Posted by swatihurde View Post
{
if(fp==NULL)
{
puts("cannot open file");
exit(0);
}

else
{
Since you exit when the fp is NULL, there is no need for the else clause.
Quote:
Originally Posted by swatihurde View Post
buffer=(int*) malloc(fileLen);
malloc returns a void pointer. In C there is a implicit conversion from
void to any pointer, so if the program will be compiled using a C
compiler (and not a C++ compiler), the cast is not needed.
Also, you should always check if malloc has actually allocated any
memory. Your file may be large or it can fail for any other reason.

Quote:
Originally Posted by swatihurde View Post
for(i=0;i<=2;i++)
{
printf("%b",&buffer[i]);
I don't understand what do you want to print here, but anyway "%b" is
not in the standard. I think that Visual studio implements it, but
your program won't be portable.

I hope i helped
 
Old 08-08-2010, 08:41 AM   #5
swatihurde
LQ Newbie
 
Registered: Aug 2010
Posts: 2

Original Poster
Rep: Reputation: 0
Thanks sir, Now its working .
 
  


Reply



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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Reading binary file in Python pwc101 Programming 4 04-28-2010 04:01 AM
Reading text file-writting binary file cdog Programming 5 06-13-2006 11:56 AM
Reading and Writing integers to binary file oulevon Programming 2 02-26-2006 12:27 AM
Binary reading/writing in cpp kornerr Programming 1 11-27-2005 07:09 PM
problem in reading Microsoft word as a binary file ljqu_happy Programming 15 02-02-2005 10:10 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 12:56 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration