-
April 16th, 2011, 01:24 PM
#1
Code Help!
My program is supposed to read in a file and a word from the user then replace every instance of the word inside the program with another word entered in by the user. I keep getting function errors and cant get past the compiling process
Here is my code:
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
int main()
{
//Varibles needed to make sure that the program will run effectively
fstream dataFile;
char *buffer;
string find, change;
int count = 0;
int length;
//Opening the data file to be a input and output (Read and Write)
dataFile.open("input.txt", ios::in|ios:ut);
//Testing to make the file opened
if (!dataFile)
{
cout << "File open error!" << endl;
return 0;
}
//Getting the length of the file
dataFile.seekg (0, ios::end);
length = dataFile.tellg();
dataFile.seekg (0, ios::beg);
//allocating memory for the file
buffer = new char [length];
//Read the buffer into the file
dataFile.get(*buffer);
//Asking the user what word would they like to search for
cout << "What word do you want to search for?" << endl;
cin >> find;
//Asking the user what word they want to use to replace
cout << "What word do you want to replace the first word with?" << endl;
cin >> change;
while (!dataFile.eof())
{
do {
if (strstr(find,*buffer) == 0)
{
count++;
}
} while (!dataFile.eof());
dataFile.replace (buffer, find, change);
}
cout << buffer;
return 0;
}
-
April 16th, 2011, 01:40 PM
#2
Re: Code Help!
Welcome to CodeGuru.
Please use code tags to make your code readable. Also, it helps if you post the errors so we don't have to guess.
a few things ...
This is not the way to check if the file is open.
Code:
//Read the buffer into the file
dataFile.get(*buffer);
You need to pass the buffer itself, not the content of the buffer.
Code:
while (!dataFile.eof())
{
do {
if (strstr(find,*buffer) == 0)
{
count++;
}
} while (!dataFile.eof());
You already read the file into the buffer, so the 'while !eof' is totally useless.
-
April 16th, 2011, 01:51 PM
#3
Re: Code Help!
Ok thanks!, this is my first post so im still getting the hang of it. the errors I keep getting are
error: no matching function for call to 'strstr(std::string*, char&)'
and
error: 'struct std::fstream' has no member named 'replace'
and i will re-do my code with code tags
Code:
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
int main()
{
//Varibles needed to make sure that the program will run effectively
fstream dataFile;
char *buffer;
string find, change;
int count = 0;
int length;
//Opening the data file to be a input and output (Read and Write)
dataFile.open("input.txt", ios::in|ios:ut);
//Testing to make the file opened
if (!dataFile)
{
cout << "File open error!" << endl;
return 0;
}
//Getting the length of the file
dataFile.seekg (0, ios::end);
length = dataFile.tellg();
dataFile.seekg (0, ios::beg);
//allocating memory for the file
buffer = new char [length];
//Read the buffer into the file
dataFile.get(*buffer);
//Asking the user what word would they like to search for
cout << "What word do you want to search for?" << endl;
cin >> find;
//Asking the user what word they want to use to replace
cout << "What word do you want to replace the first word with?" << endl;
cin >> change;
while (!dataFile.eof())
{
do {
if (strstr(find,*buffer) == 0)
{
count++;
}
} while (!dataFile.eof());
dataFile.replace (buffer, find, change);
}
cout << buffer;
return 0;
}
-
April 16th, 2011, 01:58 PM
#4
Re: Code Help!
error: 'struct std::fstream' has no member named 'replace'
That's correct. here is the help on fstream, and as you can see, there is no function called replace.
error: no matching function for call to 'strstr(std::string*, char&)'
Take a look at how strstr works.
-
April 17th, 2011, 12:13 PM
#5
Re: Code Help!
Thanks i think i got a handle on it now.
-
April 18th, 2011, 06:41 AM
#6
Re: Code Help!
A few notes:
1. You forgot to:
2. You will need to be careful if the word you are replacing is not the
same size as the new word. Using std::string instead of a char array
would probably be easier, since it has find and replace member functions.
3. The following is perfectly valid ... (as is using is_open())
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|