Question

Write a C program that reads in the file and outputs it to another file with...

Write a C program that reads in the file and outputs it to another file with its lines in reverse order. Use the following array to store the addresses of the lines as you read them in: char *lines[200]; Save each line so that it occupies only enough memory required by that line. Use recursion. Hint: On each call of the recursive function, read ONE line into a LOCAL buffer.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <stdio.h>

#include <string.h>

#include <stdlib.h>

#define bufSize 1024

int myfunction(FILE*,FILE*);

int main()

{

FILE* fp1;

FILE* fp2;

  

if ((fp1 = fopen("program.txt", "r")) == NULL)

{

printf("Error in opening the file");

exit(1);

}

  

  

if ((fp2 = fopen("save.txt", "w")) == NULL)

{

printf("Error in opening the file");

exit(1);

}

myfunction(fp1,fp2);

}

int myfunction(FILE *fp1,FILE* fp2)

{

static char lines[bufSize];

static char rstring[bufSize];

int lenbuf,lenstr,counter=0;

if (fgets(lines, sizeof(lines), fp1) != NULL)

{

lines[strlen(lines) - 1] = '\0';

lenbuf=strlen(lines);

  

for(int i=lenbuf-1;i>=0;i--)

{

rstring[counter]=lines[i];

counter++;

}

rstring[counter]='\0';

  

fputs(rstring,fp2);

fputs("\n",fp2);

  

counter=0;

  

myfunction(fp1,fp2);

  

}

else

{

printf("The data is successfully saved in reverse order");

fclose(fp1);

fclose(fp2);

return 0;

}

  

}

==========================================================================

OUTPUT:

The data is Successfully savedd in everse ordder Process exited after 0.005213 seconds with return value 0 Press any key to continue - - -

The data in program.txt file

The sata in save.txt after reversing.

===========================================================================

DESCRIPTION:

First of all I have created a local buffer bufSize with the help of #define to define the size of the line, and defined it as 1024, you may increment and decrement the size according to your wish.

#define bufSize 1024

Then there is main function which is creating 2 file pointers, because we need 2 files, 1 is for reading and another is for writing in reverse order.

so for reading we are using the file program.txt.

make sure the name of your file is also program.txt and it is stored in the same location as that of your c program. otherwise you need to provide the full path of program.txt. I have saved some data in program.txt. you may see it in screenshot.

another file save.txt is used to save the data in reverse order. this file is empty for now.

in main function I am opening both the files using file pointers fp1 and fp2. program.txt is opened in read mode whereas save.txt is opened in write mode. then we are checking if the pf1 and fp2==NULL to make sure that file has been successfully opened else it will exit the program if file not present or unable to open.

then I am calling myfunction() which will perform the task.

in myfunction first we are using fgets function to read each line from the file fp1 into a character variable lines which is of buffer size.

if the line is read then at the end of lines variable we are putting '\0', because the end of a tring must be denoted by '\0'

then we are using the strlen() to find the length of the string line. and store the length in strbuf variable.

then using for loop starting we are storing each character into fp2 means save.txt in reverse order. when it is done we are again calling myfunction()(recursion) to read next line.

if we are not able to read the line that means no new lines are present so it will come out of the function. by closing all the files.

Add a comment
Know the answer?
Add Answer to:
Write a C program that reads in the file and outputs it to another file with...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 1) Write a C program that displays all the command line arguments that appear on the...

    1) Write a C program that displays all the command line arguments that appear on the command line when the program is invoked. Use the file name cl.c for your c program. Test your program with cl hello goodbye and cl 1 2 3 4 5 6 7 8 and cl 2) Write a C program which displays the sum of the command line arguments. Hint: use sscanf to convert the decimal arguments (which are strings) to binary numbers that...

  • ( IN JAVA): Write a program that reads each line in a file, and writes them...

    ( IN JAVA): Write a program that reads each line in a file, and writes them out in reverse order into another file. This program should ask the user for the name of an input file, and the name of the output file. If the input file contains: Mary had a little lamb Its fleece was white as snow And everywhere that Mary went The lamb was sure to go Then the output file should end up containing: The lamb...

  • Implement a C program unique.c that recreates the functionality of the uniq tool by reading the...

    Implement a C program unique.c that recreates the functionality of the uniq tool by reading the input line by line and dropping each l ine that is identical to the line immediately before it. On the input abc abc abc your program should output abc abc the same output that uniq would produce. While not strictly necessary for this step, it is important as a basis for subsequent steps that you read the entire input and store it as a...

  • Write a C++ program that repeatedly reads lines until an EOF is encountered. As each line...

    Write a C++ program that repeatedly reads lines until an EOF is encountered. As each line is read, the program strips out all characters that are not upper or lower case letters or spaces, and then outputs the line. Thus, the program acts as a filter and issues no prompt. There are many ways this program could be written, but to receive full credit, you must observe the following: Place your code in a file called filterChars.cpp. The program should...

  • IN C language Write a C program that prompts the user to enter a line of...

    IN C language Write a C program that prompts the user to enter a line of text on the keyboard then echoes the entire line. The program should continue echoing each line until the user responds to the prompt by not entering any text and hitting the return key. Your program should have two functions, writeStr andcreadLn, in addition to the main function. The text string itself should be stored in a char array in main. Both functions should operate...

  • IN JAVA. Write a program that reads a file (provided as attachment to this assignment) and...

    IN JAVA. Write a program that reads a file (provided as attachment to this assignment) and writes the file to a different file with line numbers inserted at the beginning of each line. For example if file input is: This is a test File output should be 1. This is a test ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ BELOW FROM NOTEPAD FILE. This file contains lines of text to determine if you can properly read and write from a file.

  • Part1. Write a C program contains the following declarations: char customer_name[N_CUSTOMERS][MAX...

    Part1. Write a C program contains the following declarations: char customer_name[N_CUSTOMERS][MAX_NAME_LENGTH]; int customer_number[N_CUSTOMERS] A program uses a text file that contains the following data on each line: The customer number is an int, and the first and last names are alphabetic strings that contain no whitespace. The last and first names themselves are however separated by whitespace. Write a C function with the following prototype: void read_customer (char name[][MAX_NAME_LENGTH], int number[], int position, FILE *cust_file) Your function should read a...

  • Write a program to read a text file, place each line it reads into an array....

    Write a program to read a text file, place each line it reads into an array. Then print the array’s contents one line at a time. Then add to the end of the text file “Success”. Use error exception handling and if the text file does not exist print "Error file not found." Always print "It worked." as part of the try clause. Upload a zip folder file of the .py and text file. roses are roses are red, violets...

  • C++ Write a program that prompts for a file name and then reads the file to...

    C++ Write a program that prompts for a file name and then reads the file to check for balanced curly braces, {; parentheses, (); and square brackets, []. Use a stack to store the most recent unmatched left symbol. The program should ignore any character that is not a parenthesis, curly brace, or square bracket. Note that proper nesting is required. For instance, [a(b]c) is invalid. Display the line number the error occurred on.

  • Arrays and reading from a file USE C++ to write a program that will read a...

    Arrays and reading from a file USE C++ to write a program that will read a file containing floating point numbers, store the numbers into an array, count how many numbers were in the file, then output the numbers in reverse order. The program should only use one array and that array should not be changed after the numbers have been read. You should assume that the file does not contain more than 100 floating point numbers. The file name...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT