Read from text file to a string

This is a discussion on Read from text file to a string within the C++ Programming forums, part of the General Programming Boards category; If the whole string is a white space, my startPos will be 0 and endPos(length of string) will be -1. ...

  1. #16
    Apprentice
    Join Date
    Oct 2006
    Location
    LA
    Posts
    53
    If the whole string is a white space, my startPos will be 0 and endPos(length of string) will be -1. Wouldn't that truncate the string to an empty string? Or am I missing something here?

  2. #17
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,758
    Quote Originally Posted by lazyme
    If the whole string is a white space, my startPos will be 0 and endPos(length of string) will be -1.
    Actually, it will be std::string::npos, which is not -1, even though it is equal to -1 converted to std::string::size_type, which is an unsigned integer type.

    Quote Originally Posted by lazyme
    Wouldn't that truncate the string to an empty string? Or am I missing something here?
    No. It would get the substring of the string from the character at index 0 to the last character of the string.

    By the way, why did you not test before asking?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #18
    Apprentice
    Join Date
    Oct 2006
    Location
    LA
    Posts
    53
    Sorry, I just got a bit confused.
    I have made changes to my function and I think this should work fine even when the whole string is a whitespace.

    Code:
    if(endPos!=std::string::npos)
    {
    	endPos = (endPos+1)-startPos;
    }
    else
    {
    	endPos = 0;
    }

  4. #19
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,758
    Quote Originally Posted by lazyme
    I have made changes to my function and I think this should work fine even when the whole string is a whitespace.
    It looks correct now, but you should test.

    Now, notice that if std::string::npos is returned by find_first_not_of, it will also be be returned by find_last_not_of. This suggests that you can handle the case of a string consisting only of whitespace separately, and thus simplify your code.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #20
    Apprentice
    Join Date
    Oct 2006
    Location
    LA
    Posts
    53
    Does this code look better?

    Code:
    void trim( std::string& strToBeFormatted )
    {
    	size_t startPos = strToBeFormatted.find_first_not_of("\n\t ");
    	size_t endPos = strToBeFormatted.find_last_not_of("\n\t ");
    
    	if(startPos==std::string::npos)
    	{
    		startPos = 0;
    		endPos = 0;
    	}
    	else
    	{
    		endPos = (endPos+1)-startPos;
    	}
    
    	strToBeFormatted = strToBeFormatted.substr( startPos, endPos );
    }

  6. #21
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,758
    What I had in mind:
    Code:
    void trim(std::string& subject)
    {
        if (!subject.empty())
        {
            std::string::size_type startPos = subject.find_first_not_of("\n\t ");
            if (startPos != std::string::npos)
            {
                std::string::size_type endPos = subject.find_last_not_of("\n\t ");
                subject = subject.substr(startPos, endPos - startPos + 1);
            }
            else
            {
                subject.clear();
            }
        }
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #22
    Apprentice
    Join Date
    Oct 2006
    Location
    LA
    Posts
    53
    Your code definitely looks much better. I hope with more practice I would start thinking on the same lines and write better code.

    Thanks laserlight

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  3. Replies: 4
    Last Post: 03-03-2006, 01:11 AM
  4. Replies: 1
    Last Post: 09-10-2005, 06:02 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21