Code Help!
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Code Help!

  1. #1
    Join Date
    Apr 2011
    Posts
    3

    Exclamation 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;
    }

  2. #2
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    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 ...

    Code:
    if (!dataFile)
    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.

  3. #3
    Join Date
    Apr 2011
    Posts
    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;
    }

  4. #4
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    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.

  5. #5
    Join Date
    Apr 2011
    Posts
    3

    Re: Code Help!

    Thanks i think i got a handle on it now.

  6. #6
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,640

    Re: Code Help!

    A few notes:

    1. You forgot to:

    Code:
    #include <string>
    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())
    Code:
    if  (!dataFile)

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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center