FILE Learning 二

FILE Learning 2

1. system ( const char *command)

   The system( )  function passes command to command interpreter, which executes the string as an operating-system command. If command is

NULL, the function simply checks to see whether the command interpreter exists.

eg: system("type c.txt");

remarks: You must explicitly flush( using fflush ) or close any stream before calling system.

2.fflush(FILE *stream)

The fflush function return  0 if the buffer was successfully flushed.The value 0 is 

also returned in cases in which the specified stream has no buffer or is open for reading only. A return value of EOF indicates an error.

Note: If fflush returns EOF, data may have been lost due to a write failure. When setting  up a critical error handler, it is safest to turn buffering off with the setvbuf function  or to use low-level I/O routines such as _open, _close,and _write instead of

the stream I/O functions.

3. buffers

Buffers are normally maintained by the OS, which determines the optimal time to

write the data automatically to disk: when a buffer is full, when a stream is closed, or when a program terminates normally without closing the stream.The commit-to-disk 

feature of the  run-time library lets you ensure that critical data is written directly to disk rather than to the operating-system buffers. Without rewriting an existing program, you can enable this feature by linking the program's object files with 

COMMODE.OBJ. In the resulting executable file, calls to _flushall write the contents

of all buffers to disk. Only _flushall and fflush are affected by COMMODE.OBJ.

4. int fseek(FILE *stream, long offset, int origin)

The fseek function moves the file pointer to a specified location.If successful, fseek return 0. Otherwise, it returns a nonzero value. On devices incapable of seeking, the return value is undefined.

Three origin types :

SEEK_CUR: End of file

SEEK_SET: Beginning of file

SEEK_END: End of file