Thread: stream seekp doesn't work with trailing space

  1. #1
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463

    stream seekp doesn't work with trailing space

    I have this function to double a string. It puts a string into a stringstream and uses seekp to replace the old value with new value.

    Code:
    std::string double_str(const std::string & input, std::streamoff offset){
      std::stringstream ss(std::ios::in | std::ios::out);
      ss.str(input);
     
      ss.seekg(offset, std::ios::end);
      int val;
      ss >> val;
      val *= 2;
     
      ss.seekp(offset,std::ios::end);
      ss << val;
      return ss.str();
    }
    When the input string doesn't have the trailing space, seekp doesn't put the new val back into the stream.

    Code:
      std::cout << double_str("234",-3) << '\n';  // return 234
      std::cout << double_str("234 ",-4) << '\n'; // return 468
    What did i do wrong that operator<< fails?
    "All that we see or seem
    Is but a dream within a dream." - Poe

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Could the eofbit be set because it hit the end of the string?
    Test ss.eof() to see.
    Call ss.clear() to reset so the seekp will (hopefully) work.

  3. #3
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    Quote Originally Posted by algorism View Post
    Could the eofbit be set because it hit the end of the string?
    Test ss.eof() to see.
    Call ss.clear() to reset so the seekp will (hopefully) work.
    you're right, eofbit is set and calling ss.clear works. Could you how seekp got eof?
    "All that we see or seem
    Is but a dream within a dream." - Poe

  4. #4
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    The eofbit is set when the end of the stream (string) is encountered. When there's extra space after the number, the number is read without the eof being detected. When there's no space the eof is detected and the eofbit is set so that the next read will fail. With normal textual stream input you have at least a newline between the data and the actual eof.

    In the example below, stringstream and fstream act the same way.
    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    #include <sstream>
    using namespace std;
    
    void g(iostream& ios, int off) {
        int n;
        ios.seekg(off);
        ios >> n;
        cout << n << '\n';
        if (ios.eof()) {
            ios.clear();
            cout << "eof\n";
        }
        ios.seekp(off);
        ios << n * 2;
    }
    
    void f(iostream& ios) {
        g(ios, 3);
        g(ios, 9);
    }
    
    int main() {
        string s = "abc123def456";
    
        // create file with string s and no newline
        fstream fs("f123.txt", ios_base::out);
        fs << s;
        fs.close();
    
        // try stringstream
        stringstream ss(s);
        cout << ss.str() << '\n';
        f(ss);
        cout << ss.str() << '\n';
    
        // try fstream
        fs.open("f123.txt", ios_base::in|ios_base::out);
        f(fs);
        fs.close();
    
        // display file
        fs.open("f123.txt");
        fs >> s;
        cout << s << '\n';
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 12-07-2010, 06:53 AM
  2. trailing zero in memory stream
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 06-11-2008, 01:30 AM
  3. Why my if doesn't work?
    By Netflyer in forum C Programming
    Replies: 10
    Last Post: 01-14-2008, 02:39 PM
  4. trim heading and trailing space
    By George2 in forum C Programming
    Replies: 1
    Last Post: 10-20-2007, 11:41 PM
  5. my function doesn't work! it should work
    By Unregistered in forum C Programming
    Replies: 13
    Last Post: 05-02-2002, 02:53 PM

Tags for this Thread