Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

a conceptual problem

Status
Not open for further replies.

ssrc

Programmer
May 20, 2003
5
IN



hi i am writing a program

#include"stdio.h"

main()
{
FILE *fp
char another='Y'
struct emp
{
char name[40];
int age;
float bs;
};
struct emp e;
fp=fopen("EMPLOYEE.DAT","w");
if(fp==NULL)
{ puts("cannot open file");
exit();
}
while(another=='Y');
{ printf("\n ENTER NAME,AGE AND BASIC SALARY");
scanf("%s %d %f",e.name,&e.age,&e.bs);
fprintf(fp,"%s %d %f\n",e.name,e.age,e.bs);
printf("add another record(y/n)");
fflush(stdin);
another=getche();
}
fclose(fp);
}


what is the difference between &quot;stdio.h&quot; and <stdio.h>?
what is this fflush(stdin)?what is the job of this?if i donot write what problem could i face???plz i am a beginner.. give response as simple as u can(if possible with an example).
thanks

 
> what is the difference between &quot;stdio.h&quot; and <stdio.h>?
Only where the compiler starts looking for the file.
&quot;&quot; means start looking in the same directory where the .c file is, then search the INCLUDE path
<> means search the INCLUDE path.

The INCLUDE path is a list of directories which the compiler will look in for .h files. The compiler is pre-configured to look in a number of standard places for the standard header files (like stdio.h). Later on, you can add your own directories to the INCLUDE path, so that you may do things like
#include <my_good_stuff.h>


> what is this fflush(stdin)?
A mistake - the ANSI-C standard does not define what fflush() should do on an INPUT stream. Some implementations throw away characters up to the next newline, others do nothing at all. Having programs which work differently for different people is not generally a good idea.

> while(another=='Y');
You don't want that ; at the end of the line - this totally changes the meaning of the code.

 
[A few more comments to add to what Salem said]

>#include&quot;stdio.h&quot;

If you didn't get it from what Salem's comments, it's better (and more common) practice to use angle brackets here instead of quotes, since stdio.h is a standard library header.

> main()
> {
> FILE *fp
> char another='Y'
> struct emp
>{
> char name[40];
> int age;
> float bs;
> };
>struct emp e;
> fp=fopen(&quot;EMPLOYEE.DAT&quot;,&quot;w&quot;);
> if(fp==NULL)
>{ puts(&quot;cannot open file&quot;);

fputs(&quot;cannot open file\n&quot;, stderr);

It's good to get in the habit of writing error output to the standard error stream. In real programs, users will sometimes invoke your program in such a way that standard output and standard error go to different places.

> exit();

exit() requires an int argument, indicating termination status.

> }
>while(another=='Y');

If not for the trailing semi-colon, this means that the program will continue executing the statements between the next set of braces while `another' is equal to 'Y', which seems to be the intention. With the semi-colon, your program loops infinitely.

> { printf(&quot;\n ENTER NAME,AGE AND BASIC SALARY&quot;);

You'll want to insert a trailing space and call to fflush(stdout) afterwards to ensure that your prompt appears before the user can enter any input. Since stdout is line-buffered when connected to an interactive device, your characters may otherwise sit in the stream's internal buffer instead of being written.

> scanf(&quot;%s %d %f&quot;,e.name,&e.age,&e.bs);

It's dangerous to call scanf() with an unadorned %s in the format string because it doesn't limit the number of characters that can be stored in its corresponding argument. This makes your use of it equivalent to using gets(), which is a famous bug.

scanf(&quot;%39s %d %f&quot;,e.name,&e.age,&e.bs);

It will be difficult to detect errors with scanf() in this case. Initially reading the line with fgets() and then parsing the line into its components will allow you to do more effective error recovery. You should at least check scanf()'s return value.

> fprintf(fp,&quot;%s %d %f\n&quot;,e.name,e.age,e.bs);
> printf(&quot;add another record(y/n)&quot;);
> fflush(stdin);

As Salem mentioned, fflush() does not have a defined meaning in ANSI C. Implementors are free to define its meaning however they wish or not implement fflush() in such a way that it expects input streams. This even includes the possibility that calling fflush() on an input stream will result in your program crashing.

In terms of output and update streams, fflush() is defined to immediately write any characters it has stored in the stream's internal buffer to the associated file. There isn't an analogous meaning for input streams. Where should the characters be &quot;written&quot; when flushing input?

The usual intent here is to discard characters from the input stream. In this case, scanf() will leave the newline character in stdin that the user typed following his/her input. One way to do this:

int c;
while ((c = getchar()) != '\n' && c != EOF)
continue;

> another=getche();
>}
>fclose(fp);

return 0;

>}
 
Hi,
I will spend some words about fflush( stream ),
where stream is opend in output on a file.

When you make fprintf( stream, something... ), or
fput( stream, 'A' ), the system, to optimize Disk I/O,
does not write physically the char in the file, but
waits for it is full (the buffer), and then writes
all buffer (depends from OS) to the disk.

The buffer is, however &quot;flushed&quot; to disk, when you
close the file, or you exit from program
(in correct mode: if your program bombs out with an aceess
violation, no flush is done).

IMPORTANT:
----------------------------------------------------------
sometime, to avoid that that data will be lost in buffer
due a program crash, or if there is another program
that reads the shared files, while it is open, you
have to fflush(stream) to write immediatly data on disk.
----------------------------------------------------------

This, however, gives to the program no good performances
becouse, the time to write 1 byte or full buffer to the disk
is the the same; probably today these things sound as obsolete, seen disk and system performances,
but for who has worked on 8088 cpu with 10Mb HD
or for theorist speech, this has sense.

bye
 
You can control how the buffering mode for a stream with setvbuf after a succesful call to fopen.

/* No buffering. */
setvbuf(stream, NULL, _IONBF, 0);

/* Line buffering. */
setvbuf(stream, NULL, _IOLBF, 0);

/* Full buffering. */
setvbuf(stream, NULL, _IOFBF, 0);

If a stream refers to an interactive device, the stream is line-buffered by default. Otherwise, it is fully-buffered.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top