home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / C / CTUTOR1.ZIP / CHAP10.TXT < prev    next >
Encoding:
Text File  |  1986-06-30  |  18.0 KB  |  455 lines

  1.                        Chapter 10 - File Input/Output
  2.  
  3.  
  4.                               OUTPUT TO A FILE
  5.  
  6.              Load  and  display  the file named FORMOUT.C  for  your 
  7.  
  8.         first example of writing data to a file.  We begin as before 
  9.  
  10.         with the "include" statement for "stdio.h", then define some 
  11.  
  12.         variables for use in the example including a rather  strange 
  13.  
  14.         looking new type.
  15.  
  16.              The  type  "FILE"  is used for a file variable  and  is 
  17.  
  18.         defined in the "stdio.h" file.   It is used to define a file 
  19.  
  20.         pointer  for use in file operations.   The definition  of  C 
  21.  
  22.         contains  the requirement for a pointer to a "FILE",  and as 
  23.  
  24.         usual, the name can be any valid variable name.
  25.  
  26.                                OPENING A FILE
  27.  
  28.              Before we can write to a file,  we must open it.   What 
  29.  
  30.         this  really means is that we must tell the system  that  we 
  31.  
  32.         want  to  write to a file and what the filename is.   We  do 
  33.  
  34.         this with the "fopen" function illustrated in the first line 
  35.  
  36.         of the program.   The file pointer, "fp" in our case, points 
  37.  
  38.         to   the  file  and  two  arguments  are  required  in   the 
  39.  
  40.         parentheses,  the filename first, followed by the file type.  
  41.  
  42.         The filename is any valid DOS filename, and can be expressed 
  43.  
  44.         in  upper  or lower case letters,  or even mixed if  you  so 
  45.  
  46.         desire.   It is enclosed in double quotes.  For this example 
  47.  
  48.         we have chosen the name TENLINES.TXT.   This file should not 
  49.  
  50.         exist  on your disk at this time.   If you have a file  with 
  51.  
  52.         this  name,  you should change its name or move  it  because 
  53.  
  54.         when  we execute this program,  its contents will be erased.  
  55.  
  56.         If you don't have a file by this name,  that is good because 
  57.  
  58.         we will create one and put some data into it.
  59.  
  60.                                READING ("r")
  61.  
  62.              The  second parameter is the file attribute and can  be 
  63.  
  64.         any of three letters,  "r",  "w",  or "a", and must be lower 
  65.  
  66.         case.   When an "r" is used, the file is opened for reading, 
  67.  
  68.         a "w" is used to indicate a file to be used for writing, and 
  69.  
  70.         an  "a" indicates that you desire to append additional  data 
  71.  
  72.         to the data already in an existing file.  Opening a file for 
  73.  
  74.         reading  requires that the file already exist.   If it  does 
  75.  
  76.         not exist,  the file pointer will be set to NULL and can  be 
  77.  
  78.         checked by the program. 
  79.  
  80.                                WRITING ("w")
  81.  
  82.              When  a file is opened for writing,  it will be created 
  83.  
  84.         if it does not already exist and it will be reset if it does 
  85.  
  86.         resulting in deletion of any data already there. 
  87.  
  88.  
  89.  
  90.  
  91.                                    Page 66
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.                        Chapter 10 - File Input/Output
  102.  
  103.  
  104.                               APPENDING ("a")
  105.  
  106.              When a file is opened for appending, it will be created 
  107.  
  108.         if it does not already exist and it will be initially empty.  
  109.  
  110.         If  it does exist,  the data input point will be the end  of 
  111.  
  112.         the  present data so that any new data will be added to  any 
  113.  
  114.         data that already exists in the file.
  115.  
  116.                            OUTPUTTING TO THE FILE
  117.  
  118.              The  job of actually outputting to the file  is  nearly 
  119.  
  120.         identical  to  the  outputting we have already done  to  the 
  121.  
  122.         standard output device.   The only real differences are  the 
  123.  
  124.         new  function names and the addition of the file pointer  as 
  125.  
  126.         one  of  the function arguments.   In the  example  program, 
  127.  
  128.         "fprintf" replaces our familiar "printf" function name,  and 
  129.  
  130.         the  file  pointer  defined earlier is  the  first  argument 
  131.  
  132.         within  the  parentheses.   The remainder of  the  statement 
  133.  
  134.         looks  like,  and  in  fact is identical  to,  the  "printf" 
  135.  
  136.         statement.
  137.  
  138.                                CLOSING A FILE
  139.  
  140.              To close a file,  you simply use the function  "fclose" 
  141.  
  142.         with the file pointer in the parentheses.  Actually, in this 
  143.  
  144.         simple  program,  it  is  not necessary to  close  the  file 
  145.  
  146.         because   the  system  will  close  all  open  files  before 
  147.  
  148.         returning to DOS.  It would be good programming practice for 
  149.  
  150.         you to get in the habit of closing all files in spite of the 
  151.  
  152.         fact  that they will be closed automatically,  because  that 
  153.  
  154.         would act as a reminder to you of what files are open at the 
  155.  
  156.         end of each program.
  157.  
  158.              You can open a file for writing,  close it,  and reopen 
  159.  
  160.         it  for  reading,  then  close it,  and open  it  again  for 
  161.  
  162.         appending,  etc.   Each time you open it,  you could use the 
  163.  
  164.         same file pointer,  or you could use a different  one.   The 
  165.  
  166.         file  pointer  is simply a tool that you use to point  to  a 
  167.  
  168.         file and you decide what file it will point to.
  169.  
  170.              Compile  and run this program.   When you run  it,  you 
  171.  
  172.         will  not  get any output to the monitor because it  doesn't 
  173.  
  174.         generate any.   After running it, look at your directory for 
  175.  
  176.         a file named TENLINES.TXT and "type" it.  That is where your 
  177.  
  178.         output will be.   Compare the output with that specified  in 
  179.  
  180.         the program.  It should agree.
  181.  
  182.              Do not erase the file named TENLINES.TXT yet.   We will 
  183.  
  184.         use it in some of the other examples in this chapter.
  185.  
  186.  
  187.  
  188.  
  189.  
  190.                                    Page 67
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.                        Chapter 10 - File Input/Output
  201.  
  202.  
  203.                   OUTPUTTING A SINGLE CHARACTER AT A TIME
  204.  
  205.              Load the next example file,  CHAROUT.C,  and display it 
  206.  
  207.         on your monitor.  This program will illustrate how to output 
  208.  
  209.         a single character at a time.
  210.  
  211.              The  program begins with the "include" statement,  then 
  212.  
  213.         defines  some variables including a file pointer.   We  have 
  214.  
  215.         called the file pointer "point" this time, but we could have 
  216.  
  217.         used any other valid variable name.  We then define a string 
  218.  
  219.         of characters to use in the output function using a "strcpy" 
  220.  
  221.         function.   We are ready to open the file for appending  and 
  222.  
  223.         we  do so in the "fopen" function,  except this time we  use 
  224.  
  225.         the  lower cases for the filename.   This is done simply  to 
  226.  
  227.         illustrate  that  DOS  doesn't care about the  case  of  the 
  228.  
  229.         filename.  Notice that the file will be opened for appending 
  230.  
  231.         so  we  will  add  to the lines  inserted  during  the  last 
  232.  
  233.         program.
  234.  
  235.              The  program is actually two nested "for"  loops.   The 
  236.  
  237.         outer  loop  is  simply a count to ten so that  we  will  go 
  238.  
  239.         through the inner loop ten times.   The inner loop calls the 
  240.  
  241.         function  "putc" repeatedly until a character in "others" is 
  242.  
  243.         detected to be a zero.
  244.  
  245.                             THE "putc" FUNCTION
  246.  
  247.              The  part  of the program we are interested in  is  the 
  248.  
  249.         "putc" function.   It outputs one character at a  time,  the 
  250.  
  251.         character  being  the first argument in the parentheses  and 
  252.  
  253.         the  file pointer being the second and last  argument.   Why 
  254.  
  255.         the  designer of C made the pointer first in  the  "fprintf" 
  256.  
  257.         function, and last in the "putc" function is a good question 
  258.  
  259.         for which there may be no answer.   It seems like this would 
  260.  
  261.         have been a good place to have used some consistency.
  262.  
  263.              When  the textline "others" is exhausted,  a newline is 
  264.  
  265.         needed because a newline was not included in the  definition 
  266.  
  267.         above.   A  single "putc" is then executed which outputs the 
  268.  
  269.         "\n" character to return the carriage and do a linefeed.
  270.  
  271.              When  the outer loop has been executed ten  times,  the 
  272.  
  273.         program  closes the file and terminates.   Compile  and  run 
  274.  
  275.         this  program but once again there will be no output to  the 
  276.  
  277.         monitor. 
  278.  
  279.              Following  execution  of the program,  "type" the  file 
  280.  
  281.         named  TENLINES.TXT and you will see that the 10  new  lines 
  282.  
  283.         were  added to the end of the 10 that already  existed.   If 
  284.  
  285.         you run it again,  yet another 10 lines will be added.  Once 
  286.  
  287.  
  288.  
  289.  
  290.  
  291.                                    Page 68
  292.  
  293.  
  294.  
  295.  
  296.  
  297.  
  298.  
  299.  
  300.  
  301.                        Chapter 10 - File Input/Output
  302.  
  303.  
  304.         again,  do  not  erase  this file because we are  still  not 
  305.  
  306.         finished with it. 
  307.  
  308.                                READING A FILE
  309.  
  310.              Load  the file named READCHAR.C and display it on  your 
  311.  
  312.         monitor. This is our first program to read a file. 
  313.  
  314.              This program begins with the familiar  "include",  some 
  315.  
  316.         data  definitions,  and  the  file opening  statement  which 
  317.  
  318.         should  require no explanation except for the fact  that  an 
  319.  
  320.         "r"  is  used  here because we want to  read  it.   In  this 
  321.  
  322.         program,  we  check to see that the file exists,  and if  it 
  323.  
  324.         does,  we  execute  the  main body of the  program.   If  it 
  325.  
  326.         doesn't,  we print a message and quit.  If the file does not 
  327.  
  328.         exist,  the system will set the pointer equal to NULL  which 
  329.  
  330.         we can test.
  331.  
  332.              The  main body of the program is one "do while" loop in 
  333.  
  334.         which a single character is read from the file and output to 
  335.  
  336.         the monitor until an EOF (end of file) is detected from  the 
  337.  
  338.         input  file.   The  file is then closed and the  program  is 
  339.  
  340.         terminated. 
  341.  
  342.                          CAUTION  CAUTION  CAUTION
  343.  
  344.              At  this point,  we have the potential for one  of  the 
  345.  
  346.         most  common and most perplexing problems of programming  in 
  347.  
  348.         C.   The  variable  returned from the "getc" function  is  a 
  349.  
  350.         character,  so  we  could  use a "char"  variable  for  this 
  351.  
  352.         purpose.   There is a problem with that however,  because on 
  353.  
  354.         some,  if not most,  implementations of C, the EOF returns a 
  355.  
  356.         minus  one  which a "char" type variable is not  capable  of 
  357.  
  358.         containing.  A "char" type variable can only have the values 
  359.  
  360.         of  zero to 255,  so it will return a 255 for a minus one on 
  361.  
  362.         those  compilers that use a minus one for EOF.   This  is  a 
  363.  
  364.         very   frustrating  problem  to  try  to  find  because   no 
  365.  
  366.         diagnostic is given.   The program simply can never find the 
  367.  
  368.         EOF  and will therefore never terminate the loop.   This  is 
  369.  
  370.         easy  to prevent,  always use an "int" type variable for use 
  371.  
  372.         in returning an EOF.   You can tell what your compiler  uses 
  373.  
  374.         for  EOF  by  looking  at the "stdio.h" file  where  EOF  is 
  375.  
  376.         defined.  That is the standard place to define such values.
  377.  
  378.              There is another problem with this program but we  will 
  379.  
  380.         worry  about it when we get to the next program and solve it 
  381.  
  382.         with the one following that.
  383.  
  384.              After  you  compile  and  run  this  program  and   are 
  385.  
  386.         satisfied  with the results,  it would be a good exercise to 
  387.  
  388.         change the name of "TENLINES.TXT" and run the program  again 
  389.  
  390.  
  391.  
  392.                                    Page 69
  393.  
  394.  
  395.  
  396.  
  397.  
  398.  
  399.  
  400.  
  401.  
  402.                        Chapter 10 - File Input/Output
  403.  
  404.  
  405.         to see that the NULL test actually works as stated.  Be sure 
  406.  
  407.         to  change  the name back because we are still not  finished 
  408.  
  409.         with "TENLINES.TXT".
  410.  
  411.                           READING A WORD AT A TIME
  412.  
  413.              Load  and  display  the file named  READTEXT.C  for  an 
  414.  
  415.         example of how to read a word at a time. 
  416.  
  417.              This  program  is nearly identical as the  last  except 
  418.  
  419.         that  this program uses the "fscanf" function to read  in  a 
  420.  
  421.         string  at  a  time.   Because the "fscanf"  function  stops 
  422.  
  423.         reading  when it finds a space or a  newline  character,  it 
  424.  
  425.         will read a word at a time, and display the results one word 
  426.  
  427.         to  a line.   You will see this when you compile and run it, 
  428.  
  429.         but first we must examine a programming problem.
  430.  
  431.                              THIS IS A PROBLEM
  432.  
  433.              Inspection of the program will reveal that when we read 
  434.  
  435.         data in and detect the EOF, we print out something before we 
  436.  
  437.         check  for the EOF resulting in an extra line  of  printout.  
  438.  
  439.         What  we usually print out is the same thing printed on  the 
  440.  
  441.         prior  pass  through  the loop because it is  still  in  the 
  442.  
  443.         buffer "oneword".  We therefore must check for EOF before we 
  444.  
  445.         execute  the  "printf"  function.   This has  been  done  in 
  446.  
  447.         READGOOD.C,  which  you will shortly examine,  compile,  and 
  448.  
  449.         execute.
  450.  
  451.              Compile  and execute the original program we have  been 
  452.  
  453.         studying, READTEXT.C and observe the output.  If you haven't 
  454.  
  455.         changed  TENLINES.TXT you will end up with "Additional"  and 
  456.  
  457.         "lines."  on  two  separate lines  with  an  extra  "lines." 
  458.  
  459.         displayed because of the "printf" before checking for EOF.
  460.  
  461.              Compile  and  execute READGOOD.C and observe  that  the 
  462.  
  463.         extra  "lines." does not get displayed because of the  extra 
  464.  
  465.         check for the EOF in the middle of the loop.   This was also 
  466.  
  467.         the problem referred to when we looked at READCHAR.C,  but I 
  468.  
  469.         chose  not  to expound on it there because the error in  the 
  470.  
  471.         output was not so obvious.
  472.  
  473.                         FINALLY, WE READ A FULL LINE
  474.  
  475.              Load and display the file READLINE.C for an example  of 
  476.  
  477.         reading  a complete line.   This program is very similar  to 
  478.  
  479.         those we have been studying except for the addition of a new 
  480.  
  481.         quantity, the NULL.
  482.  
  483.              We  are  using "fgets" which reads in an  entire  line, 
  484.  
  485.         including  the newline character into a buffer.   The buffer 
  486.  
  487.  
  488.  
  489.                                    Page 70
  490.  
  491.  
  492.  
  493.  
  494.  
  495.  
  496.  
  497.  
  498.  
  499.                        Chapter 10 - File Input/Output
  500.  
  501.  
  502.         to be read into is the first argument in the function  call, 
  503.  
  504.         and  the maximum number of characters to read is the  second 
  505.  
  506.         argument,  followed by the file pointer.  This function will 
  507.  
  508.         read  characters into the input buffer until it either finds 
  509.  
  510.         a  newline  character,  or it reads the  maximum  number  of 
  511.  
  512.         characters  allowed minus one.   It leaves one character for 
  513.  
  514.         the end of string NULL character.   In addition, if it finds 
  515.  
  516.         an  EOF,  it will return a value of NULL.   In our  example, 
  517.  
  518.         when the EOF is found,  the pointer "c" will be assigned the 
  519.  
  520.         value  of NULL.   NULL is defined as zero in your  "stdio.h" 
  521.  
  522.         file. 
  523.  
  524.              When  we find that "c" has been assigned the  value  of 
  525.  
  526.         NULL,  we can stop processing data, but we must check before 
  527.  
  528.         we print just like in the last program.
  529.  
  530.              Last of course, we close the file.
  531.  
  532.                        HOW TO USE A VARIABLE FILENAME
  533.  
  534.              Load  and display the file ANYFILE.C for an example  of 
  535.  
  536.         reading  from any file.   This program asks the user for the 
  537.  
  538.         filename desired,  reads in the filename and opens that file 
  539.  
  540.         for reading.   The entire file is then read and displayed on 
  541.  
  542.         the   monitor.    It  should  pose  no  problems   to   your 
  543.  
  544.         understanding so no additional comments will be made.
  545.  
  546.              Compile  and  run  this program.   When it  requests  a 
  547.  
  548.         filename,  enter  the  name and extension of any  text  file 
  549.  
  550.         available, even one of the example C programs.
  551.          
  552.                               HOW DO WE PRINT?
  553.  
  554.              Load  the  last example file in this chapter,  the  one 
  555.  
  556.         named  PRINTDAT.C  for an example of  how  to  print.   This 
  557.  
  558.         program  should not present any surprises to you so we  will 
  559.  
  560.         move very quickly through it.
  561.  
  562.              Once  again,  we  open TENLINES.TXT for reading and  we 
  563.  
  564.         open PRN for writing.  Printing is identical to writing data 
  565.  
  566.         to  a disk file except that we use a standard name  for  the 
  567.  
  568.         filename.   There  are  no definite standards as far as  the 
  569.  
  570.         name  or names to be used for the printer,  but some of  the 
  571.  
  572.         usual names are,  "PRN",  "LPT",  "LPT1", and "LPT2".  Check 
  573.  
  574.         your documentation for your particular implementation.
  575.  
  576.              Some  of  the newest compilers use  a  predefined  file 
  577.  
  578.         pointer  such as "stdprn" for the print file.   Once  again, 
  579.  
  580.         check your documentation.
  581.  
  582.  
  583.  
  584.  
  585.  
  586.                                    Page 71
  587.  
  588.  
  589.  
  590.  
  591.  
  592.  
  593.  
  594.  
  595.  
  596.                        Chapter 10 - File Input/Output
  597.  
  598.  
  599.              The  program is simply a loop in which a  character  is 
  600.  
  601.         read, and if it is not the EOF, it is displayed and printed.  
  602.  
  603.         When the EOF is found, the input file and the printer output 
  604.  
  605.         files are both closed.
  606.  
  607.              You can now erase TENLINES.TXT from your disk.  We will 
  608.  
  609.         not be using it in any of the later chapters.
  610.  
  611.  
  612.         PROGRAMMING EXERCISES
  613.  
  614.         1.   Write a program  that will prompt for a filename for  a 
  615.  
  616.              read file,  prompt for a filename for a write file, and 
  617.  
  618.              open both plus a file to the printer. Enter a loop that 
  619.  
  620.              will read a character,  and output it to the file,  the 
  621.  
  622.              printer, and the monitor. Stop at EOF.
  623.  
  624.         2.   Prompt for a  filename to read. Read the file a line at 
  625.  
  626.              a time and display it on the monitor with line numbers.
  627.  
  628.  
  629.  
  630.  
  631.  
  632.  
  633.  
  634.  
  635.  
  636.  
  637.  
  638.  
  639.  
  640.  
  641.  
  642.  
  643.  
  644.  
  645.  
  646.  
  647.  
  648.  
  649.  
  650.  
  651.  
  652.  
  653.  
  654.  
  655.  
  656.  
  657.  
  658.  
  659.  
  660.  
  661.                                    Page 72
  662.  
  663.