943,155 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 999
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Feb 8th, 2010
0

Buffer fwrite problem Pls Help!

Expand Post »
I am having issues with outputting a string correctly that is coming from a 4K read buffer. Here is the code I have currently:

read_4K.c (cannot be modified)
  1. int read_4K(char *b)
  2. {
  3. return read(0, b, 4096);
  4. }

reader.c
  1. static char Buffer[4096];
  2. static int Bend = 0;
  3. static int Bptr = 0;
  4.  
  5. int read_string(char *s,int size){
  6.  
  7. if(Bptr >= Bend){
  8. Bend = read_4K(Buffer);
  9. if(!Bend){
  10. return 0;
  11. }
  12. Bptr = 0;
  13. }
  14.  
  15. strncpy(s, &Buffer[Bptr], size);
  16. Bptr += size;
  17.  
  18. }

bcat.c
  1. main(int argc, char **argv)
  2. {
  3. int i;
  4. char *str;
  5. int bsize;
  6.  
  7. if (argc != 2 || sscanf(argv[1], "%d", &bsize) == 0 || bsize <= 0) {
  8. fprintf(stderr, "usage: bcat strsize\n");
  9. }
  10.  
  11. str = (char *) malloc(sizeof(char)*bsize);
  12. if (str == NULL) { perror("malloc str"); exit(1); }
  13.  
  14. while (1) {
  15. i = read_string(str, bsize);
  16. if (i == 0) exit(0);
  17. fwrite(str, 1, i, stdout);
  18. }
  19. }

When I execute this I get the following output

> ./bcat2 10 < input1.txt
Up and down the puppñies' hair ñ
Fleas andñ ticks jumñp everywheñre
'Causeñ of originñal sin

input1.txt:
Up and down the puppies' hair
Fleas and ticks jump everywhere
'Cause of original sin


I can't figure out why I am getting these garbage characters after the first read from the buffer... Can anyone help?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sfreema4 is offline Offline
1 posts
since Feb 2010
Feb 8th, 2010
-2

fflush

Try to use fflush(stdin) before reading. It will flush the standard input(keyboard)

Click to Expand / Collapse  Quote originally posted by sfreema4 ...
I am having issues with outputting a string correctly that is coming from a 4K read buffer. Here is the code I have currently:

read_4K.c (cannot be modified)
  1. int read_4K(char *b)
  2. {
  3. return read(0, b, 4096);
  4. }

reader.c
  1. static char Buffer[4096];
  2. static int Bend = 0;
  3. static int Bptr = 0;
  4.  
  5. int read_string(char *s,int size){
  6.  
  7. if(Bptr >= Bend){
  8. Bend = read_4K(Buffer);
  9. if(!Bend){
  10. return 0;
  11. }
  12. Bptr = 0;
  13. }
  14.  
  15. strncpy(s, &Buffer[Bptr], size);
  16. Bptr += size;
  17.  
  18. }

bcat.c
  1. main(int argc, char **argv)
  2. {
  3. int i;
  4. char *str;
  5. int bsize;
  6.  
  7. if (argc != 2 || sscanf(argv[1], "%d", &bsize) == 0 || bsize <= 0) {
  8. fprintf(stderr, "usage: bcat strsize\n");
  9. }
  10.  
  11. str = (char *) malloc(sizeof(char)*bsize);
  12. if (str == NULL) { perror("malloc str"); exit(1); }
  13.  
  14. while (1) {
  15. i = read_string(str, bsize);
  16. if (i == 0) exit(0);
  17. fwrite(str, 1, i, stdout);
  18. }
  19. }

When I execute this I get the following output

> ./bcat2 10 < input1.txt
Up and down the puppñies' hair ñ
Fleas andñ ticks jumñp everywheñre
'Causeñ of originñal sin

input1.txt:
Up and down the puppies' hair
Fleas and ticks jump everywhere
'Cause of original sin


I can't figure out why I am getting these garbage characters after the first read from the buffer... Can anyone help?
Reputation Points: 6
Solved Threads: 0
Newbie Poster
srrid.ajmal is offline Offline
2 posts
since Jun 2008
Feb 8th, 2010
1
Re: Buffer fwrite problem Pls Help!
It looks like you need to null-terminate your strings.
Reputation Points: 29
Solved Threads: 0
Newbie Poster
tmoorez06 is offline Offline
6 posts
since Jan 2010
Feb 8th, 2010
0
Re: Buffer fwrite problem Pls Help!
Try to use fflush(stdin) before reading. It will flush the standard input(keyboard)
Never, NEVER, NEVER use fflush(stdin) . Here's why.
Moderator
Reputation Points: 3275
Solved Threads: 889
Posting Sage
WaltP is offline Offline
7,704 posts
since May 2006
Feb 8th, 2010
1
Re: Buffer fwrite problem Pls Help!
Are you guys on autopilot again? Beg to explain how null termination is related to the OP problem.

The real problem is that read_string doesn't return anything when it should.
Reputation Points: 707
Solved Threads: 185
Practically a Posting Shark
nezachem is offline Offline
843 posts
since Dec 2009
Feb 8th, 2010
0
Re: Buffer fwrite problem Pls Help!
Click to Expand / Collapse  Quote originally posted by nezachem ...
Are you guys on autopilot again? Beg to explain how null termination is related to the OP problem.
I would guess that it had very much to do with the OP's description of the issue:
Click to Expand / Collapse  Quote originally posted by sfreema4 ...
When I execute this I get the following output

> ./bcat2 10 < input1.txt
Up and down the puppñies' hair ñ
Fleas andñ ticks jumñp everywheñre
'Causeñ of originñal sin

input1.txt:
Up and down the puppies' hair
Fleas and ticks jump everywhere
'Cause of original sin


I can't figure out why I am getting these garbage characters after the first read from the buffer...
See that garbage character every 10? Which coincides with the size limit with the call to strncpy? Which doesn't automatically null terminate?

Just a guess.
Click to Expand / Collapse  Quote originally posted by nezachem ...
The real problem is that read_string doesn't return anything when it should.
I don't think the OP posted enough of the code to reproduce the problem. Or at least not reliably on different systems. But it would appear that on the OP's system that input is being obtained.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Feb 8th, 2010
0
Re: Buffer fwrite problem Pls Help!
See that garbage character every 10? Which coincides with the size limit with the call to strncpy? Which doesn't automatically null terminate?
I sure do. I also see that the output is produced with fwrite(str, 1, i, stdout) , which cares not of null termination. I am absolutely sure that the counter i is plain wrong.
Reputation Points: 707
Solved Threads: 185
Practically a Posting Shark
nezachem is offline Offline
843 posts
since Dec 2009
Feb 8th, 2010
0
Re: Buffer fwrite problem Pls Help!
Click to Expand / Collapse  Quote originally posted by nezachem ...
I sure do. I also see that the output is produced with fwrite(str, 1, i, stdout) , which cares not of null termination. I am absolutely sure that the counter i is plain wrong.
Well, I'll defer to that since my I/O looks like this:
Quote ...
D:\projects\misc\c>debug\capp 10 < file.txt

D:\projects\misc\c>
[edit]But indeed read_string should return a value.
[edit=2]Let me instead say that read_string probably really needs a fair rewrite. Handling a rollover of say 4090 to 4100, so adjusting the size. Plus returning how many bytes are really copied. And maybe using memcpy instead of strncpy. Maybe.
Last edited by Dave Sinkula; Feb 8th, 2010 at 5:07 pm.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Feb 8th, 2010
0
Re: Buffer fwrite problem Pls Help!
Anytime strncpy is used, it is up to you to make sure the string is terminated properly.
Reputation Points: 29
Solved Threads: 0
Newbie Poster
tmoorez06 is offline Offline
6 posts
since Jan 2010
Feb 8th, 2010
0
Re: Buffer fwrite problem Pls Help!
Dave- I can very much relate to your tagline "Long time no C"!

Nezachem - I don't mean to imply, by my terse answers, that I have all the answers to the problem. I was merely pointing out one potentential problem that caught my eye. You might have a valid point, however; whenever I see output such as what the OP supplied, and see a call to strncpy without explicit termination of the destination string (which is the input for fwrite: Garbage In Garbage Out) I see a red flag.
I would feel much more comfortable if I saw something like
destination[strlen(source)] = 0, after the call to strncpy
It's been a while cause like Dave says... "long time no C".
I do miss C, though.
Reputation Points: 29
Solved Threads: 0
Newbie Poster
tmoorez06 is offline Offline
6 posts
since Jan 2010

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: Authenticating and authorizing
Next Thread in C Forum Timeline: Code Request for Optimization





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


Follow us on Twitter


© 2011 DaniWeb® LLC