CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Mar 2001
    Location
    Belgrade, Serbia
    Posts
    629

    unable to recognise 'space' char in ifstream

    hi,

    I'm stuck with a quite stupid problem....

    I'm trying to parse a txt file, and seems like I cannot recognise 'space' ( asc(32) char) .... concider this:

    ifstream input("some_text.txt");
    while (input >> * pszFile)
    {
    // ....
    if (* pszFile == 32) // space char
    // do smth...


    all other chars are recognised correctly, only 'space' not.

    what am I doing wrong??

    thx for reading

  2. #2
    Join Date
    May 2000
    Location
    Washington DC, USA
    Posts
    715
    >> what am I doing wrong..

    what your doing wrong is using the >> operator to extract your characters. Using this it seemed to skip spaces. I'm not sure if this is a bug or a feature. But to get the results you need use a different method for sucking out the characters...

    Here is code which worked for me..


    char szFile = ' ';
    int i = 0;

    ifstream input("\\foo.txt", ios::binary);
    while ( input.get(szFile) )
    {
    i++;
    }


    you could probable loose the ios::binary.... just put that in when I was experimenting with >>

  3. #3
    Join Date
    Mar 2001
    Location
    Belgrade, Serbia
    Posts
    629
    yeah.... it makes sense....

    thx a lot!

  4. #4
    Join Date
    Jan 2001
    Posts
    588
    The reason that the extraction operator >> does not return the ' ' character is that it is a delimiter for tokenizing the contents of the stream. It does not return the delimiters, and I'm not sure whether you can make it include them or not. I know you can do something like a

    cin >> noskipws;

    I'm not sure what this does, it's been a while since I've used it, but you may want to look it up in your C++ reference of choice.

  5. #5
    Join Date
    Mar 2001
    Location
    Belgrade, Serbia
    Posts
    629
    thx for ********ing.....

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