| | |
Files in C
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
> void main
main returns int
> char user_choice;
> while (user_choice!='z')
This variable is uninitialised at the point you first use it.
> university univ;
This isn't initialised either.
Which is very important when you get to make_file(), since you do
- dereference an uninitialised pointer
- try and realloc an uninitialised pointer
> if (un->ptr==NULL) un->ptr=(student*)realloc(un->ptr,(count+1)*sizeof(student));
The test serves no purpose, since at best it only extends the array once.
In main(), you need this to start off with a NULL pointer
And this function needs to be something like
main returns int
> char user_choice;
> while (user_choice!='z')
This variable is uninitialised at the point you first use it.
> university univ;
This isn't initialised either.
Which is very important when you get to make_file(), since you do
- dereference an uninitialised pointer
- try and realloc an uninitialised pointer
> if (un->ptr==NULL) un->ptr=(student*)realloc(un->ptr,(count+1)*sizeof(student));
The test serves no purpose, since at best it only extends the array once.
In main(), you need this to start off with a NULL pointer
university univ = { 0 };And this function needs to be something like
C Syntax (Toggle Plain Text)
int make_file(FILE *in,university *un) { char buff[BUFSIZ]; int count=0; while ( fgets( buff, sizeof buff, in ) != NULL ) { student s; if ( sscanf( buff, "%s %ld %f %ld", s.name, &s.id, &s.avg, &s.hw_submit ) == 4 ) { /* success decode, extend array and copy the info */ void *temp = realloc( un->ptr, (count+1)*sizeof(student) ); if ( temp != NULL ) { un->ptr = temp; /* update array */ un->ptr[count] = s; /* copy data */ count++; /* one more stored */ } else { /* no more room, return with what we have */ return count; } } else { /* that line didn't make sense, report it */ fprintf( stderr, "Bad line %s", buff ); } } return count; }
•
•
Join Date: Jan 2006
Posts: 38
Reputation:
Solved Threads: 0
•
•
•
•
Originally Posted by Salem
> void main
main returns int
> char user_choice;
> while (user_choice!='z')
This variable is uninitialised at the point you first use it.
> university univ;
This isn't initialised either.
Which is very important when you get to make_file(), since you do
- dereference an uninitialised pointer
- try and realloc an uninitialised pointer
> if (un->ptr==NULL) un->ptr=(student*)realloc(un->ptr,(count+1)*sizeof(student));
The test serves no purpose, since at best it only extends the array once.
In main(), you need this to start off with a NULL pointer
university univ = { 0 };
And this function needs to be something like
C Syntax (Toggle Plain Text)
int make_file(FILE *in,university *un) { char buff[BUFSIZ]; int count=0; while ( fgets( buff, sizeof buff, in ) != NULL ) { student s; if ( sscanf( buff, "%s %ld %f %ld", s.name, &s.id, &s.avg, &s.hw_submit ) == 4 ) { /* success decode, extend array and copy the info */ void *temp = realloc( un->ptr, (count+1)*sizeof(student) ); if ( temp != NULL ) { un->ptr = temp; /* update array */ un->ptr[count] = s; /* copy data */ count++; /* one more stored */ } else { /* no more room, return with what we have */ return count; } } else { /* that line didn't make sense, report it */ fprintf( stderr, "Bad line %s", buff ); } } return count; }
1. what does "buff" stands for , after all it has no Value.
2.BUFSIZ shoulde be defined or resized each reading from source file?
3.how can get rid of the double menu display?
4.is there a way to write to target file so it wont skip chars?
thanx salem
1. what does "buff" stands for , after all it has no Value.
buff is short for buffer.
2.BUFSIZ shoulde be defined or resized each reading from source file?
No, it is a fixed constant declared in stdio.h (for an ANSI-C compiler anyway).
You are using an ANSI-C compiler and not some ancient fossil like TurboC.
3.how can get rid of the double menu display?
Don't use getchar(), scanf(), getc() to read a single character.
Use fgets() to read a line from stdin, then read what you need from the buffer.
It's just like my use of fgets() in the previous post.
4.is there a way to write to target file so it wont skip chars?
I've no idea - post the problem code.
buff is short for buffer.
2.BUFSIZ shoulde be defined or resized each reading from source file?
No, it is a fixed constant declared in stdio.h (for an ANSI-C compiler anyway).
You are using an ANSI-C compiler and not some ancient fossil like TurboC.
3.how can get rid of the double menu display?
Don't use getchar(), scanf(), getc() to read a single character.
Use fgets() to read a line from stdin, then read what you need from the buffer.
It's just like my use of fgets() in the previous post.
4.is there a way to write to target file so it wont skip chars?
I've no idea - post the problem code.
![]() |
Similar Threads
- hjsplit doenst work for avi files? (Windows NT / 2000 / XP)
- "Save Target As.." isn't working in IE6 (Web Browsers)
- Does Samba send deleted files to a recycle bin? (*nix Software)
- problems wid cpp files (C++)
- Dia, .PDB files (C++)
- Cannot transfer files from one hardrive to another =[ please help (Windows NT / 2000 / XP)
- FTP files (Geeks' Lounge)
Other Threads in the C Forum
- Previous Thread: problem in concatenation of 2 strings
- Next Thread: Help regarded SIP(Session initiation protocol)
| Thread Tools | Search this Thread |
adobe ansi api array arrays asterisks binarysearch calculate centimeter char convert copyimagefile copypdffile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax directory dynamic fflush file fork forloop frequency getlasterror givemetehcodez graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators infiniteloop initialization interest kernel km linked linkedlist linux linuxsegmentationfault list lists locate logical_drives match matrix microsoft motherboard multi mysql number open opendocumentformat opensource owf pattern pdf performance pointer pointers posix power problem probleminc program programming pyramidusingturboccodes radix read recursion recv repetition research scanf scheduling scripting segmentationfault send sequential shape socketprograming stack standard string strings structures systemcall testautomation turboc unix user variable voidmain() wab win32api windows.h







