1answer.
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]
3 years 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]3 years 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
Which is the last step in conducting a URL search?
dimulka [17.4K]
The user's browser renders the html code as a visual web page
3 0
4 years ago
I have been working on this python program for a hour now, and I am trying to figure out what I an doing wrong in the loop. I ha
Alona [7]

Answer:

The correct loop is as follows:

for year in range(starting_tuitionYear, ending_tuitionYear, increment):

   tuition = startingTuition + rate * startingTuition

   startingTuition = tuition

   print('Tuition for the year of ' + str(year)+ ' is ' + str(tuition));

Explanation:

Required

The correction to the attached program

Some variables are not needed; so, I've removed the redundant variables.

The main error in the program is in the loop;

After the tuition for each year has been calculated, the startTuition of the next year must be set to the current tuition

<em>See attachment for complete program</em>

7 0
3 years ago
________ are devices in a computer that are in either the on or off state
Vanyuwa [196]
The appropriate response is Electrical Switches. These are electromechanical gadgets that are utilized as a part of electrical circuits to control, recognize when frameworks are outside their working reaches, flag controllers of the whereabouts of machine individuals and work pieces, give a way to manual control of machine and process capacities, control lighting, et cetera.
6 0
4 years ago
Can you hook up an ethernet cable to a phone jack
laila [671]
The connector for ethernet cables is called RJ45. for phone jack is RJ11. Although they look the same, the RJ11 has only 4 leads, as opposed to 9 leads in RJ45. Also, RJ11 is not as wide as RJ45, so the connector does not fit.

Even if it would, the signals would be totally incompatible, so no success can be expected.
8 0
4 years ago
Which is currently the most common cellular network?<br> 4G<br> 4G LTE<br> 5G<br> 5G LTE
Misha Larkins [42]

Answer:

4GLte

Explanation:

8 0
3 years ago
Other questions:
  • A manufacturer of machine tools creates a spreadsheet of tools and the cost for those. The spreadsheet has four fields: name of
    13·1 answer
  • Is it better to meet online or offline<br> (Please answer QUICK)<br><br> Thanks :')
    5·2 answers
  • Question 2 (2 points)
    9·1 answer
  • - Explain the Windows User Authentication process for Client connecting to the domain
    11·1 answer
  • To find your personal learning style you can
    5·2 answers
  • Package Newton’s method for approximating square roots (Case Study: Approximating Square Roots) in a function named newton. This
    7·1 answer
  • Select the correct answer.
    10·1 answer
  • Which of the examples is part of client-side code?
    12·1 answer
  • Which of the following is true of functions?
    10·1 answer
  • Which option is an example of an algorithm that is used in daily life?
    8·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!