Develop a simple C program that reads and writes from a file and uses loops and arrays.

Files allow you to store data persistently. In this task you will take a simple c file reading program and modify it to read multiple lines using a loop, store each line as an element in an array, then print the lines to the Bash terminal screen.
To explore this topic, we will create a C program that will:
■ Open a file and read in the number of lines in the file.
■ Loop according to the number of lines to read in each line.
■ Store each line in an array.
■ Print out the lines from the array to the terminal

1. The integer read from the first line of the file should indicate the number of text lines that follow. Your program should then loop reading one line after another into an array of my_string (or *char[]’s) until all the text lines have been read in. Call a procedure, passing in the array and the number of lines, that prints out to the terminal all the text lines that read from the file.

2. Comment out the write_lines() procedure. Instead you will need to create a text file (mytestfile.dat) with a number on the first line (to indicate the following number of text lines) and then lines of text. Use notepad or Sublime Text to create the file.

Solution PreviewSolution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

int numberOfLines;
    my_string lineArray[20];
    FILE *file_ptr;
    int i;   
    // Steps:
    // 1: Open the file
    file_ptr = fopen("mytestfile.dat", "r");

    //2. Read in the number of text lines in the file.
    fscanf(file_ptr, "%d", &numberOfLines);
    //       3. Loop reading each line of line text into an array until
    //       all the lines have been read.
    for (i = 0; i < 20 && i < numberOfLines; i++) {...

By purchasing this solution you'll be able to access the following files:

for this solution

or FREE if you
register a new account!

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available C-Family Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Upload a file
Continue without uploading

We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats