-1

I'm trying to read a bunch of words from a file and sort them into what kind of words they are (Nouns, Adjective, Verbs ..etc). For example :

-Nouns;

zyrian
zymurgy
zymosis
zymometer
zymolysis


-Verbs_participle;

zoom in
zoom along
zoom
zonk out
zone

I'm using getline to read until the delimiter ';' but how can I know when it read in a type and when it read in a word?

The function below stop right after "-Nouns;"

int main()
{
    map<string,string> data_base;
    ifstream source ;
    source.open("partitioned_data.txt");
    char type [MAX];
    char word [MAX];

    if(source) //check to make sure we have opened the file
    {
        source.getline(type,MAX,';');
        while( source && !source.eof())//make sure we're not at the end of file
        {
            source.getline(word,MAX);
            cout<<type<<endl;
            cout<<word<<endl;

            source.getline(type,MAX,';');//read the next line
        }
    }


    source.close();
    source.clear();
    return 0;

}
Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
  • 1
    It seems your categories have a `-` in front of them. Seems an easy enough way to parse them. On a side note you should check the return value of getline and not use eof. – Retired Ninja Jul 26 '19 at 01:22
  • 1
    I think it might be partially that you still have the `\n` to worry about after `source.getline(type,MAX,';');`. Try adding `source.ignore();` after this line, and you'll see what I mean. Not 100% sure that is the source of all of your problems, but it's a start. –  Jul 26 '19 at 03:42
  • See [this question](https://stackoverflow.com/questions/25475384/when-and-why-do-i-need-to-use-cin-ignore-in-c) for more information. (`cin.ignore()` is similar to `source.ignore()` in this case). –  Jul 26 '19 at 03:45

1 Answers1

0

I am not fully sure about the format of your input file. But you seem to have a file with lines, and in that, items separated by a semicolon.

Reading this should be done differently.

Please see the following example:

#include <iostream>
#include <string>
#include <sstream>
#include <fstream>

std::istringstream source{R"(noun;tree
noun;house
verb;build
verb;plant
)"};

int main()
{
    std::string type{};
    std::string word{};
    //ifstream source{"partitioned_data.txt"};

    if(source) //check to make sure we have opened the file
    {
        std::string line{};
        while(getline(source,line))//make sure we're not at the end of file
        {
            size_t pos = line.find(';');
            if (pos != std::string::npos) {
                type = line.substr(0,pos);
                word = line.substr(pos+1);
            }
            std::cout << type << " --> " << word << '\n';
        }
    }
    return 0;
}
  • There is no need for open and close statements. The constructor and destructor of the std::ifstream will do that for us.
  • Do not check eof in while statement
  • Do not, and never ever use C-Style arrays like char type [MAX];

Read a line in the while statement and check validity of operation in the while. Then work on the read line later.

Search the ';' in the string, and if found, take out the substrings.

If I would knwo the format of the input file, then I will write an even better example for you.

Since I do not have files on SO, I uses a std::istringstream instead. But there is NO difference compared to a file. Simply delete the std::istringstream and uncomment teh ifstream definition in the source code.

A M
  • 14,694
  • 5
  • 19
  • 44