944,129 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 1190
  • C RSS
Apr 16th, 2006
0

Files in C

Expand Post »
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, 31 views)
File Type: txt input.txt (93 Bytes, 14 views)
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
YoTaMiX is offline Offline
38 posts
since Jan 2006
Apr 16th, 2006
0

Re: Files in C

> 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. }
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Apr 16th, 2006
0

Re: Files in C

Quote 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
Reputation Points: 10
Solved Threads: 0
Light Poster
YoTaMiX is offline Offline
38 posts
since Jan 2006
Apr 16th, 2006
0

Re: Files in C

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.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: problem in concatenation of 2 strings
Next Thread in C Forum Timeline: Help regarded SIP(Session initiation protocol)





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC