| 
  • If you are citizen of an European Union member nation, you may not use this service unless you are at least 16 years old.

  • You already know Dokkio is an AI-powered assistant to organize & manage your digital files & messages. Very soon, Dokkio will support Outlook as well as One Drive. Check it out today!

View
 

Exercises - File Access

Page history last edited by Steve Sweeney 8 years, 2 months ago

 

  1. Key Concepts: File Operations, String Variables, Input/Output
    Write a program that creates/opens a file called "file01.txt" and outputs a single string (sentence) to the file.  Once your program is completed, check the file to confirm the contents.  Make sure to open your file for output.

    (a) Modify your program.  After writing to the file, open the same file for input.  Read the sentence into a single string variable and output the string to the screen.

    (b)  Let the user input the sentence to be output to the file.

     

  2. Key Concepts: File Operations, Random Values, Arrays (extension)
    Write a program or modify #1 above to output 5 random integer values between 1 and 10 to "file02.txt".  Verify your results.

    (a) Modify the program to read the 5 values from the file.  Output the values to the screen. (Turing Solution)

    (b) Let the user pick the number of values read from the file.  Output the values.

    (c) Increase the number of values to 100.  You may wish to use an array of integers for your variables.

     

  3. Key Concepts: File Operations, Variables
    Write a program that will input information (name, address, age) for 3 people from a data file called "file03.txt".  You should use a text editor to create the file in advance.  Output the data on the screen.  Format your output so all of the information for a single person is on one line (i.e., name, address, age).  For example, the contents of the data file might look like:

    Bob

    123 Some Road

    16

    Doug

    456 Other Place

    17

    Mackenzie

    789 Elsewhere Avenue

    15

    Extensions:

    (a)  Start by asking the user for name, address, and age information for 3 people.  Save that information to a file first.

    (b)  Let the user edit information after it has been loaded from the file.  Give the option to save the new data to the file (overwriting the old data).  Verify your program is working by inspecting the file in a text editor. (Turing Solution)

     

  4. Key Concepts: File Operations, Random Values, Calculations, Organizing & Formatting Output (extension)
    Generate a text file with 20 integer values between 35 and 95 with a single value per line.  Write a program to read this file and calculate the average of the values.

    (a)  Copy your text file to a new file name.  Modify your file so there is a name (or random characters) for every four marks, starting with the name on the first line.  Now modify your program to determine the average for each person, and output a message with the name and the average.

    For example:

    Bob

    76

    86

    63

    88

    Doug

    56

    54

    etc...

    (b)  Perform further modifications to add a last name under the first name.  Output each name and average.

    (c)  Perform further modifications so that each student is on a single line:

    FirstName   LastName   M1   M2   M3    M4

    Modify your program so that it reads information for each student from the new file format and then displays each name and average.

    (d)  Display the overall average using one of the extensions you completed above.

     

  5. Key Concepts: File Operations, Calculations, Arrays (optional)
    Create a program that will determine the mean (numerical average) from the data in file05a.txt.  Display the average on the screen for the user.  Use the concept of end-of-file (EOF) in your program.

    (a) Determine the average from the data in file05b.txt.

     

  6. Key Concepts: File Operations, Selection (if/else), Loops, Arrays, Data Structures/Records
    Create a program that will read the information in the file file06.txt.  For each "student", display their full name and overall average.

    (a) After displaying averages, display a special message for the top and bottom student in the class.

    (b)  Let the user modify the names or marks of any student.  You will need to be creative in how you display information and/or prompt the user to choose the item to edit.  When edits are complete, give the option to save the file. (Note:  When creating this file, there were 10 spaces for the first and last name, and 5 spaces for each mark).

     

  7. Write a program that will take an existing file (e.g., a list of names) and let the user add new data at the top of the file (overwriting existing data). (Sample File) (Turing Solution)

    (a) Modify your program to write the new data to the end of the file, so you don't lose any data. (Turing Solution)

     

  8. Write a program that will do a "search & replace" on a text file.  Prompt the user for a string and search the text file for that string.  If the string is found, replace that string.  Otherwise, notify the user that the string was not found. (Turing Solution)

    (a) Modify the program to keep asking for strings until the user enters the command to quit or exit the program.

    (b) Add another feature to let the user print out the contents of the file so they can review the file before their search and replace.

    (c) Add another feature to let the user append data (i.e., add data) to the end of the file.

     

  9. Create a program that will prompt the user for some names and save them in a binary file.  When the user is finished entering names, display the contents of the file. (Turing Solution)

     

 

Comments (0)

You don't have permission to comment on this page.