Thread: extra line while reading from file

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    87

    extra line while reading from file

    My file has these lines.
    I try to remove extra line in the end, in notepad/gedit, however, after saving it automatically adds up there.
    one
    two
    three
    Code:
    void help::read_ref(const char* file_name){
         std::ifstream file_handle;
         file_handle.open(file_name);
    
         std::string line,name;
         getline(file_handle,line);
    
         while(!file_handle.eof()){
    
             if(line[0]=='>'){
                 name=line.substr(1,line.length()-1);//taking selected chars
                 reference[name]="";
             }
    
            
           getline(file_handle,line);     
     }
         file_handle.close();
    
         std::cerr<<"file reading done"<<"\n";
    
    }
    //when I print size of map, it comes out to be an extra then what is should be.
    std::map<std::String,std::string> ->is my map

    on the contrary if I use
    Code:
     file_handle >> line;
    I get correct number of entries in map.

    My doubt: getline, automatically finds \n and in last, it finds a \n, why it doesn't end the iteration?
    I have faced this issue many number of times.
    Please guide.

    Code:
    line[0]=='>'
    I am making this check, then also, I get a null string as key in my map.

    I have printed key with their index, using
    Code:
    map iterator
    .

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,309
    You should not use eof() to control the loop in this way. Rather, use the return value of getline, e.g.,
    Code:
    std::string line, name;
    while (getline(file_handle, line)) {
        if (line[0] == '>') {
            name = line.substr(1, line.length() - 1); //taking selected chars
            reference[name] = "";
        }
    }
    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

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    >> std::ifstream file_handle;
    >> file_handle.open(file_name);
    You should learn to use constructors:
    std::ifstream file_handle(file_name);

    >> file_handle.close();
    There is no need to close files manually. They close automatically when they go out of scope. C++ isn't Java.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Oct 2013
    Posts
    87
    Quote Originally Posted by deathmetal View Post
    My file has these lines.
    I try to remove extra line in the end, in notepad/gedit, however, after saving it automatically adds up there.
    laserlight, thank you for your reply.
    Can you please help me understand this?
    eof looks for last empty line, if I am not wrong?

  5. #5
    Registered User
    Join Date
    Oct 2013
    Posts
    87
    Elysia,
    Thank you very much on these points.
    Quote Originally Posted by Elysia View Post
    >> std::ifstream file_handle;
    >> file_handle.open(file_name);
    You should learn to use constructors:
    std::ifstream file_handle(file_name);

    >> file_handle.close();
    There is no need to close files manually. They close automatically when they go out of scope. C++ isn't Java.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,309
    Quote Originally Posted by deathmetal
    Can you please help me understand this?
    eof looks for last empty line, if I am not wrong?
    No, eof() returns true if the end of file condition has been detected (and thus the appropriate flag has been set). This condition is not yet detected when you have read the last line; it is detected on the next attempt to read a line.
    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

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by deathmetal View Post
    laserlight, thank you for your reply.
    Can you please help me understand this?
    eof looks for last empty line, if I am not wrong?
    eof() checks if the eof flag has been set. That flag is set only after the first read beyond the end of the file.
    So your loop, on its last iteration, succeeds (ie eof() return false). Next iteration, you read beyond the file (hence you will get an empty line), and the eof flag gets set.
    Next iteration, the loop terminates.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Oct 2013
    Posts
    87
    @Elysia, laserlight:
    Thanks much. I got it
    Quote Originally Posted by Elysia View Post
    eof() checks if the eof flag has been set. That flag is set only after the first read beyond the end of the file.
    So your loop, on its last iteration, succeeds (ie eof() return false). Next iteration, you read beyond the file (hence you will get an empty line), and the eof flag gets set.
    Next iteration, the loop terminates.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 06-07-2012, 02:50 AM
  2. getting two extra ?? while reading my input file
    By monsoonwinds7 in forum C++ Programming
    Replies: 3
    Last Post: 09-23-2011, 03:58 PM
  3. Replies: 7
    Last Post: 12-13-2010, 02:13 PM
  4. Reading Input from a file, line-by-line
    By Acolyte in forum C Programming
    Replies: 8
    Last Post: 09-30-2007, 01:03 PM
  5. reading a file line by line and char *
    By odysseus.lost in forum C Programming
    Replies: 8
    Last Post: 05-31-2005, 09:47 AM

Tags for this Thread