Reading from a binary file


 
Thread Tools Search this Thread
Top Forums Programming Reading from a binary file
# 1  
Old 04-22-2002
Question 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
Code:
fread(&file_data,sizeof(struct book_type),1,fileSave);

ive also tried it without the & but i still get the coredump.
thanks!
primal

here is some other information that might be required...
global variable
Code:
struct book_type{
    char title[30];
    float price;
    char authorName[30];
    struct book_type * link;
   };

saving to file
Code:
fwrite(book_head,sizeof(struct book_type),1,fileSave);
fwrite(temp,sizeof(struct book_type),1,fileSave);

function definition
Code:
void display_File_Data(){
     char ch;
     int count=1;
     struct book_type * file_data;
     rewind(fileSave);
     printf("\nSerial Number\tTitle\t\tAuthor\t\tPrice\n");
     do{
        fread(&file_data,sizeof(struct book_type),1,fileSave);
          if(feof(fileSave)){
             break;
            }
          else{
             printf("%d\t\t%s\t%s\t$%2.2f\n",count,file_data->title,file_data->authorName,file_data->price);
             count++;
            }
       }while(!feof(fileSave));
     printf("\nPress enter to continue...");
     ch=getchar();
     getchar();
    }

# 2  
Old 04-22-2002
someone else might have a better answer than me; but when i used to C program (many years ago!) and had memory errors it was often because I tried to read (and write) into memory an amount larger than was allocated and ... a coredump!


Quote:
FREAD()

size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);

DESCRIPTION
The function fread reads nmemb elements of data, each size
bytes long, from the stream pointed to by stream, storing
them at the location given by ptr.
so, perhaps you have not allocated a large enough space that begins at the address in memory that your *ptr is pointing to.....
that would be my first guess....


the storage area (in memory) for file_data must be large enough to store the fread size from stream or you will get a memory error core dump..... how big is your memory area? did you initialize it or malloc some nice space?
# 3  
Old 04-23-2002
Hi

It seems like the core dump is generated at the 2nd printf statement inside your display_File_Data().

You have declared file_data as a pointer to the structure....but didnt allocate memory using malloc/calloc.

Do the necessary dynamic allocation and it should work this time.

Cheers
Deepa
# 4  
Old 04-23-2002
Hi

Just one more addition......

If you have gdb/dbx debugging tool in ur machine, u can get the exact statement where the core was dumped.

Cheers
Deepa
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. Shell Programming and Scripting

Convert binary file to csv and then back to the binary format

Hello *nix specialists, Im working for a non profit organisation in Germany to transport DSL over WLAN to people in areas without no DSL. We are using Linksys WRT 54 router with DD-WRT firmware There are at the moment over 180 router running but we have to change some settings next time. So my... (7 Replies)
Discussion started by: digidax
7 Replies

4. Shell Programming and Scripting

Output redirection of c binary file to a file in shell script is failing

I am struck up with a problem and that is with output redirection. I used all the ways for the redirection of the output of c binary to a file, still it is failing. Here are the different ways which I have used: ./a.out | tee -a /root/tmp.txt 2>&1 ./a.out | tee -a /root/tmp.txt 1>&1 ./a.out |... (2 Replies)
Discussion started by: Maya29988
2 Replies

5. 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

6. Programming

reading binary files

#include <stdio.h> /* typedef struct { char name; 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);... (7 Replies)
Discussion started by: robin_simple
7 Replies

7. UNIX for Dummies Questions & Answers

Pipe binary file matches grep results to file

I am using grep to match a pattern, but the output is strange. $ grep -r -o "pattern" * Gives me: Binary file foo1 matches Binary file foo2 matches Binary file foo3 matches To find the lines before/after, I then have to use the following on each file: $ strings foo1 | grep -A1 -B1... (0 Replies)
Discussion started by: chipperuga
0 Replies

8. 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

9. 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

10. Solaris

compiled binary file gives "cannot execute binary file"

Hi, I have two Solaris machines. 1. SunOS X 5.8 Generic_108528-29 sun4u sparc SUNW,Sun-Blade-1500 2. SunOS Y 5.8 Generic_108528-13 sun4u sparc SUNW,Ultra-60 I am trying to buiild a project on both these machines. The Binary output file compiled on machine 2 runs on both the machines. Where... (0 Replies)
Discussion started by: scgupta
0 Replies
Login or Register to Ask a Question