Strange problems with string class

This is a discussion on Strange problems with string class within the C++ Programming forums, part of the General Programming Boards category; Hey Guys. This is a weird one. Basically what I am doing is reading a string from the file using ...

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    34

    Strange problems with string class

    Hey Guys. This is a weird one. Basically what I am doing is reading a string from the file using getline(file, buffer) where buffer is a C++ string. For example: lets say the file contains the string "hello" without the quotes on one line. After I read the file and print out the buffer it says "hello" (cout << buffer) like it should..BUT when I do this:

    buffer == "hello" , the answer is false. Why? I can't seem to see why. Furthermore, when I do cout << buffer << "." << endl;

    I get this output:

    .ello

    Why is this happening? In case you are wondering the file does not contain any blank spaces or lines after the string "hello". Any ideas?

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    Can you post your code, it sounds like it is only a few lines long.

  3. #3
    Registered User
    Join Date
    Jun 2007
    Posts
    34
    Here is my code. I am using g++ on ubuntu linux btw.

    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    using namespace std;
    
    int main()
    {
      string buffer;
      ifstream file;
      file.open("tfile.txt");
      getline(file, buffer);
      cout << buffer<<endl;
      if(buffer == "hello")
      {
        cout << "match" <<endl;
      }
      return 0;
    }

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    Change this:
    Code:
      getline(file, buffer);
      cout << buffer<<endl;
    to this:
    Code:
      if (!file.is_open())
      {
        cout << "Error opening file.";
        return 1;
      }
    
      getline(file, buffer);
      cout << "-->" << buffer << "<--" << endl;
    What's the output?

  5. #5
    Registered User
    Join Date
    Jun 2007
    Posts
    34
    The output is:

    <--hello

  6. #6
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Your program returns "hello" and "match" for me, and appears to contain no errors.

    I suspect tfile.txt does not contain what you think it contains. Your note about getting ".ello" for output makes it look like you've got a "\r" in the buffer somehow - check your file. It should be 5 bytes, exactly. (Use a hex editor to see what's really in the file.)
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  7. #7
    Registered User
    Join Date
    Jun 2007
    Posts
    34
    I think you are right. My file is 7 bytes somehow. I don't quite understand why, since hello is the only thing in it. I examined it in hex editor, and it appears to confirm it. I attached the file here too. I don't understand why its there, and how it gets there, very weird.
    Attached Files Attached Files
    Last edited by creativeinspira; 07-17-2007 at 09:55 PM.

  8. #8
    Registered User
    Join Date
    Jun 2007
    Posts
    34
    Yeah, thanks everybody I fixed it.

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    I think it's because you switched platforms, specifically, line endings. If you created tfile.txt under DOS or Windows and then ran your program under Linux (i.e., Ubuntu), that's exactly what would happen.

    Here's why. From http://en.wikipedia.org/wiki/Newline
    Code:
        * LF:    Multics, Unix and Unix-like systems (GNU/Linux, AIX, Xenix, Mac OS X, etc.), BeOS, Amiga, RISC OS, and others
        * CR+LF: DEC RT-11 and most other early non-Unix non-IBM OSes, CP/M, MP/M, MS-DOS, OS/2, Microsoft Windows
        * CR:    Commodore machines, Apple II family and Mac OS up to version 9
    DOS line endings are CR+LF, so that's what your file would contain. Then, reading it under Linux, which has LF line endings, the CR would be part of the previous line. With Macs, the opposite problem would occur: each line (except the first) would start with a LF.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #10
    Massively Single Player AverageSoftware's Avatar
    Join Date
    May 2007
    Location
    Buffalo, NY
    Posts
    141
    Quote Originally Posted by dwks View Post
    With Macs, the opposite problem would occur: each line (except the first) would start with a LF.
    Not quite, with crusty old Macs the opposite problem would occur. Mac OS X is firmly in the Unix camp, as evidenced by your list.
    There is no greater sign that a computing technology is worthless than the association of the word "solution" with it.

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Yes, you're right. I didn't know that . . . even though it was in my post.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 03-03-2006, 01:11 AM
  2. Problems with my custom string class
    By cunnus88 in forum C++ Programming
    Replies: 15
    Last Post: 11-15-2005, 07:21 PM
  3. io stream and string class problems
    By quizteamer in forum C++ Programming
    Replies: 2
    Last Post: 04-25-2005, 12:07 AM
  4. class object manipulation
    By guda in forum C++ Programming
    Replies: 2
    Last Post: 10-09-2004, 10:43 AM
  5. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 09:33 PM

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