0

I am using ifstream to get lines from a file and store them to a string. Each line contains a single word with no spaces.

    virtual void readFromFile(char* input){
        ifstream inf(input);

        if(!inf){
            cerr << "The specified file could not be found." << endl;
        }

        while(inf){
            string str;
            getline(inf, str);
            pushFront(str); // store it in my data structure

        }

        inf.close();
    }

file.txt

a <= returns length 1 (correct)
at <= returns length 3
ate <= returns length 4
rate <= returns length 5
irate <= returns length 6

When I call length() on the string corresponding to the very first of the file, it returns the correct value. However, calling length on strings corresponding all other lines results in a offset of +1. For example, if the length of the string is actually 5, it returns 6. Does this have something to do with new lines? If so, how can I properly extract these words from the file?

rmi
  • 532
  • 4
  • 15
Bob Shannon
  • 638
  • 2
  • 10
  • 19
  • Are you sure that no intervals are written on the lines? Potentially this can be related to the two symbols that Linux systems use for new lines '\r' and '\n', but I think getline will trim them both. Even so, please try to see if the incorrect words end with '\r' or '\n' and we would know for sure. – Boris Strandjev Feb 27 '14 at 07:11
  • Let me check that out. – Bob Shannon Feb 27 '14 at 07:13
  • Looks fine to me? http://oi61.tinypic.com/99qqsz.jpg – Bob Shannon Feb 27 '14 at 07:15
  • 1
    This is linux. It might be assumed that some lines end with the linux line ending "\r\n". They need to be like so otherwise you will not get to the 55 characters with just 36 visible. Apparently, however, the first line end in just '\n', that's why there is no mismatch there. Try setting the last character of input to '\0' in case it matches '\r' I thik this might fix it. – Boris Strandjev Feb 27 '14 at 07:25
  • 2
    In order to see the invisible characters in vim do `:set list`. this will help you figure out what happens – Boris Strandjev Feb 27 '14 at 07:28
  • Thank you, I've gotten it working by following your advice :) – Bob Shannon Feb 27 '14 at 07:31
  • I will turn my help into an answer so we cna process the question as it should be in SO – Boris Strandjev Feb 27 '14 at 07:32

2 Answers2

1

You are using vi as text editor so you can show the invisible characters by doing :set list. This will help you figure out what might be these additional characters you see on most of the lines.

In linux the usual line ending is "\r\n", which are in fact two characters. I am not exactly sure whether the getline will omit them both or not. However, just as a precaution you can add the following logic:

getline(inf, str);
int len = str.size();
if (str[len - 1] == '\r') {
   str.pop_back(); // not C++11 you do it str = str.erase(str.end() - 1);
}
pushFront(str); // store it in my data structure
Boris Strandjev
  • 46,145
  • 15
  • 108
  • 135
  • 1
    I think it would be good to link to some [reference on the getline function](http://en.cppreference.com/w/cpp/string/basic_string/getline) or [similar discussions](http://stackoverflow.com/questions/6089231/getting-std-ifstream-to-handle-lf-cr-and-crlf/6089413#6089413). – mbschenkel Feb 27 '14 at 07:46
0

If the format in Your text file is defined, that every line contains exactly one word, so it is more easy and more sure to read this words.

void readFromFile(char* input){
    ifstream inf(input);
    if(!inf){
        cerr << "The specified file could not be found." << endl;
    }
    for( string word; inf >> word; )
        pushFront( word ); // store it in my data structure
}   // inf.close() is done automaticly leaving the scope
cpp-progger
  • 406
  • 3
  • 6