help with reading a binary file and fseek


 
Thread Tools Search this Thread
Top Forums Programming help with reading a binary file and fseek
# 1  
Old 04-16-2012
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?

Code:
 
#include <stdio.h>
typedef struct
{
        char name[40];
        int number;
        float balance;
} acct_info_t;
int main (int argc, char *argv[])
{
        FILE *ptr_acctfile;
        acct_info_t acct;
        ptr_acctfile = fopen("/accounts/facstaff/bhatias/cs2750/acct_info", "rb");
        if (!ptr_acctfile)
        {
                printf("Unable to open file!\n");
                return 1;
        }
        printf("Enter a record number: ");
        int recnum;
        scanf("%d", &recnum);
        fseek(ptr_acctfile, sizeof(acct_info_t)*recnum, SEEK_SET);
        fread(&acct, sizeof(acct_info_t), 1, ptr_acctfile);
        printf("%s, %d\n", acct.name, acct.balance);
        fclose(ptr_acctfile);
        return 0;
}

ok also i guess im getting odd results since there are 2 integers i have to acount for in the beggining of the file. how would i get past these with an fseek?
Moderator's Comments:
Mod Comment homework and classwork belong in the homework forum, please post there

Last edited by jim mcnamara; 04-16-2012 at 11:31 PM..
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. 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

5. Programming

help with fseek

Hi, I working a c project in IBM AIX. I have a requirement like this. I have some contents in *temp. I am writing the contents of *temp to a file pointer ftemp (*ftemp for an tmp file tmpfile.txt) Now I want to read the contents of *ftemp in the reverse order an dneed to print it in the... (2 Replies)
Discussion started by: ramkrix
2 Replies

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

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

8. Programming

q on fseek() & ftell() + tga files

In determining the true file size of a .tga file is the fseek(fptr,0,SEEK_END); file_size = ftell(fptr); combination reliable ? The reason Im asking is because the value of file_size is in agreement with doing a wc -c mytga.tga however when I get the value of the extension... (2 Replies)
Discussion started by: JamesGoh
2 Replies

9. Programming

error when using fseek() function

The errors EBADF & ESPIPE occur at this fseek call. Does anybody know how to solve this problem? Thanks in advance. toFileStream=fdopen(localFileDes,"ra+"); if(fseek(toFileStream, 0, SEEK_END)!=0){ if(errno==EBADF) printf("errno==EBADF\n"); if(errno==EINVAL)... (3 Replies)
Discussion started by: ivancheung
3 Replies

10. 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
Login or Register to Ask a Question