Use fstream .get to get more then one char

This is a discussion on Use fstream .get to get more then one char within the C++ Programming forums, part of the General Programming Boards category; How can I use fstream .get to get more then one char?...

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    23

    Use fstream .get to get more then one char

    How can I use fstream .get to get more then one char?

  2. #2
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    Loop it.

    Or you could use the getline function.
    http://www.cplusplus.com/ref/iostrea...m/getline.html
    Or if you are using strings
    http://www.cppreference.com/cppstring/getline.html

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,817
    Take a look at get()
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    May 2005
    Posts
    23
    I am using get but it is only leting me get one char at a time
    Code:
    while (fin.get(ch))
       {
         
          //Image file name   
          if (ch == 'F' )
          {
                 fin >> imageName;
                 
                 }
    It only lets me compare one char. what I would like to do is compare a string but not sure how to do that.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,348
    Use getline like Enahs said.

    You can also use fin >> like you did with imageName, just read into a string variable. It will read up until the next whitespace, whereas getline will read until the next newline.

  6. #6
    Registered User
    Join Date
    May 2005
    Posts
    23
    I am using

    Code:
    string s;
       int flt;
         
       while (getline(fin , s))
       {
         
          //Image file name   
          if (s == "F" )
          {
                 fin >> imageName;
                 
                 }
           if (s == "I")
          {
    	 fin >> flt;
    	 ImRight = flt;
          }
    It wont get any data from the text file. Any one know why not?

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,348
    What does the file data look like (especially the first few lines)?

  8. #8
    CS Major Glorfindel-RW's Avatar
    Join Date
    Nov 2005
    Posts
    5
    fin.getline(fin , s);

    getline is a member, similarly to get.

  9. #9
    Registered User
    Join Date
    May 2005
    Posts
    23
    ok well the file looks like this

    F AnimBall.bmp
    L 1
    I 93
    T 41
    O 125
    R 0
    G 255
    B 0
    # 14
    D 150
    W 900
    M 87

    until I can get more then one char.

    second if I use

    Code:
     while (fin.getline(fin , s))
    I get error
    no matching function for call to `std::basic_ifstream<char, std::char_traits<char> >::getline(std::basic_ifstream<char, std::char_traits<char> >&, std::string&)'

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,348
    Don't use fin.getline(fin, s). That is wrong (getline is a member if you are using with C style strings, but you are using C++ strings, so you had it correct).

    The problem is that getline reads in the entire line. But since F and AnimBall.bmp are on the same line, one call to getline reads them both.

    Use fin >> s to read into the string instead of getline.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  3. Conversion Char To Char * Problem
    By ltanusaputra in forum Windows Programming
    Replies: 3
    Last Post: 03-01-2008, 01:06 PM
  4. How do i un-SHA1 hash something..
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 09-14-2005, 05:59 AM
  5. I'm having a problem with data files.
    By OmniMirror in forum C Programming
    Replies: 4
    Last Post: 05-14-2003, 09:40 PM

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