CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 3 FirstFirst 123 LastLast
Results 16 to 30 of 37
  1. #16
    Join Date
    Oct 2007
    Posts
    32

    Re: Editing a single word in a file

    I found the problem is indeed with the "(getline(infile, messageword, '\n'))". Is there any way to have more than one delimiter in this? My instruction say "Allow words to be separated by newlines, blanks, tabs, formfeeds, and punctuation symbols like commas, periods, single and double quotes, and parentheses." I was just trying to get the basics before worrying about all that. I tried putting two conditions in the line, something like
    "(getline(infile, messageword, '\n' || ' '))", but that doesn't work.

  2. #17
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Editing a single word in a file

    Unless you have some pressing need to get an entire line at once, you should normally stick to the >> operator. That automatically skips all whitespace.

  3. #18
    Join Date
    Apr 2008
    Posts
    26

    Re: Editing a single word in a file

    Quote Originally Posted by veronicak5678
    I found the problem is indeed with the "(getline(infile, messageword, '\n'))". Is there any way to have more than one delimiter in this? My instruction say "Allow words to be separated by newlines, blanks, tabs, formfeeds, and punctuation symbols like commas, periods, single and double quotes, and parentheses." I was just trying to get the basics before worrying about all that. I tried putting two conditions in the line, something like
    "(getline(infile, messageword, '\n' || ' '))", but that doesn't work.
    Code:
    #include <iostream>
    #include <iomanip>
    #include <fstream>
    #include <string>
    using namespace std;
    
    int main()
    {
     
        string x;
        ifstream inFile;
        
        inFile.open("test.txt");
        if (!inFile) {
            cout << "Unable to open file";
            exit(1); // terminate with error
        }
        
        while (inFile >> x) {
    	cout<<"x : "<<x<<endl;        
        }
        
        inFile.close();
     
        return 0;
    }
    Use something like this

  4. #19
    Join Date
    Oct 2007
    Posts
    32

    Re: Editing a single word in a file

    Then how do I write it back to the message file with all the whitespace and parentheses and everything else?

  5. #20
    Join Date
    Oct 2007
    Posts
    32

    Re: Editing a single word in a file

    And any idea how I can change all but the first letter? I could do this in a 2D array, but in a vector...?

  6. #21
    Join Date
    Apr 2008
    Posts
    26

    Re: Editing a single word in a file

    read up on how to read and write binary files reading seekp/seekg and tellp/tellg

  7. #22
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Editing a single word in a file

    That's a good point.....you could certainly use tellp after each read to get the ending position in the file. You would store this along with the actual word in the vector----probably it would be a vector< pair<string,streampos> > then----and use this information to control your write-back.

    The key observations would be that a word starts string::size() bytes prior to the tellp() position you got after reading it, and that you'd need to make sure you wrote exactly string::size() '*' characters so as not to mess up the rest of the file.

  8. #23
    Join Date
    Apr 2008
    Posts
    26

    Post Re: Editing a single word in a file

    Quote Originally Posted by Lindley
    That's a good point.....you could certainly use tellp after each read to get the ending position in the file. You would store this along with the actual word in the vector----probably it would be a vector< pair<string,streampos> > then----and use this information to control your write-back.

    The key observations would be that a word starts string::size() bytes prior to the tellp() position you got after reading it, and that you'd need to make sure you wrote exactly string::size() '*' characters so as not to mess up the rest of the file.
    You can do the same by the following
    Code:
    //GLOBAL DEFINITION
    int ast_length=0;
    char *ast;
    
    
    //WITHIN THE LOOP
    {
           ast_length=inFile.tellp() - x.length()
           ast = new char[ast_length];
           for (int i=0;i<ast_length;i++;
                   ast[i]='*';
        //MORE CODE TO SUBSTITUTE VECTOR DATA
            delete [] ast;
    }

  9. #24
    Join Date
    Oct 2007
    Posts
    32

    Re: Editing a single word in a file

    Sorry, but I've never even seen a pair vector before. Would I do two pushbacks in a row? And how do I use that to dictate my writing out?

  10. #25
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Editing a single word in a file

    A std:air is constructed using make_pair(), and the members are accessed at .first and .second.

  11. #26
    Join Date
    Apr 2008
    Posts
    26

    Re: Editing a single word in a file

    You can actually create a Vector of a struct
    Code:
    struct words
    {
          string word;
          int pos;
    };

  12. #27
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Editing a single word in a file

    Well, if you're going to modify words in-place anyway, there's no need to represent the file as a vector at all. You could just read a word, check if it's on the list, replace it if so, and move on.

  13. #28
    Join Date
    Apr 2008
    Posts
    26

    Re: Editing a single word in a file

    Quote Originally Posted by Lindley
    Well, if you're going to modify words in-place anyway, there's no need to represent the file as a vector at all. You could just read a word, check if it's on the list, replace it if so, and move on.
    I agree with Lindley here. You can read the file with the censored words into an array or a Vector (safer option)

  14. #29
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Editing a single word in a file

    Well, that's not quite what I was saying, but that's valid also. Instead of using a vector of strings, you could just use a single string for the entire file, using cin.get() to pull in characters individually and appending them with +=.

    You could then do your replacement on this string using std::string::find(). Of course, your find routine would be slightly more complicated due to the need to check all the words in the censored list, and the fact that find() only returns the first instance after the specified index.

  15. #30
    Join Date
    Apr 2008
    Posts
    26

    Re: Editing a single word in a file

    Quote Originally Posted by Lindley
    Well, that's not quite what I was saying, but that's valid also. Instead of using a vector of strings, you could just use a single string for the entire file, using cin.get() to pull in characters individually and appending them with +=.

    You could then do your replacement on this string using std::string::find(). Of course, your find routine would be slightly more complicated due to the need to check all the words in the censored list, and the fact that find() only returns the first instance after the specified index.
    In short there is not optimal way of doing things here

Page 2 of 3 FirstFirst 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured