Question

6. Write a program to read a text file and produce another text file in which...

6. Write a program to read a text file and produce another text file in which all lines are less than some given length. Make sure and break lines in sensible places; for example, avoid breaking words or putting isolated punctuation marks at the beginning of a line.

Please finish the question in C language programming, thank you!

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

before running this program create the two text files and enter them along with their extension when asked at the console.

#include <stdio.h>
#include <stdlib.h> // For exit()

int main()
{
FILE *frd;
FILE *fwrite;

int char_count;
int length;
int i;
int temp,temp2;
char filename[100], filename1[100],*C;


printf("Enter the filename to open \n");
scanf("%s", filename);
printf("Enter the max line size(no of chars) \n");
scanf("%d",&char_count);
printf("Enter the filename to write the output \n");
scanf("%s", filename1);


// Open input file
frd = fopen(filename, "r");
//Open output file
fwrite = fopen(filename1, "w");
       //handle the case when input or output file does not exist.
if (frd == NULL||(fwrite == NULL))
{
printf("Cannot open file \n");
exit(0);
}

//start reading from input file
if (frd)
{
fseek (frd, 0, SEEK_END);
length = ftell (frd);
fseek (frd, 0, SEEK_SET);
C = malloc (length);
if (C)
{
fread (C, 1, length, frd);
}
fclose (frd);
}

//C stores the content of entire file
//traverse the string from the begining

temp2 =0; // temp2 will keep track of the character in each line
for(i=0;i<=(strlen(C));i++)
{
temp2++;
   if(temp2 == char_count+1)
   {
       C[temp]='\n';
       temp2 =i - temp;
       }

else if(C[i]==' ')
{
   temp =i;
}

}

//write the contents to the file
fprintf(fwrite,C);
  
return 0;
}

// Also note that the filename should be less than 100.

Add a comment
Know the answer?
Add Answer to:
6. Write a program to read a text file and produce another text file in which...
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
  • (Count the occurrences of words in a text file) Rewrite Listing 21.9 to read the text...

    (Count the occurrences of words in a text file) Rewrite Listing 21.9 to read the text from a text file. The text file is passed as a command-line argument. Words are delimited by white space characters, punctuation marks (, ; . : ?), quotation marks (' "), and parentheses. Count the words in a case-sensitive fashion (e.g., consider Good and good to be the same word). The words must start with a letter. Display the output of words in alphabetical...

  • Write a Python program to read lines of text from a file. For each word (i.e,...

    Write a Python program to read lines of text from a file. For each word (i.e, a group of characters separated by one or more whitespace characters), keep track of how many times that word appears in the file. In the end, print out the top twenty counts and the corresponding words for each count. Print each value and the corresponding words, in alphabetical order, on one line. Print this in reverse sorted order by word count. You can assume...

  • 3. Write a program to read a (binary) file of integers, sort the integers, and write...

    3. Write a program to read a (binary) file of integers, sort the integers, and write them back to the same file. Assume that all the numbers can be stored in an array. (Exercise 3) 4. Repeat exercise 3, but assume that only 20 numbers can be stored in memory (in an array) at any one time. Hint: you will need to use at least two additional files for temporary output. Please finish the question in C language programming, thank...

  • Write a C program to run on ocelot to read a text file and print it...

    Write a C program to run on ocelot to read a text file and print it to the display. It should optionally find the count of the number of words in the file, and/or find the number of occurrences of a substring, and/or take all the words in the string and sort them lexicographically (ASCII order). You must use getopt to parse the command line. There is no user input while this program is running. Usage: mywords [-cs] [-f substring]...

  • All the white space among words in a text file was lost. Write a C++ program...

    All the white space among words in a text file was lost. Write a C++ program which using dynamic programming to get all of the possible original text files (i.e. with white spaces between words) and rank them in order of likelihood with the best possible runtime. You have a text file of dictionary words and the popularity class of the word (words are listed from popularity 1-100 (being most popular words), 101-200, etc) - Input is a text file...

  • Homework IX-a Write a program that opens a text file, whose name you enter at the keyboard You wi...

    Homework IX-a Write a program that opens a text file, whose name you enter at the keyboard You will be using the file text.txt to test your program. Print out all of the individual, unique words contained in the file, in alphabetical order Print out the number of unique words appearing in text.txt. Call your program: YourName-HwrklXa.py Make sure that your name appears as a comment at the beginning of the program as well as on the output display showing...

  • Write a complete Python program with prompts for the user for the main text file (checks...

    Write a complete Python program with prompts for the user for the main text file (checks that it exists, and if not, output an error message and stop), for any possible flags (including none), and for any other input that this program may need from the user: split has an option of naming the smaller files head_tail list the first 10 lines (default) and the last 10 lines (default) in order of the given text file flag: -# output #...

  • Python: The attached text file: https://drive.google.com/open?id=1Hcj4Ey4NbKHn5-vyF-foR3iny0aX8y1...

    Python: The attached text file: https://drive.google.com/open?id=1Hcj4Ey4NbKHn5-vyF-foR3iny0aX8y1c Unique Words Write a program that opens a specified text file and then displays the number of unique words in the text file after stripping all the white spaces, comma, and the punctuation marks and a list of all the unique words found in the file. The list should be sorted in alphabetical order. The program should handle the ‘file not found’ error. Store each word as an element of list. Be sure not...

  • Pig Latin Translator in Java The goal here is to read in the characters stored in a text file and create a second text file that contains the original text but with every word converted to “Pig-Latin....

    Pig Latin Translator in Java The goal here is to read in the characters stored in a text file and create a second text file that contains the original text but with every word converted to “Pig-Latin.” The rules used by Pig Latin are as follows: • If a word begins with a vowel, just as "yay" to the end. For example, "out" is translated into "outyay". • If it begins with a consonant, then we take all consonants before...

  • Part I) Write a program that copies one file to another, replacing all lowercase characters with...

    Part I) Write a program that copies one file to another, replacing all lowercase characters with their uppercase equivalents. Part II) Write a program that merges lines alternately from two files and writes the results to a third file. If one file has less lines than the other, then the remaining lines from the larger file should be printed to the screen. Using C language

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