The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM


High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Binary txt file received when i use uuencode to send txt file as attachment ash22 UNIX for Dummies Questions & Answers 2 04-24-2008 05:03 PM
compiled binary file gives "cannot execute binary file" scgupta SUN Solaris 0 07-12-2006 10:59 PM
binary file markms UNIX for Dummies Questions & Answers 1 04-07-2006 06:40 AM
binary file ramneek UNIX for Dummies Questions & Answers 1 09-13-2005 02:02 PM
Reading a binary file in text or ASCII format Nagendra High Level Programming 2 09-07-2005 08:08 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 04-22-2002
Registered User
 

Join Date: Oct 2001
Location: Toronto, Ontario, Canada
Posts: 34
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();
    }
Reply With Quote
Forum Sponsor
  #2  
Old 04-22-2002
Neo's Avatar
Neo Neo is offline
Administrator
 

Join Date: Sep 2000
Location: Asia Pacific
Posts: 4,483
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?
Reply With Quote
  #3  
Old 04-22-2002
Registered User
 

Join Date: Apr 2002
Posts: 53
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
Reply With Quote
  #4  
Old 04-22-2002
Registered User
 

Join Date: Apr 2002
Posts: 53
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
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 12:59 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0