Sponsored Content
Top Forums Programming help with reading a binary file and fseek Post 302624781 by bjhum33 on Monday 16th of April 2012 09:54:31 PM
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..
 

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

10. 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
ACCT(2) 						      BSD System Calls Manual							   ACCT(2)

NAME
acct -- enable or disable process accounting SYNOPSIS
#include <unistd.h> int acct(const char *file); DESCRIPTION
The acct() call enables or disables the collection of system accounting records. If the argument file is a nil pointer, accounting is dis- abled. If file is an existing pathname (null-terminated), record collection is enabled and for every process initiated which terminates under normal conditions an accounting record is appended to file. Abnormal conditions of termination are reboots or other fatal system prob- lems. Records for processes which never terminate can not be produced by acct(). For more information on the record structure used by acct(), see /usr/include/sys/acct.h and acct(5). This call is permitted only to the super-user. NOTES
Accounting is automatically disabled when the file system the accounting file resides on runs out of space; it is enabled when space once again becomes available. RETURN VALUES
On error -1 is returned. The file must exist and the call may be exercised only by the super-user. ERRORS
Acct() will fail if one of the following is true: [EPERM] The caller is not the super-user. [ENOTDIR] A component of the path prefix is not a directory. [ENAMETOOLONG] A component of a pathname exceeded {NAME_MAX} characters, or an entire path name exceeded {PATH_MAX} characters. [ENOENT] The named file does not exist. [EACCES] Search permission is denied for a component of the path prefix, or the path name is not a regular file. [ELOOP] Too many symbolic links were encountered in translating the pathname. [EROFS] The named file resides on a read-only file system. [EFAULT] File points outside the process's allocated address space. [EIO] An I/O error occurred while reading from or writing to the file system. SEE ALSO
acct(5), sa(8) HISTORY
An acct() function call appeared in Version 7 AT&T UNIX. 4th Berkeley Distribution June 4, 1993 4th Berkeley Distribution
All times are GMT -4. The time now is 07:20 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy