0

I have problem while getting first word of each line in my file. My txt file is:

GB LONDON 1 9 3 0 4 5
D BERLIN 2 5 6 1 4 
E MADRYT 1 2 3 4 5 

And I want only to put first word each line of file to array like [GB,D,E]; I tried this:

ifstream plik("galerie.txt");
    
    if(!plik){
        
        
        cout<<"not working";
    }

    
    string city[50];
    int n=0;
    while(plik.eof()){
        plik>>city[n];
    
        n++;
    
    }
    plik.close();

But it gets me every string in line like: GB,LONDON,1,9...

PolakRobak
  • 17
  • 3
  • 1
    try [`std::getline`](https://en.cppreference.com/w/cpp/string/basic_string/getline) – Alan Birtles Mar 12 '21 at 15:46
  • Yes, I tried, but how can I use getline till whitespace because I tried this: getline(plik,city[n],' '); and stillnot working – PolakRobak Mar 12 '21 at 15:51
  • `while(plik.eof())` is certainly a typo, but see why the intended `while(!plik.eof())` [is also wrong](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons). – churill Mar 12 '21 at 15:55
  • Even if I changed it, still not working – PolakRobak Mar 12 '21 at 16:03
  • 1
    BTW, your program is ignoring the `if` statement. The file will be read if the input open fails. Maybe you want to `return` an error code instead of continuing. – Thomas Matthews Mar 12 '21 at 16:53

4 Answers4

1
#include <fstream>
#include <iostream>
#include <string>
#include <vector>

int main() {
  std::ifstream plik("galerie.txt");

  if (!plik) {
    std::cerr << "not working";
    return 1;
  }

  std::vector<std::string> city;
  std::string tmp;
  while (std::getline(plik, tmp)) {
    city.push_back(tmp.substr(0, tmp.find_first_of(" ")));
  }
  plik.close();

  for (auto i : city) {
    std::cout << i << '\n';
  }
}

Output:

GB
D
E

Optionally, if you don't want tmp staying in scope longer than it's needed:

  for (std::string tmp; std::getline(plik, tmp);) {
    city.push_back(tmp.substr(0, tmp.find_first_of(" ")));
  }
sweenish
  • 3,215
  • 2
  • 10
  • 19
1

If you only want to keep the first word you have read a single word and ignore the rest of the line. You can use std::istream::ignore to do so.

// read first word of a line
while(plik >> city[n]) 
{
    // skip rest of the line, including the line break
    plik.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

    n++;    
}
churill
  • 9,299
  • 3
  • 13
  • 23
0

! After reading again the question, code is changed and explanation is changed twice!

You are reading word by word without throwing away the words after the first one on each line, and appending it to the string. I guess the best is to read a line and evaluate that, smiliar to Syed has proposed.

ifstream plik("galerie.txt");

if(!plik){
    
    
    cout<<"not working";
}


std::string city;
std:: istringstream streamCity;
std::string line;

streamCity << '[';

while (std::getline(plik, line)) {
    std::string cCode;
    std::istringstream iss(line);        
    iss>>cCode;
    streamCity << cCode << ',';

     n++;

}

streamCity << ']';
plik.close();
Johannes
  • 1
  • 3
-2

You can try this code works fine for me can check on your side. You just need to push in vector instead of output in another file.

#include<iostream>
#include<fstream>
#include <sstream>
#include<string>
using namespace std;

int main()
{
    ifstream fin("input.txt");
    vector<std::string> v;
    string line;
    string word;

    int i;
    int count=0;

    while(!fin.eof())
    {
        getline(fin,line);
        for(i=0;i<line.length();i++)
        {
            if(line[i]==' ')
            {
                break;
            }
            word = word + line[i]
        }
        v.push_back(word);
        word = "";
    }
    for(std::size_t i = 0; i < v.size(); ++i) {
        std::cout << v[i] << "\n";
    }
    return 0;
}
Syed Mohib Uddin
  • 551
  • 3
  • 14