reading binary files


 
Thread Tools Search this Thread
Top Forums Programming reading binary files
# 1  
Old 04-14-2012
reading binary files

Code:
#include <stdio.h>
/*
typedef struct
{
      char name[40];
      int number;
      float balance;
} acct_info_t;
*/
int main()
{
        FILE *fptr;
        fptr = fopen("acct_info", "r");
        int magic = 5;

        fseek(fptr,3,SEEK_SET);
        fread(&magic,sizeof(int),1,fptr);

        printf("%i\n", magic);
        fclose(fptr);
}

I am trying to read info from a binary file with this code. Nothing I have tried is working.Smilie
# 2  
Old 04-14-2012
What "isn't working" ?
What are you expecting but not getting ?
# 3  
Old 04-14-2012
The file is a file of structs and two integers at the beginning. I am trying to pull something from the file, but have not been able to.
# 4  
Old 04-14-2012
What makes you feel you are not pulling anything from the file ?
My first guess is your seek is wrong (3 is pretty odd for anything magic ...).
This User Gave Thanks to jlliagre For This Post:
# 5  
Old 04-14-2012
I've set fseek to 0, 4, 7, 8 trying to pull one of the first two ints and I've set it as sizeof(acct_info_t), sizeof(acct_info_t) + 7 . . .
I was able to pull info from a txt file I made, but not with this binary file.


The second int is the number of records.
# 6  
Old 04-14-2012
What makes you insisting writing you are not pulling anything from the file while you obviously are ?

Please post the file content, eg:
Code:
od -t u1 acct_info | head -1

and what you expect your code to return.

If you don't get what you expect with a proper seek (like 0 or 4), you might have an endianness issue.
This User Gave Thanks to jlliagre For This Post:
# 7  
Old 04-15-2012
ok, very cool. the ob command is great i'm glad you showed it. i haven't done anything yet, but am feeling better since i saw that the file was empty. i guess i used a write flag when opening at one point. Thanks for the help so far!

---------- Post updated 04-15-12 at 02:31 PM ---------- Previous update was 04-14-12 at 04:36 PM ----------

Code:
#include <stdio.h>
typedef struct
{
      char name[40];
      int number;
      float balance;
} acct_info_t;
int main(int argc, char * argv[])
{
//      printf("Enter record #(number).\n");

        FILE *fptr;
        fptr = fopen("acct_info", "r");
        acct_info_t acct;
        size_t a = sizeof(acct_info_t), b = argc;

        fseek(fptr,8 + a*b,SEEK_SET);
        fread(&acct,a,b,fptr);

        printf("%s\n", acct.name);
        fclose(fptr);
}

This gives me a segmentation fault. Segmentation fault: 11
Any ideas?

---------- Post updated at 02:38 PM ---------- Previous update was at 02:31 PM ----------

Ok, figured it out.
Code:
fread(&acct,a,b,fptr);

The b was screwing things up. I don't have an array, or what I was reading it into was a length of only 1 and if I did more than 1 with b a segmentation fault would occur? Thats kinda cool because it wasn't with an array.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Solaris

Reading binary content

Dear Gurus I am stuck with the peice of work and do not know from where to start. I get a machine generated file which is binary file contain binary data, i want to read binary data as it is without converting into any other format. i want to read byte by byte. Please let me know what... (24 Replies)
Discussion started by: guddu_12
24 Replies

2. Programming

How to replicate Ruby´s binary file reading with Java?

Hello to all guys, Maybe some expert could help me. I have a working ruby script shown below that reads a big binary file (more than 2GB). The chunks of data I want to analyze is separated by the sequence FF47 withing the binary. So, in the ruby script is defined as "line separator" =... (10 Replies)
Discussion started by: Ophiuchus
10 Replies

3. Programming

help with reading a binary file and fseek

this is my code and no matter what record number the user enters i cant get any of the records fields to read into the structure acct. What am i doing wrong? #include <stdio.h> typedef struct { char name; int number; float balance; } acct_info_t; int main (int... (0 Replies)
Discussion started by: bjhum33
0 Replies

4. Shell Programming and Scripting

Binary files

Hi All,Is there anyway I can conver the binary file to ascii. I don't know the binary file format. file command just lists this as "data" file and when I view it has a lot of non-printable characters.Can I write any command equivalent to wc -l to find out the number of rows in the file?Can I use... (4 Replies)
Discussion started by: rahulkav
4 Replies

5. Programming

Reading a binary file in text or ASCII format

Hi All, Please suggest me how to read a binary file in text or ASCII format. thanks Nagendra (3 Replies)
Discussion started by: Nagendra
3 Replies

6. Shell Programming and Scripting

Reading Numerical Binary Data using KSH

Hi, I've searched and couldn't find anyone else with this problem. Is there anyway (preferably using ksh - but other script languages would do) that I can read in binary float data into a text file. The data (arrays from various stages of radar processing) comes in various formats, but mainly... (3 Replies)
Discussion started by: Jonny2Vests
3 Replies

7. Shell Programming and Scripting

search for files excluding binary files

Hi All, I need a solution on my following find command find ./.. -name '*.file' -print BTW This gives me the output as belows ./rtlsim/test/ADCONV0/infile/ad0_dagctst.file ./rtlsim/test/ADCONV0/user_command.file ./rtlsim/test/ADCONV0/simv.daidir/scsim.db.dir/scsim.db.file... (2 Replies)
Discussion started by: user_prady
2 Replies

8. UNIX for Dummies Questions & Answers

Binary Files

Here's the problem... I'm using a simulator on UNIX, and it requires a filename where bits are stored, it should read them out and do whatever with them at that point.. So what i'm trying to do is make a binary file on UNIX. On my PC i can use MSDEV, or any of my C++ compilers to generate a... (2 Replies)
Discussion started by: wcRandThor
2 Replies

9. Programming

Reading from a binary file

I'm having trouble with reading information back into a program from a binary file. when i try to display the contents of the file i get a Memory fault(coredump). would anyone be able to assist? this is my fread line fread(&file_data,sizeof(struct book_type),1,fileSave); ive also tried it without... (3 Replies)
Discussion started by: primal
3 Replies

10. UNIX for Dummies Questions & Answers

Binary Files

Does any one know how to view a binary file as it is (in 1s and 0s) on unix environment? (1 Reply)
Discussion started by: devildivine
1 Replies
Login or Register to Ask a Question