Files in C

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jan 2006
Posts: 38
Reputation: YoTaMiX is an unknown quantity at this point 
Solved Threads: 0
YoTaMiX YoTaMiX is offline Offline
Light Poster

Files in C

 
0
  #1
Apr 16th, 2006
Hello to you all ,

I am having a little trouble in C , working with Files.
for some reason , it doesnt copy to output file the first letter and jumps
over 3 digits of the last number in the file .

whats wrong , i cant find it .

yotam

P.S - source and Input.txt included
Attached Files
File Type: c HW2.C (2.2 KB, 10 views)
File Type: txt input.txt (93 Bytes, 6 views)
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Files in C

 
0
  #2
Apr 16th, 2006
> 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
  1. int make_file(FILE *in,university *un) {
  2. char buff[BUFSIZ];
  3. int count=0;
  4.  
  5. while ( fgets( buff, sizeof buff, in ) != NULL ) {
  6. student s;
  7. if ( sscanf( buff, "%s %ld %f %ld",
  8. s.name, &s.id, &s.avg, &s.hw_submit ) == 4 ) {
  9. /* success decode, extend array and copy the info */
  10. void *temp = realloc( un->ptr, (count+1)*sizeof(student) );
  11. if ( temp != NULL ) {
  12. un->ptr = temp; /* update array */
  13. un->ptr[count] = s; /* copy data */
  14. count++; /* one more stored */
  15. } else {
  16. /* no more room, return with what we have */
  17. return count;
  18. }
  19. } else {
  20. /* that line didn't make sense, report it */
  21. fprintf( stderr, "Bad line %s", buff );
  22. }
  23. }
  24. return count;
  25. }
Reply With Quote Quick reply to this message  
Join Date: Jan 2006
Posts: 38
Reputation: YoTaMiX is an unknown quantity at this point 
Solved Threads: 0
YoTaMiX YoTaMiX is offline Offline
Light Poster

Re: Files in C

 
0
  #3
Apr 16th, 2006
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
  1. int make_file(FILE *in,university *un) {
  2. char buff[BUFSIZ];
  3. int count=0;
  4.  
  5. while ( fgets( buff, sizeof buff, in ) != NULL ) {
  6. student s;
  7. if ( sscanf( buff, "%s %ld %f %ld",
  8. s.name, &s.id, &s.avg, &s.hw_submit ) == 4 ) {
  9. /* success decode, extend array and copy the info */
  10. void *temp = realloc( un->ptr, (count+1)*sizeof(student) );
  11. if ( temp != NULL ) {
  12. un->ptr = temp; /* update array */
  13. un->ptr[count] = s; /* copy data */
  14. count++; /* one more stored */
  15. } else {
  16. /* no more room, return with what we have */
  17. return count;
  18. }
  19. } else {
  20. /* that line didn't make sense, report it */
  21. fprintf( stderr, "Bad line %s", buff );
  22. }
  23. }
  24. return count;
  25. }
I did so , however , only the first name was fixed .... how bizzare.
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
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Files in C

 
0
  #4
Apr 16th, 2006
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC