Question
Write a program that will add line numbers to a file. You must ask the user for two filenames, one for the input file and
Write a program that will add line numbers to a file. You must ask the user for two filenames, one for the input file and one for the output file. Open both files. While the end of the input file is not reached, read the next line in the input file add the line number and write the line to the output file.
Before running the program, create an input file using an ASCII editor like NOTEPAD with at least 10 lines. Do not use the current C program as the input file. In fact do not use any file that you need since if the program fails it will destroy the file you are inputting.
Some hints:
Suppose you read a line from the input file into a string variable called str using the following statement: fgets(str, 80, infile);You can add a line number and save the line with the line number to the output file using the same statement using: fprintf(outfile,"%d. %s ",Next_line_number++, str);
To open two file simultaneously simply use two FILE variables with different names.
You may consider using the following algorithm
Get the two file names from the user.
Open two files, one for input and the other for output.
Make the current line number 1.
While the end of the input file is not reached
Read a line from the input file.
Write the line along with the current line number to the output file.
Add 1 to the current line number.
Close both files.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started