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
Fantom [35]
1 year 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:
Serggg [28]1 year ago
7 0

Answer:

See explaination

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
Jim is writing a program to calculate the wages of workers in a teddy bear factory.
34kurt

Answer:

The algorithm is as follows;

1. Start

2. Input TeddyBears

3. Input Hours

4. WagebyTeddy = 2 * TeddyBears

5. WagebyHour = 5 * Hours

6. If WagebyHour > WagebyTeddy then

6.1 Print WagebyHour

7. Else

7.1. Print WagebyTeddy

8. Stop

Explanation:

The following variables are used;

TeddyBears -> Number of teddy bears made

Hours -> Number of Hours worked

WagebyTeddy -> Wages for the number of teddy bears made

WagebyHour -> Wages for the number of hours worked

The algorithm starts by accepting input for the number of teddy bears and hours worked from the user on line 2 and line 3

The wages for the number of teddy bears made  is calculated on line 4

The wages for the number of hours worked  is calculated on line 5

Line 6 checks if wages for the number of hours is greated than wages for the number of bears made;

If yes, the calculated wages by hour is displayed

Otherwise

the calculated wages by teddy bears made is displayed

3 0
2 years ago
Write a copy constructor for carcounter that assigns origcarcounter.carcount to the constructed object's carcount. sample output
Drupady [299]

#include <iostream>

using namespace std;

class CarCounter {

  public:

     CarCounter();

     CarCounter(const CarCounter& origCarCounter);

     void SetCarCount(const int count) {

         carCount = count;

     }

     int GetCarCount() const {

         return carCount;

     }

  private:

     int carCount;

};

CarCounter::CarCounter() {

  carCount = 0;

  return;

}

CarCounter::CarCounter(const CarCounter &p){

carCount = p.carCount;

}

void CountPrinter(CarCounter carCntr) {

  cout << "Cars counted: " << carCntr.GetCarCount();

  return;

}

int main() {

  CarCounter parkingLot;

  parkingLot.SetCarCount(5);

  CountPrinter(parkingLot);

  return 0;

}

Sample output:  

Cars Counted: 5

8 0
2 years ago
Read 2 more answers
Replace the underlines with the words in parentheses that follow: the ____ solves the ____ of a ____ by expressing an ____ in a
adell [148]

The programmer solves the problems of a user by expressing an algorithm in a programming language to make a program that can run on a computer.

5 0
2 years ago
Suppose your name was George Gershwin. Write a complete program that would print your last name, followed by a comma, followed b
NNADVOKAT [17]

Answer:

I will write the code in C++ and JAVA      

Explanation:

<h2>JAVA CODE</h2>

public class Main

{ public static void main(String[] args) {

       // displays Gershwin,George

     System.out.println("Gershwin,George"); }  }

<h2>C++ Code:</h2>

#include <iostream>

using namespace std;  

int main()

{  cout<<"Gershwin,George"; }    

// displays last name Gershwin followed by , followed by first name George

//displays Gershwin,George as output.

6 0
2 years ago
Read 2 more answers
Suppose your company has decided that it needs to make certain busy servers faster. Processes in the workload spend 60% of their
vekshin1

Answer:

CPU need 50% much faster

disk need 100% much faster

Explanation:

given data

workload spend time CPU  = 60%

workload spend time I/O = 40%

achieve overall system speedup = 25%

to find out

How much faster does CPU need and How much faster does the disk need

solution

we apply here Amdahl’s law for the overall speed of a computer that is express as

S = \frac{1}{(1-f)+ \frac{f}{k} }      .............................1

here f is fraction of work i.e 0.6 and S is overall speed  i.e 100% + 25% = 125 % and k is speed up of component

so put all value in equation 1 we get

S = \frac{1}{(1-f)+ \frac{f}{k} }  

1.25 = \frac{1}{(1-0.6)+ \frac{0.6}{k} }  

solve we get

k = 1.5

so we can say  CPU need 50% much faster

and

when f = 0.4 and S = 125 %

put the value in equation 1

S = \frac{1}{(1-f)+ \frac{f}{k} }  

1.25 = \frac{1}{(1-0.4)+ \frac{0.4}{k} }  

solve we get

k = 2

so here disk need 100% much faster

7 0
2 years ago
Other questions:
  • U.S. industries like steel, computers, and energy need to be protected from foreign competition to ensure which of the following
    6·2 answers
  • Use a VLOOKUP function in cell I5 to identify and calculate the federal withholding tax. Use the tax rates from the range D21:E2
    15·1 answer
  • Manny is a personal trainer. He gives his client an endurance test each week. He would like to illustrate how much the client ha
    7·2 answers
  • Propane also known as LP gas, is often mixed with about _______ percent of other gases, such as butane, propylene, and mercaptan
    13·1 answer
  • An electronics store purchased a CD player at a wholesale price of $60 and then sold it at a 40 percent discount off the origina
    13·1 answer
  • Convert the following binary number to octal (111000)2​
    12·1 answer
  • Which method allows a computer to react accordingly when it requests data from a server and the server takes too long to respond
    5·1 answer
  • Which of the following Teacher Tips would NOT be helpful when trying to select content from the Chrome Web Store? "Can be used a
    9·1 answer
  • Which of the following is true of how computers represent numbers?
    9·2 answers
  • Nstructions
    7·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!