36.5k views
3 votes
Write a program in main.c that reads the file lorum.txt and counts the number of characters on each line. Your program should create a file named count.txt. The contents of count.txt will contain the character count you computed for each line in lorum.txt. For each line in lorum.txt, your count.txt file must contain a line in the following format: linenumber: number_of_characters The character count for a line should not include the "newline character" (\\).

1 Answer

1 vote

Answer:

A C programming language was used to write a file lorum.txt and counts the number of characters on each line.

Step-by-step explanation:

Raw_Code :

#include<stdio.h>

int main(){

FILE *read,*write; //creating file pointers

int line=1,count=0;

char ch;

read = fopen("lorum.txt","r"); //opening files to read and write

write = fopen("count.txt","w");

while((ch = getc(read))!=EOF){ //reading lines until end of the file

if(ch == '\\'){ //if new line character encountred

fprintf(write,"%d: %d\\",line,count); //Then printing count of that line to file

line++; //incrementing the count of lines

count = 0; //making characters count to 0

}

else{

count++; //if it is not new line character then increasing character count

}

}

fprintf(write,"%d: %d",line,count); //writing last lines count to count.txt

fclose(read);

fclose(write);

return 0;

}

User SamiAzar
by
3.6k points