Chp 13 More Issues In Input/Output Flashcards
File copy programs where we transfered data char by char at the beginning, is typed first then compiled then executed.
2 ways it can be improved:
- to use file-copy utility there is no need to compile it every time. It means program must be executable at command prompt ( A> or C> : MS-DOS, Run dialog: Windows, $ prompt : Unix)
[F9, F7 to compile program, in Unix we get exe file always.] - Instead of program prompting us, we should be able to enter source and traget filenames at command prompt.
filecopy PR1.C PR2.C
using argc and argv, passing source n target filename to main().
Using argc and argv
main( int argc, char *argv[ ] ) {…}
The arguments that we pass on to main() at the command prompt are called command line arguments.
main() can have 2 arguments, traditionally named as argc and argv.
argv is array of pointers to string and argc is no. of strings to which argv points.
its a good habit to check if correct number of arg are passed on to main().
feof()
it returns zero if end of file is not reached.
Its a Macro.
returns non zero if end of file is reached.
while( !feof(fs) )
Detecting Errors in reading/Writing :
if (ferror() )
{ perror ( “Trial” ); break; }
both func are std lib functions. ferror returns 0 in case of no errors and non zero otherwise.
perror prints error message specified by compiler and “ Trial “ precedes that msg. Example:
Trial: Permission denied.
Standard I/O device:
Most OS’s predefine pointers for 3 standard file:
stdin: Standard Input Device(Keyboard).
stdout: Std output device (VDU).
stderr: Standard Error device (VDU).
MS DOS: stdprn(printer), stdaux (auxiliary device, serial port).
No need to open or close these.
example:
fputc( ch, stdprn);
I/O Redirection:
OS makes assumptions like input comes from keyboard and output goes to VDU.
Redirection permits us to change these assumptions.
This approach is more convenient and flexible than providing a separate function in program.
To use redirection facility is to execute the program from the command prompt, inserting the redirection symbols at appropriate places.
End of file character:
Ctrl+Z
Redirecting the output:
using stdout causes output to print on screen, but the redirection operator ‘>’ causes it to be written to the file whose name follows. Example:
C>UTIL.EXE>POEM.TXT
(UTIL gets input from stdin and sends output to stdout.)
DOS has PRN for printer, so
C>UTIL.EXE>PRN
this will redirect it to printer.
Redirecting the Input:
Using Input redirection operator
C>UTIL.EXE
Wht kind of a program is filter:
the input of program comes from a file via redirection and at the same time its output can be redirected to a file.
Redirection of Input and Output Together:
C>UTIL.EXEPOETRY.TXT
input from newpoem and output to poetry.
C>UNTILPRN
newpoem to printer.
What if we try to recieve input from a file to whom we send output to?
the output file gets erased before its written to, so by the time we manage to receive the input from a file it is already erased.
piping
To feed one’s output directly as input to another file. It’s done using ‘|’.