Practice Problems

File I/O

Notice when you get compile errors (when gcc gives you an error)
vs. errors that crash the program (run-time errors)
vs. logical errors (compile and run fine but problem with your code)

  1. Write the code to declare and open a file for reading named coba.txt
  2. Write the code to declare and open a file for writing named out.txt
  3. What is the purpose of the fclose call on a file?
  4. What is the code to read the first line of the file (and that's it)?
  5. Write the loop to read each line from a file.
  6. What additional argument is required when using fprintf and fscanf as opposed to printf and scanf?
  7. List 3 functions that can be used to read data from a file.
  8. List 3 functions that can be used to write data to a file.
  9. Write the code to open a file named input.txt and print an error statement if there is a problem opening the file.