Question

In: Computer Science

Write a C program that can search for a string within a text file, replace it...

Write a C program that can search for a string within a text file, replace it with another string and put results in a new file.

Solutions

Expert Solution

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void main()
{
//variable declaration
FILE *fptr, *outputfp;
char ch;
int wrd=1,charctr=1;
char fname[20];

//open file
fptr=fopen("inputFile.txt","r");
outputfp = fopen("outputFile.txt", "w");
char str[100];
char newStr[100];
char searchStr[] = "how";
char replaceStr[] = "done";
  
if(fptr==NULL || outputfp==NULL)
{
printf(" File does not exist or can not be opened.");
}
else
{
ch=fgetc(fptr);
int i = 0;
  
char c;
while(ch!=EOF)
{
str[i++] = ch;
ch=fgetc(fptr);
}
str[i++] = '\0';
}
  
int i = 0, j = 0;
int flag = 0, start = 0;
while (str[i] != '\0')
{
if (str[i] == searchStr[j])
{
if (!flag)
start = i;
j++;
if (searchStr[j] == '\0')
break;
flag = 1;
}
else
flag = start = j = 0;
i++;
}
  
  
if (searchStr[j] == '\0' && flag)
{
for (i = 0; i < start; i++)
putc(str[i], outputfp);

for (j = 0; j < strlen(replaceStr); j++)
{
putc(replaceStr[j], outputfp);
i++;
}

for (j = start + strlen(searchStr); j < strlen(str); j++)
{
putc(str[j], outputfp);
i++;
}

newStr[i] = '\0';
}
  
//close file
fclose(fptr);
fclose(outputfp);
}

INPUT:

inputFile.txt

Search for a string = "how"

Replace with a string = "done"

outputFile.txt


Related Solutions

Write a C++ program that reads a string from a text file and determines if the...
Write a C++ program that reads a string from a text file and determines if the string is a palindrome or not using stacks and queue
Create this C++ program using classes 1. Create a file text file with a string on...
Create this C++ program using classes 1. Create a file text file with a string on it 2. Check the frecuency of every letter, number and symbol (including caps) 3. Use heapsort to sort the frecuencys found 4. Use huffman code on the letters, symbols or numbers that have frecuencys I created the file, and the frecuency part but i'm having trouble with the huffman and heapsort implementation.
Write a C++ program to create a text file. Your file should contain the following text:...
Write a C++ program to create a text file. Your file should contain the following text: Batch files are text files created by programmer. The file is written in notepad. Creating a text file and writing to it by using fstream: to write to a file, you need to open thew file as write mode. To do so, include a header filr to your program. Create an object of type fsrteam. Open the file as write mode. Reading from a...
Write a C program that Reads a text file(any file)  and writes it to a binary file....
Write a C program that Reads a text file(any file)  and writes it to a binary file. Reads the binary file and converts it to a text file.
Write a program to remove extra blanks from text files. Your program should replace any string...
Write a program to remove extra blanks from text files. Your program should replace any string of two or more blanks with one single blank. It should work as follows: * Create a temporary file. * Copy from the input file to the temporary file, but do not copy extra blanks. * Copy the contents of the temporary file back into the original file. * Remove the temporary file. Your temporary file must have a different name than any existing...
Create C# code that can search a text file and output the data at the line...
Create C# code that can search a text file and output the data at the line number inputted and amount of entries needed. Example of call in command window: Search16s filename.txt 273   10 Where 273 is the line number to start the output from, and 10 is the number of sequences that the program should output. The number of sequences entered on call should always be a odd number or give an error in console. The output should also display...
Write a java program: Write a program that creates a text file. Write to the file...
Write a java program: Write a program that creates a text file. Write to the file three lines each line having a person's name. In the same program Append to the file one line of  'Kean University'.  In the same program then Read the file and print the four lines without lines between.
In C++, write a program that reads data from a text file. Include in this program...
In C++, write a program that reads data from a text file. Include in this program functions that calculate the mean and the standard deviation. Make sure that the only global variables are the actual data points, the mean, the standard deviation, and the number of data entered. All other variables must be local to the function. At the top of the program make sure you use functional prototypes instead of writing each function before the main function... ALL LINES...
Using C++, write a code that this program always stores text file output into a text...
Using C++, write a code that this program always stores text file output into a text file named "clean.txt". -The program should read one character at a time from "someNumbers.txt", and do the following. -If it is a letter, print that letter to the screen, AND also store it in the text file. All letters should be converted to lowercase beforehand. -If it is a number, print that number to screen, but do NOT store it in the text file....
For C++ Write a program that opens a specified text file then displays a list of...
For C++ Write a program that opens a specified text file then displays a list of all the unique words found in the file. Hint: Store each word as an element of a set.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT