-4

I am new to C++ language and I was looking for clear answer to my problem but with no result. What I am trying to do is get substr value of the string which is single line in text file. When I run compilation like this I receive first twelve letters of the sentence and I don't have problem with that:

a[count] = sentence.substr(0,12);

but when I am trying to change pos from 0 to any other value for example:

a[count] = sentence.substr(1,12);

I receive error:

terminate called after throwing an instance of std::out_of_range what(): basic_string::substr: _pos (which is 1) this -> size() (which is 0)

I checked it with YT and online guides and no one had problem with substr. Any ideas?

EDIT: Sorry for confusion cause. This is a part of the code:

string sentence;
string a[10000];
string next_line[10000];

main()
{

int count = 1;    


fstream file;
file.open("converted.txt",ios::in);

while(!file.eof())
{
   getline(file, line);
   next_line[count] = line;
   sentence = next_line[count];

   a[count] = sentence.substr(1,12);

   count++;
}

}
Rabbid76
  • 142,694
  • 23
  • 71
  • 112
  • 2
    Does `sentence` has more than 12 characters? – kocica Aug 22 '17 at 08:37
  • 2
    This is not a [mcve] ... What is sentence? What does it contain? – muXXmit2X Aug 22 '17 at 08:40
  • `sentence` has 24 characters –  Aug 22 '17 at 08:40
  • 4
    Did you print out how long `sentence` is? The error message suggests it is empty. – Galik Aug 22 '17 at 08:41
  • 3
    Please post a [mcve] – n. 'pronouns' m. Aug 22 '17 at 08:41
  • @FilipKočica How would it matter?If the string has less than `12` characters, then all of them would be returned.Right? – Gaurav Sehgal Aug 22 '17 at 08:45
  • The error message tells you the problem. The first parameter `_pos` must not be larger than the `size()` of the string. – Bo Persson Aug 22 '17 at 08:46
  • @FilipKočica I read it [here](http://www.cplusplus.com/reference/string/string/substr/) and tried it [here](https://ideone.com/Wm72ER) – Gaurav Sehgal Aug 22 '17 at 08:50
  • Oke you are right – kocica Aug 22 '17 at 08:52
  • Can you read guys? I wrote it that sentence has 24 characters and there is no error when I type `a[count] = sentence.substr(0,12);` only when I change `_pos` value. –  Aug 22 '17 at 08:54
  • Still, not a [mcve]. What is the content of the file you are reading? I assume one line (or multiple lines) is empty? Check the size of `sentence` before calling `substr`. – muXXmit2X Aug 22 '17 at 08:55
  • 2
    @MrDominikku Yes we all can read.Otherwise we would not be responding to your question(without [mcve](https://stackoverflow.com/help/mcve)) and comments.`sentence.substr(0,12);` would work on empty `sentence` but `sentence.substr(1,12);` would fail.So you have to provide more information like what's in the file.Check what you actually get in `a[count]` after `sentence.substr(0,12)`.That will help you. – Gaurav Sehgal Aug 22 '17 at 09:00
  • File has 6300 lines. Mostly dates and numbers which I don't want to share with anyone. I still don't get it why you can't understand that each line has 24 characters and when I use `a[count] = sentence.substr(0,12);` I get first 12 numbers of the string (that's works) but when I change `_pos` and I want get numbers from 1 to 13 I get this error. I have no idea why, that's why asking you guys for help. –  Aug 22 '17 at 09:00
  • 1
    Read [Why is `iostream::eof` inside a loop condition considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – molbdnilo Aug 22 '17 at 09:02
  • `cout< –  Aug 22 '17 at 09:03
  • The condition `while(!file.eof())` will give you an extra **empty** line at the end of the file. The condition will not be set until *after* `getline` has failed one read. – Bo Persson Aug 22 '17 at 09:03

1 Answers1

1

From feof

This indicator is generally set by a previous operation on the stream that attempted to read at or past the end-of-file.

It means you have read one more row, before condition was false. This row was empty.

From substr

The substring is the portion of the object that starts at character position pos and spans len characters (or until the end of the string, whichever comes first).

So if you used substr with first parameter 0, it was ok, it was skipped. But if you passed 1 as first parameter, which was more that the string had characters, an exception was thrown.


Here is the correct reading from file

#include <iostream>
#include <fstream>

using namespace std;

string sentence;
string a[10000];
string next_line[10000];

int main()
{

    int count = 1;
    std::string line;

    fstream file;
    file.open("test.txt",ios::in);

    while(getline(file, line))
    {
       next_line[count] = line;
       sentence = next_line[count];

       a[count] = sentence.substr(1,12);

       count++;
    }
}
kocica
  • 6,172
  • 2
  • 11
  • 34
  • Worked perfectly! That the answer I was looking for. Thanks! –  Aug 22 '17 at 09:21
  • 3
    You are welcome, next time try to debug your code and you would see there is an empty row read from the file :-) Have a nice day. – kocica Aug 22 '17 at 09:22