Problem with text input

This is a discussion on Problem with text input within the C++ Programming forums, part of the General Programming Boards category; I am trying to input a group of words from a text file. I can only get the first word ...

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    2

    Problem with text input

    I am trying to input a group of words from a text file. I can only get the first word and it ignores the rest...what is the problem?

    My input file is called "patterns.txt". As an example, lets say it contains the words (Big House). When I run the code below, it returns Big only...why? and how can I fix this?

    int main()
    {
    char pattern[50];

    //open the file for reading
    ifstream pattern_file("patterns.txt");
    pattern_file>>pattern;
    //for debugging purposes only
    cout<<"the file patterns.txt contained the pattern: "<<pattern<<"\n";
    //close the file after reading in the pattern
    pattern_file.close();
    return 0;
    }

    pls help...

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >pattern_file>>pattern;
    The >> operator of input streams are delimited by whitespace when reading strings. The fix for this is to use a function called getline which is delimited by a newline and/or the size of the array you are reading to.
    pattern_file.getline ( pattern, 50 );

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

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    2
    I am using a redhat linux machine...I tried the

    pattern_file.getline ( pattern, 50 );

    statement, but it gave a compile error:
    invalid operands of types '{unknown type}' and 'int' to binary 'operator >>'
    am I doing something wrong?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. geting input in C, problem in getting input
    By BujarM in forum C Programming
    Replies: 3
    Last Post: 04-17-2009, 10:38 PM
  2. problem with keyboard input
    By fighter92 in forum Game Programming
    Replies: 6
    Last Post: 03-20-2009, 10:41 AM
  3. problem collecting data from input file
    By gkoenig in forum C Programming
    Replies: 12
    Last Post: 03-30-2008, 08:40 AM
  4. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 04:42 PM
  5. problem : input and calculation working together
    By itay390 in forum C Programming
    Replies: 13
    Last Post: 07-30-2005, 01:32 PM

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