I'm writing a program which finds and replaces characters in an input file, and print the file content in an output file. I'm able to write the program using string method. But requirement is to use character method. Except for the 4 input parameters I do not know how to modify the rest codes using character and its methods. Could anyone please help? Thank you.
Instead of getline(stream, string), use cin.getline (char* s, streamsize n ); where s is a buffer and n is the length of that buffer (null terminator will be written for you).
e.g.
1 2
char line[1000];
cin.getline(line, 1000);
To find a substring, research strstr.
To replace characters, probably just write the logic yourself by iterating through each character and shifting them.
Be sure to #include <cstring> when using functions meant for c-style strings.
int main(int argc, char *argv[])
{
//string inFile = string(argv[1]); //input file name
//string outFile = string(argv[2]); //output file name
//string chrToFind = string(argv[3]); //characters to find
//string chrToReplace = string(argv[4]);//characters to replace
if (argc != 5)
{
std::cerr <<
"\n Inproper arguements on command line!\n""\n Should be input file name output file name character to find character to replace.\n";
return 1;
}
//modify above 4 parameters to below
constchar *const inFileName = argv[1];
constchar *const outFileName = argv[2];
char *chrToFind = argv[3];
char *chrToReplace = argv[4];
ifstream inFile(inFileName);
if (!inFile)
{
std::cerr << "\n File \"" << inFileName << "\" did not open!\n";
return 2;
}
ofstream outFile(outFileName);
It would be a good idea to check "argc" before you start using "argv[1] - [4]".
When opening an input file it prety must a must to check that the file is open and usable.
The way you are using the "ofstream" it will create the file, in the current working directory, if it does not exist. If you use a path you will need to check it as I did for the input.
An alternative is: while (inFile.get(ch)). Then inside the loop just check the character read against "charToFind" and output the replacement or the character (ch) read.
One tip when you use "charToFind" in the if statement do not forget to de-reference it.
#include <fstream>
#include <iostream>
int main(int argc, char *argv[])
{
int count;
std::cout << "This program was called with " << argv[0] << '\n';
if (argc > 1)
{
for (count = 1; count < argc; count++)
{
std::cout << "argv[" << count << "] " << argv[count] << '\n';
}
}
else
{
std::cout << "The command had no other arguments.\n";
return -99;
}
char change_from = argv[1][0];
char change_to = argv[2][0];
std::cout << "Change from: " << change_from << '\n';
std::cout << "Change to: " << change_to << '\n';
// SOURCE FILE
std::ifstream file_from;
file_from.open("ch_read_from.txt");
if (file_from.fail())
{
std::cout << "Error opening file: ";
exit(1);
}
// DESTINATION FILE
std::ofstream file_to;
file_to.open("ch_write_to.txt");
if (file_to.fail())
{
std::cout << "Error opening file: ";
exit(2);
}
// PROCESS CHARACTER AT A TIME
char ch_read;
while (file_from.get(ch_read))
{
if(ch_read == change_from)
{
file_to << change_to;
}
else
{
file_to << ch_read;
}
}
std::cout << "Done\n";
return 0;
}
Mac terminal input first line, then output ..
./General e U
This program was called with ./General
argv[1] e
argv[2] U
Change from: e
Change to: U
Done
Input file:
The Constitution of the People's Republic of China is nominally the supreme
law of the People's Republic of China. It was adopted by the 5th National
People's Congress on December 4, 1982, with further revisions about every
five years.
It is the fourth constitution in the country's history,
superseding the 1954 constitution, the 1975 constitution, and the 1978
constitution.
Output file:
ThG Constitution of thG PGoplG's RGpublic of China is nominally thG suprGmG
law of thG PGoplG's RGpublic of China. It was adoptGd by thG 5th National
PGoplG's CongrGss on DGcGmbGr 4, 1982, with furthGr rGvisions about GvGry
fivG yGars.
It is thG fourth constitution in thG country's history,
supGrsGding thG 1954 constitution, thG 1975 constitution, and thG 1978
constitution.
It is a little late, but now is a good time to post the full description of what the program should do, so that everyone will know what should be done.