0

I hope anyone will be able to help me out, or at least understand what i am trying to achieve. i tried searching for this all over the web and i did not find the answer, or at least did not understand what others did.

So, im trying to read each line from file.txt into a array. As an example, my file.txt contains:

3
labas 
ka tu
nu tai davai

(Just an example)

I want to get each line into my

string sentenses[CMax]; 

(CMax is set to 10000)

After hours of searching the web and tinkering myself i ended up with:

    ifstream KC;
    KC.open(langPicked.c_str());
      KC >> m;
      for (int o = 0; o < m; o++) {
        KC.getline(sentenses[o], 255); 
        if(KC) cout << sentenses[o] << endl;
      }
    KC.close();

But, surprise surprise, it aint working.

Basickly what i am trying to achieve is that each line, from KC, would be added to >> sentenses[o] array so i could cout what ever line i wish so, as an example, if i typed:

  cout << sentenses[2] << endl;

it would output the 2 line:

ka tu

error that pops up:

D:\Lith\Codeblocks\uzd\Skaiciuokle 2.0\main.cpp|88|error: no matching function for call to 'std::basic_ifstream::getline(std::__cxx11::string&, int)'|

NOTE: The code that i posted might, and propobly is, in some way, shape or form wrong. I am just a started trying to learn c++ on my own. If you understood what i was trying to do, feel free to answer with your own written code that would fit in my situation. Thank you in advance

genpfault
  • 47,669
  • 9
  • 68
  • 119
Lith
  • 398
  • 3
  • 13
  • 1
    What is a "massive"? –  May 10 '18 at 18:49
  • "Massive" might be a mistranslation...were there any similar suggested words? – Drew Dormann May 10 '18 at 18:52
  • I am sorry, what i meant by massive is (used google translate) : array – Lith May 10 '18 at 18:55
  • what is not working with your code? – tima May 10 '18 at 19:03
  • 1
    @tima I am getting an error: D:\Lith\Codeblocks\uzd\Skaiciuokle 2.0\main.cpp|88|error: no matching function for call to 'std::basic_ifstream::getline(std::__cxx11::string&, int)'| – Lith May 10 '18 at 19:08
  • 1
    Rustyx's suggestion requires you to use [a slightly different `getline`](http://en.cppreference.com/w/cpp/string/basic_string/getline): `getline (KC, sentences[o]);` – user4581301 May 10 '18 at 19:14
  • Likely missing `#include ` – CoolBots May 10 '18 at 19:15
  • I suggest reading [mcve] and altering your question accordingly. If you need the question at all after creating an MCVE. The true beauty of the MCVE is making one almost always enables you to see the error for yourself and how to fix it. – user4581301 May 10 '18 at 19:18
  • Possible duplicate of [Read file line by line](https://stackoverflow.com/questions/7868936/read-file-line-by-line) – KPCT May 10 '18 at 19:50

2 Answers2

0

Thank you Rustyx

the solution was to change getline.

For any1 interested. This code is working:

ifstream KC;
KC.open(langPicked.c_str());
  KC >> m;
  for (int o = 0; o < m; o++) {
    getline(KC, sentenses[o]);
  }
KC.close();

P.s. idk how to attach rustyx's answer to the "solved' segment so i posted my own comment. sorry

Lith
  • 398
  • 3
  • 13
0

Take a look at this pseudocode:

string filename; //load the filename
cout << "Give the name of the file:\n";
cin >> filename;
cout << endl; 

ifstream file;

string txt = ".txt";

filename.append(txt);  //append .txt to the filename

file.open(filename);   //open this file

if(file.is_open()) {
    const int CMax = 10000;
    string senteces[CMax];
    int o = 0;

    while (getline(file, senteces[o])) 
        ++o;

    cout << senteces[2] << endl; //it will print "ka tu"
    cout << senteces[0] << endl; //it will print "3"
}
patryk-szwed
  • 137
  • 2
  • 11