strlen probs

This is a discussion on strlen probs within the C++ Programming forums, part of the General Programming Boards category; this is me code, i need strlen to recognise the sapces, how do you do this. Code: #include <iostream> #include ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    142

    strlen probs

    this is me code, i need strlen to recognise the sapces, how do you do this.

    Code:
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    int main () {
    
    
    	char * buffer;
    	char tbuffer[100];
    	int len;
    	int leng;
    
    	ofstream a;
    	a.open("test.txt",ios::binary);
    
    cout<<"please type a sentence > ";
    cin>> tbuffer;
    len=strlen(tbuffer);
    a.write(tbuffer,len);
    tbuffer[len]=0;
    cout<< strlen(tbuffer);
    cin.get();
    a.close();
    
    
    }
    WhAtHA hell Is GoInG ON

  2. #2
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    It's not strlen that gives you a wrong result its's the operator >> that doesn't read strings containing spaces.
    You have to use getline() to read a whole line of text including spaces.
    Kurt

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    142
    can you show me an example cause i don't want to read the line i need the number of letters
    WhAtHA hell Is GoInG ON

  4. #4
    Registered User
    Join Date
    Sep 2005
    Posts
    142
    ok i see what you are saying

    cin>> won't read the spaces
    use getline

    cool cheers
    WhAtHA hell Is GoInG ON

  5. #5
    Registered User
    Join Date
    Sep 2005
    Posts
    142
    but i'm using binary mode on my io,,, which doesn't recognise getline....

    what should i use instead
    WhAtHA hell Is GoInG ON

  6. #6
    Registered User
    Join Date
    Nov 2005
    Posts
    85
    dont use ios::binary leave that parameter out then try cin.getline(tbuffer);

  7. #7
    Registered User
    Join Date
    Sep 2005
    Posts
    142
    what is the difference between binary and no binary LOL...
    WhAtHA hell Is GoInG ON

  8. #8
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Basically it's just the way the end of line is handled. in textmode if you read or write a '\n' from or to the stream the '\n' is translated to whatever end of line marks your OS uses. In binary mode this translation is not done.
    Kurt

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Playing around strlen :)
    By audinue in forum C Programming
    Replies: 6
    Last Post: 06-13-2008, 04:22 PM
  2. strlen in expressions
    By justforthis1 in forum C++ Programming
    Replies: 4
    Last Post: 10-24-2006, 11:28 AM
  3. strlen()
    By exoeight in forum C Programming
    Replies: 9
    Last Post: 04-01-2005, 10:18 AM
  4. Just say NO to strlen.
    By anonytmouse in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 02-11-2005, 01:34 PM
  5. Why O why, strlen?
    By Sebastiani in forum C Programming
    Replies: 11
    Last Post: 08-24-2001, 02:41 PM

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