Thread: why can't ifstream read spaces or enters

  1. #1
    bobish
    Guest

    why can't ifstream read spaces or enters

    see here http://www.cprogramming.com/tutorial/lesson10.html

    it stops reading after it reaches a space. Why is this and is there any easy way around it.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Input from istreams is delimited by a space, this is why you can say something like:

    cin>> i >> j >> k;

    And use input with spaces:

    10 20 30

    Of course this becomes a problem when you want to read a string of data that includes spaces. In this case you want to have input delimited by a newline character, which is what the getline method does:

    cin.getline ( array, SIZE );

    or with C++ strings:

    getline ( cin, someString );

    Note that wherever I used cin, you could replace it with a user defined istream:

    ifstream ifs ( "someFile.txt" );
    getline ( ifs, someString );

    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Use std::getline(cin, string).

    Kuphryn

  4. #4
    bobish
    Guest
    okay

    If I include the line

    Using std::getline(cin, string);

    i get these error messages

    error C2653: 'std' : is not a class or namespace name
    error C2146: syntax error : missing ';' before identifier 'getline'
    : error C2501: 'Using' : missing storage-class or type specifiers
    : fatal error C1004: unexpected end of file found

    if i don't i get this

    error C2065: 'getline' : undeclared identifier

    here is the line getting the error message:
    getline ( Levels, String );

  5. #5
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    Put this:

    using namespace std;

    at the very top of your code, just below your includes, if it is not already there (which it ought to be anyway)

    then, when you want to fill a string called String from a istream called Stream just use this:

    getline(Stream, String);


    If this doesn't work, post the smallest complete segment of code you can isolate that produces an error. Be sure to use code tags.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Using std::getline(cin, string);
    C++ is case sensitive, it should be

    using std::getline;

    Also note that getline is defined in <string>, not <iostream> and the previous statement would cause an error if you don't include string. The getline from iostream is bound to cin and is used differently, so the using statement would be

    using std::cin;

    And the call to getline would be

    cin.getline ( cstring, size, delimiter );

    >at the very top of your code, just below your includes, if it is not
    >already there (which it ought to be anyway)
    Actually, according to good style it shouldn't be there. using namespace std; opens the entire std namespace to the program, thus making everything in it global, which is a bad thing. The preferred method is to either use the std:: prefix for anything in std or open only the parts of std that you will be using in the function that you will use them. If something is to be used in every area of the program then you can make it open globally, such as cout.

    #include <iostream>
    using std::cout;

    This is a valid use of the using statement in the global scope. Anything more should require a great deal of thought and understanding of the consequences.

    -Prelude
    My best code is written with the delete key.

  7. #7
    bobish
    Guest
    okay now I'm getting these errors

    (6) : error C2653: 'std' : is not a class or namespace name

    (6) : error C2873: 'getline' : symbol cannot be used in a using-declaration

    (73) : error C2065: 'getline' : undeclared identifier

    here is the first six lines of my code perhaps allegro is interfering
    [code]
    #include <allegro.h>
    #include <stdio.h>
    #include <fstream.h>
    #include <string.h>

    using std::getline;
    [code]

    and here is the only real relevent part

    ifstream Levels("Levels.txt");
    getline ( Levels, String ); //line 73

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >#include <string.h>
    string.h doesn't contain the C++ string class, use:

    #include <string>
    using std::getline;
    .
    .
    .
    getline ( Levels, String );

    -Prelude
    My best code is written with the delete key.

  9. #9
    bobish
    Guest
    okay thanks here the error message I'm getting now

    : error C2780: 'class std::basic_istream<_E,_Tr> &__cdecl std::getline(class std::basic_istream<_E,_Tr> &,class std::basic_string<_E,_Tr,_A> &,const _E)' : expects 3 arguments - 2 provided

    and

    : error C2784: 'class std::basic_istream<_E,_Tr> &__cdecl std::getline(class std::basic_istream<_E,_Tr> &,class std::basic_string<_E,_Tr,_A> &)' : could not deduce template argument for 'clas

    Aren't are also suposed to input in a third argument which will stop getline

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Show your code please, it's easier to debug with an error message AND code.

    -Prelude
    My best code is written with the delete key.

  11. #11
    bobish
    Guest
    I didn't really change anything other then String.h to string and

  12. #12
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Here's the corrected tutorial with char arrays holding the strings:
    Code:
    #include <fstream>
    #include <iostream>
    /* Edits: The .h extension is not the current standard */
    
    int main()
    {
      char astr[10]; /* Edits: Identifiers starting with str are reserved */
        //Used later
      std::ofstream a_file("example.txt");
        //Creates an instance of ofstream, and opens example.txt
      a_file<<"This text will now be inside of example.txt";
        //Outputs to example.txt through a_file
      a_file.close();
        //Closes up the file
      std::ifstream b_file("example.txt");
        //Opens for reading the file
      b_file.getline ( astr, 10 );
        //Reads one string from the file
      std::cout<<astr;
        //Should output 'this'
      b_file.close();
        //Do not forget this!
    }
    /* Big Edits: Namespaces are used to conform with the current standard,
       no changes were made to handle errors, one should be wary of that
       when working with files
    */
    And here is the corrected tutorial with C++ strings:
    Code:
    #include <fstream>
    #include <iostream>
    #include <string>
    /* Edits: The .h extension is not the current standard */
    
    int main()
    {
      std::string astr; /* Edits: Identifiers starting with str are reserved */
        //Used later
      std::ofstream a_file("example.txt");
        //Creates an instance of ofstream, and opens example.txt
      a_file<<"This text will now be inside of example.txt";
        //Outputs to example.txt through a_file
      a_file.close();
        //Closes up the file
      std::ifstream b_file("example.txt");
        //Opens for reading the file
      std::getline ( b_file, astr );
        //Reads one string from the file
      std::cout<<astr;
        //Should output 'this'
      b_file.close();
        //Do not forget this!
    }
    /* Big Edits: Namespaces are used to conform with the current standard,
       no changes were made to handle errors, one should be wary of that
       when working with files
    */
    I hope that clears everything up for you.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [question] ifstream read file, thanks!
    By userpingz in forum C++ Programming
    Replies: 6
    Last Post: 05-19-2009, 06:38 PM
  2. How can I know the actual bytes read in a file read
    By pliang in forum C++ Programming
    Replies: 1
    Last Post: 06-08-2005, 04:23 PM
  3. What Would You Use To Read User Input?
    By djwicks in forum C Programming
    Replies: 11
    Last Post: 04-05-2005, 03:32 PM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. I am lost on how to read from file and output to file?
    By vicvic2477 in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2005, 11:52 AM