473,664 Members | 3,035 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

the buffer question of the stdio

i have written a small program, it turns out to be wrong,
while(read()!=E OF){
read();
read();
read();

}
so,when read==EOF,the next read() will read a -1, and the program will go
infinitely.

but,when i use ^c to abort the program,the output file's size is
always 4096 bytes.why? notice that the program is always writing
that file. the buffer should be flushed again and again.
--

My name is sunway
Jan 11 '06 #1
9 1723
sunway wrote:
i have written a small program, it turns out to be wrong,
while(read()!=E OF){
read();
read();
read();

}
so,when read==EOF,the next read() will read a -1, and the program will go
infinitely.

but,when i use ^c to abort the program,the output file's size is
always 4096 bytes.why? notice that the program is always writing
that file. the buffer should be flushed again and again.


1: There is no read() function in C. <OT> There is
a read() function in POSIX, but it takes three arguments
rather than zero and doesn't return EOF at end-of-file. </OT>

2: I don't "notice that the program is always writing"
anything at all. There are no output operations anywhere in
what you've shown. <OT> Unless, of course, the non-POSIX
read() function you've implemented performs output. </OT>

3: Nothing in the code you've shown has anything to do
with stdio.

4: <OT> Nothing in the code you've shown has anything to
do with buffering. </OT>

5: The fact that your program "turns out to be wrong" does
not surprise me in the slightest. You don't have the faintest
idea of what you're doing. And, given the non-information
you've provided, neither do I.

--
Eric Sosman
es*****@acm-dot-org.invalid
Jan 11 '06 #2
sorry,i have shown only a brief of the code
the code is not written by me,a fellow give it to me and ask me for help

here is the total program:

#include "stdio.h"
#include "stdlib.h"
#include "math.h"
main(){
int opt,i;
char ch[4],cht,*p;
//p=ch;
FILE *fpin,*fpout;
int ch_opt(char ch_option);
/**************/

/**************/
fpin=fopen("bli nk.hex", "rb");
fpout=fopen("bl ink.dat","w");
/**************/
while(cht!=EOF) {
~~~~~~~here is the problem,the program will go infinitely,but
the output file"blink.dat" would not larger than 4096

for(i=0;i<4;){
cht=fgetc(fpin) ;
opt=ch_opt(cht) ;
if(opt != 3){
ch[i]=cht;
i++;}

//printf("%d ",opt);
//printf("%d\n",c ht);
//getchar();
}
i=0;
fputc('0',fpout );
fputc('x',fpout );
fputc(ch[0],fpout);
fputc(ch[1],fpout);
fputc(ch[2],fpout);
fputc(ch[3],fpout);
fputc(',',fpout );
}
//return 0;
}

int ch_opt(char ch_option)
{
if(ch_option >= '0' && ch_option <='9')
return 1;
else if(ch_option >= 'A' && ch_option <='Z')
return 2;
else
return 3;
}
--

free thought,free world
Jan 11 '06 #3
sunway wrote:
sorry,i have shown only a brief of the code
the code is not written by me,a fellow give it to me and ask me for help

here is the total program:

#include "stdio.h"
#include "stdlib.h"
#include "math.h"
main(){
int opt,i;
char ch[4],cht,*p;
You must make `cht' an *int*, otherwise it can *never* be equal to EOF. //p=ch;
FILE *fpin,*fpout;
int ch_opt(char ch_option); Function prototypes should usually be at file scope. /**************/

/**************/
fpin=fopen("bli nk.hex", "rb");
fpout=fopen("bl ink.dat","w");
/**************/
while(cht!=EOF) { Here, `cht' is uninitialized and could contain *anything* (well,
anything that will fit in a `char' -- which, as mentioned above,
excludes EOF. ~~~~~~~here is the problem,the program will go infinitely,but
the output file"blink.dat" would not larger than 4096

for(i=0;i<4;){
cht=fgetc(fpin) ;
opt=ch_opt(cht) ;
if(opt != 3){
ch[i]=cht;
i++;}

//printf("%d ",opt);
//printf("%d\n",c ht);
//getchar();
}
i=0;
fputc('0',fpout );
fputc('x',fpout );
fputc(ch[0],fpout);
fputc(ch[1],fpout);
fputc(ch[2],fpout);
fputc(ch[3],fpout);
fputc(',',fpout );
}
//return 0;
}

int ch_opt(char ch_option)
{
if(ch_option >= '0' && ch_option <='9')
return 1;
else if(ch_option >= 'A' && ch_option <='Z')
This is not guaranteed to work the way you want (though it will if your
character set is ASCII). return 2;
else
return 3;
}


HTH,
--ag
--
Artie Gold -- Austin, Texas
http://goldsays.blogspot.com
http://www.cafepress.com/goldsays
"If you have nothing to hide, you're not trying!"
Jan 11 '06 #4
Artie Gold wrote:
sunway wrote:
sorry,i have shown only a brief of the code
the code is not written by me,a fellow give it to me and ask me for help

here is the total program:

#include "stdio.h" ....and this should be #include <stdio.h> #include "stdlib.h" #include <stdlib.h> #include "math.h" ....and you're not using this header main(){
int opt,i;
char ch[4],cht,*p;

[snip]

--ag

--
Artie Gold -- Austin, Texas
http://goldsays.blogspot.com
http://www.cafepress.com/goldsays
"If you have nothing to hide, you're not trying!"
Jan 11 '06 #5
On 2006-01-11, Artie Gold <ar*******@aust in.rr.com> wrote:
#include "stdio.h" ...and this should be #include <stdio.h> #include "stdlib.h"

#include <stdlib.h>


Yes, it should, but it's not required to.
Jan 11 '06 #6
Jordan Abel <ra*******@gmai l.com> writes:
On 2006-01-11, Artie Gold <ar*******@aust in.rr.com> wrote:
#include "stdio.h"

...and this should be #include <stdio.h>
#include "stdlib.h"

#include <stdlib.h>


Yes, it should, but it's not required to.


According to the standard,
#include <foo.h>
searches for a "header" (which isn't necessarily a file) in a sequence
of implementation-defined places.
#include "foo.h"
searches for a file named "foo.h" in an implementation-defined manner.
If that search fails or is not supported, the directive is then
reprocessed as if it were
#include <foo.h>

So
#include "stdio.h"
is *nearly* equivalent to
#include <stdio.h>
but if there happens to be a file named "stdio.h" somewhere in the
search path, that file will be included instead of the standard
header. Using the <> form avoids this possibility.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jan 11 '06 #7
On 2006-01-11, Keith Thompson <ks***@mib.or g> wrote:
Jordan Abel <ra*******@gmai l.com> writes:
On 2006-01-11, Artie Gold <ar*******@aust in.rr.com> wrote:
> #include "stdio.h"
...and this should be #include <stdio.h>
> #include "stdlib.h"
#include <stdlib.h>


Yes, it should, but it's not required to.


According to the standard,
#include <foo.h>
searches for a "header" (which isn't necessarily a file) in a sequence
of implementation-defined places.
#include "foo.h"
searches for a file named "foo.h" in an implementation-defined manner.
If that search fails or is not supported, the directive is then
reprocessed as if it were
#include <foo.h>

So
#include "stdio.h"
is *nearly* equivalent to
#include <stdio.h>
but if there happens to be a file named "stdio.h" somewhere in the
search path, that file will be included instead of the standard
header. Using the <> form avoids this possibility.


There can be benefits to using "", though - say, have #include
"stdbool.h" , and conditionally generate a local header that contains the
appropriate definitions for pre-c99 systems
Jan 11 '06 #8
Artie Gold wrote:
sunway wrote:
char ch[4],cht,*p;
while(cht!=EOF) {


You must make `cht' an *int*, otherwise it can *never* be equal to EOF.


Actually it can be equal to EOF. EOF can be any negative value
but is usually -1, and -1 is in the range of char if char is signed.

The problem with 'cht' being a char is that it cannot correctly
represent values greater than CHAR_MAX, and in the OP's
case, if getchar() returns 255 then cht will end up with the
value -1, which will test equal to EOF.

The OP should change ch[] to 'int' as well.

Jan 12 '06 #9
"Old Wolf" <ol*****@inspir e.net.nz> writes:
Artie Gold wrote:
sunway wrote:
char ch[4],cht,*p;
while(cht!=EOF) {


You must make `cht' an *int*, otherwise it can *never* be equal to EOF.


Actually it can be equal to EOF. EOF can be any negative value
but is usually -1, and -1 is in the range of char if char is signed.

The problem with 'cht' being a char is that it cannot correctly
represent values greater than CHAR_MAX, and in the OP's
case, if getchar() returns 255 then cht will end up with the
value -1, which will test equal to EOF.


That's the problem with 'cht' being a char if plain char happens to be
signed. If plain char is unsigned, the problem is that it can never
be equal to EOF. (I'm deliberately ignoring the possibility of
sizeof(int)==1. )

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jan 12 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1604
by: gaool | last post by:
Hello, I wrote a python script (python 2.3.3) to send some commands to a programm and to show on the screen the responses of this programm. I use "Pipedream.py" module in my script to speak with the programm (under unix system, it uses fdopen() to create a pipe and to send the specified string. fdopen has a buffer size argument).So I can specify a buffer size when I send a command with the pipedream module. On windows it works but on...
28
3864
by: bwaichu | last post by:
Is it generally better to set-up a buffer (fixed sized array) and read and write to that buffer even if it is larger than what is being written to it? Or is it better to allocate memory and realloc it for the size of the what is being written each time? In other words, what is the decision factor between deciding to use a fixed size buffer or allocating memory space and reallocing the size? I don't think the code below is optimal...
16
38611
by: junky_fellow | last post by:
Is there any efficcient way of removing the newline character from the buffer read by fgets() ? Is there any library function that is similar to fgets() but also tells how many bytes it read into the buffer ?
6
4672
by: vaidehikedlaya | last post by:
Hello, I am using gmp.h library. I am trying to put mpz_t variable into char buffer and back. I came across mpz_import/mpz_export to suffice this purpose. But, I am not very clear about using these functions. I am not getting the right result for the program below. In the program I have included the code(present in the gmp document, in the description of mpz_export function) numb = 8*size - nail;
19
2634
by: Chad | last post by:
Okay, let's say I have an exotic os that limits how much goes to stdout. When I go like.. #include <stdio.h> int main(void) { int i=0; for(i=0; i< 10; i++) { printf("a \n");
3
2422
by: scorro1 | last post by:
Hey guys, I am working on a program which is supposed to read input from a file at 100 chars per time, then change the spaces to underscores and reverse the order of the entire document. Now we are not allowed to go over 100 input characters per time. I had the project finished until i realized that after 100 characters the program would print the incorrect part first. for (i=0;i<n;i++) { if(buffer== 32) ...
64
9709
by: Philip Potter | last post by:
Hello clc, I have a buffer in a program which I write to. The buffer has write-only, unsigned-char-at-a-time access, and the amount of space required isn't known a priori. Therefore I want the buffer to dynamically grow using realloc(). A comment by Richard Heathfield in a thread here suggested that a good algorithm for this is to use realloc() to double the size of the buffer, but if realloc() fails request smaller size increments...
36
3795
by: James Harris | last post by:
Initial issue: read in an arbitrary-length piece of text. Perceived issue: handle variable-length data The code below is a suggestion for implementing a variable length buffer that could be used to read text or handle arrays of arbitrary length. I don't have the expertise in C of many folks here so I feel like I'm offering a small furry animal for sacrifice to a big armour plated one... but will offer it anyway. Please do suggest...
15
1399
by: raashid bhatt | last post by:
#include <stdio.h> #include <string.h> #include <stdlib.h> void func(char *p) { char i; strcpy(i, p); }
0
8348
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8861
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8778
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8636
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7375
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6187
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4351
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2764
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2003
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.