answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
RideAnS
1 month ago
11

Write a program using the standard library I/O functions (fopen, fread, fwrite, fclose, ferror, feof, and fflush) to read a text

file and search it for a string (specified on the command line) in each line of an input file (whose name is also specified on the command line); if the string is found substitute another string (also specified on the command line) ONLY ONCE on the line.Write the output with the substitutions to the stdout, and write any errors to the stderr.
Computers and Technology
1 answer:
amid [951]1 month ago
7 0

Answer:

Refer to the explanation

Explanation:

#include <stdio.h>

#include <stdlib.h>

#include<string.h>

int main(int argc, char *argv[])

{

FILE *fr,*fr1,*fr3; /* declare the file pointer */

char filename[256];

char search[256];

char line[256];

int count=0;

char replace[256];

printf("Enter FileName: "); // ask for filename

scanf("%s",filename);

fr3 = fopen (filename, "r");

if(ferror(fr3)) //check for any error assosicated to file

{

fprintf(stderr, "Error in file"); // print error in stderr

printf("Error");

}

printf("Input file data is");

while(!feof(fr3)) // checking end of file condition

{ //printf("inside");

fgets(line,256,fr3);

printf("%s",line);

}

printf("\nEnter String to search: "); //ask for string to search

scanf("%s",search);

printf("Enter string with whom you want to replace: "); //ask for string with whom you want to replace one time

scanf("%s",replace);

fr = fopen (filename, "r"); // open first file in read mode

fr1 = fopen ("output.txt", "w"); //open second file in write mode

if(ferror(fr)) //check for any error assosicated to file

{

fprintf(stderr, "Error in file"); // print error in stderr

printf("Error");

}

while(!feof(fr)) // checking end of file condition

{ //printf("inside");

fgets(line,256,fr);

int r=stringCompare(line,search); // comparing every string and search string

// printf("%d",r);

if(r==1 && count==0)

{

fwrite(replace, 1, sizeof(replace), fr1 ); // writing string to file.

printf("%s\n",replace);

count++;

}

else{

printf("%s",line);

fwrite(line, 1, sizeof(line), fr1 );

}}

printf("\n");

fflush(fr1); // it will flush any unwanted string in stream buffer

fflush(fr);

fflush(fr3);

fclose(fr); //closing file after processing. It is important step

fclose(fr1);

fclose(fr3);

system("PAUSE");

return 0;

}

// Compare method which is comparing charaacter by character

int stringCompare(char str1[],char str2[]){

int i=0,flag=0;

while(str1[i]!='\0' && str2[i]!='\0'){

if(str1[i]!=str2[i]){

flag=1;

break;

}

i++;

}

if (flag==0 && (str1[i]=='\0' || str2[i]=='\0'))

return 1;

else

return 0;

}

You might be interested in
Within a word processing program, predesigned files that have layout and some page elements already completed are called text bo
Harlamova29_29 [1022]
I believe the answer is D. You're welcome:)
7 0
1 month ago
The Coins class was created to hold all your loose change, kind of like a piggy bank! For this exercise, you are going to simula
Amiraneli [1052]

Answer:

Coins c1 = new Coins (4, 3, 2, 1);

c1.bankValue();

c1.addQuarter();

c1.addQuarter();

c1.addDime();

c1.addDime();

c1.addPenny();

c1.bankCount();

c1.bankValue();

Explanation:

8 0
1 month ago
A computer’s memory is composed of 8K words of 32 bits each. How many bits are required for memory addressing if the smallest ad
Natasha_Volkova [1026]

Answer:

The required number of bits to address 8K words is 13.

Explanation:

Addressable words total 8000, where a word is defined as the smallest unit of memory that can be addressed.

These 8000 words can be accessed using 2^{n} units. To find the value of n corresponding to the number of words, you need to calculate

2^2=4\\2^4=16\\2^7=128\\2^{10}=1024\\2^{12}=4096\\2^{13}=8192

It's clear that 13 bits are necessary to address 8K words.

7 0
3 months ago
The email application used by Jim’s office is sending emails with viruses attached to them to user’s computers. What step should
Harlamova29_29 [1022]
Jim should first try D. to check for and install patches or software updates.
7 0
1 month ago
Other questions:
  • The Pentium 4 Prescott processor, released in 2004, had a clock rate of 3.6 GHz and voltage of 1.25 V. Assume that, on average,
    8·1 answer
  • When you add a zero to the right of a decimal number, it multiplies its value by 10 (For example, "15" becomes "150"). What simi
    10·1 answer
  • Why computer is known as versatile and diligent device ?explain​
    14·1 answer
  • An author is preparing to send their book to a publisher as an email attachment. The file on their computer is 1000 bytes. When
    6·1 answer
  • Write a function solution that returns an arbitrary integer which is greater than n.
    13·1 answer
  • Charlie has a large book collection. He was keeping track of it in a spreadsheet, but it has grown big enough that he wants to c
    10·1 answer
  • Allan needs to ensure that an object is in a very precise location on a slide. He decides to use the Ruler option to achieve thi
    5·2 answers
  • When performing actions between your computer and one that is infected with a virus which of the following offers no risk becomi
    5·1 answer
  • Write a function named firstLast2 that takes as input an array of integers and an integer that specifies how many entries are in
    14·1 answer
  • Insert the appropriate functions in the Summary Statistics section of the worksheet: cells H18:H22. Format the payments with Acc
    5·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!