Need Help... Writing on to, Scanning, and Returning Data from .txt File?

Question:
So I want to send and store: words, their definition, synonyms, and part of speech, in a .txt file and later be able to recall and use the words according to their part of speech(and posibly have a synonym picked at random to replace it) ...how might I do that? Examples would help me a ton!
Last edited on
Bump
Do you have a Dict file in mind that you plan to use or you want to make one ?

First you need to come up with a format for your text file.
The plan was to make one. It hadn't occured to me that I might be able to find one pre made, though now that it's been pointed out as an option, I'd rather find one if possible.

And as far as format, my thought was something along the lines of:
Word 'tab' Parts of speech 'tab' synonyms 'tab' definition

The idea is for part of a chatbot. I want to be able to scan the file for the word, find the part of speech it belongs to and respond accordingly, and to randomly switch out the word for one of its synonyms.
Last edited on
It's my assuption that I need to write something like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <fstream>
using namespace std;

int main () {
  char str[] =userResponse;
  char * pch;
  pch = strtok (userResponse," ");
  ofstream myfile ("dic.txt");
  if (myfile.is_open())
  {
    myfile << strtok


But I'm not sure how to keep it in the format that I want.

Sorry if I butchered the code, I'm still fairly new to C++ and coding as a whole.
Last edited on
Bump
> It's my assuption that I need to write something like:
1
2
3
4
5
6
> #include <stdio.h>
> // ...
>   char str[] =userResponse;
>   char * pch;
>   pch = strtok (userResponse," ");
> // ... 


You would discover that learning to use the standard C++ library makes programming a lot easier.
The task is certainly not trivial, but with standard C++, it becomes an order of magnitude simpler.

For instance:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <iterator>
#include <map>
#include <fstream>

// define a class to hold the information
struct word_info
{
    enum part_of_speech_t { NOUN, PRONOUN,  ADJECTIVE, DETERMINANT,
                             VERB, ADVERB, PREPOSITION, CONJUNCTION, UNKNOWN };
    part_of_speech_t part_of_speech ;
    std::vector<std::string> synonyms ; // www.mochima.com/tutorials/vectors.html
    std::string definition ;
};

constexpr char TAB = '\t' ; // http://www.stroustrup.com/C++11FAQ.html#constexpr
constexpr char SPACE = ' ' ;
constexpr char NEWLINE = '\n' ;

// provide a function to write the information into a stream
std::ostream& operator<< ( std::ostream& stm, const word_info& info )
{
    // wrie part_of_speech, TAB
    stm << info.part_of_speech << TAB ;

    // write synonyms, TAB
    // http://www.stroustrup.com/C++11FAQ.html#for
    for( const std::string& synonym : info.synonyms ) stm << synonym << SPACE ;
    stm << TAB ;

    // write definition, NEWLINE
    return stm << info.definition << NEWLINE ;
}

// provide a function to read the information from a stream
std::istream& operator>> ( std::istream& stm, word_info& info )
{
    // read part_of_speech
    int pos ;
    stm >> pos ;
    if( pos >= word_info::NOUN && pos < word_info::UNKNOWN )
        info.part_of_speech = word_info::part_of_speech_t(pos) ;
    else info.part_of_speech = word_info::UNKNOWN ;

    // http://www.cplusplus.com/reference/istream/istream/ignore/
    stm.ignore( 1000, TAB ) ; // throw away the TAB after part of speech

    // read synonyms, upto the next TAB
    std::string synonyms ;

    // http://www.cplusplus.com/reference/string/string/getline/
    std::getline( stm, synonyms, TAB ) ;

    // http://www.cplusplus.com/reference/sstream/istringstream/
    std::istringstream str_stream(synonyms) ;

    // http://www.cplusplus.com/reference/iterator/istream_iterator/
    std::istream_iterator<std::string> begin(str_stream), end ;

    // http://www.stroustrup.com/C++11FAQ.html#uniform-init
    info.synonyms = { begin, end } ;

    // read definition, everything left up to the NEWLINE
    return std::getline( stm, info.definition, NEWLINE ) ;
}

// defie a dictionary type to associate a word with its word_info
// http://www.cplusplus.com/reference/map/map/
// http://www.stroustrup.com/C++11FAQ.html#template-alias
using dictionary_type = std::map< std::string, word_info > ;

// provide a function to look up the dictionar for synonyms
std::vector<std::string> synonyms( const std::string& word, const dictionary_type& dict )
{
   auto iter = dict.find(word) ;
   return iter != dict.end() ? iter->second.synonyms : std::vector<std::string>{} ;
}

// TODO: provide a function to look up the dictionar for part_of_speech
word_info::part_of_speech_t pos( const std::string& word, const dictionary_type& dict ) ;

// TODO: provide a function to look up the dictionar for definition
std::string definition( const std::string& word, const dictionary_type& dict ) ;


// now, put it all together
int main()
{
    // create a dictionary mapping word toword_info
    dictionary_type dictionary // http://www.stroustrup.com/C++11FAQ.html#uniform-init
    {
        {
            "synonym",
            {
                word_info::NOUN,
                { "metonym", "equivalent", "analogue" },
                "word having or nearly the same meaning as another"
            }
        },
        {
            "colourful",
            {
                word_info::ADJECTIVE,
                { "hued", "vivid", "florid", "prismatic" },
                "richly or strikingly coloured"
            }
        },
        {
            "disembark",
            {
                word_info::VERB,
                { "alight", "dismount" },
                "get off a car, train, plane, ship etc."
            }
        }
        // etc.
    };

    // write the contents to a file
    const char* const file_name = "dict.txt" ;
    {
        std::ofstream file(file_name) ;
        for( const auto& pair : dictionary )
            file << pair.first << TAB << pair.second << NEWLINE ;
    }

    dictionary.clear() ; // empty the dictionary

    // read the contents back from the file
    {
        std::ifstream file(file_name) ;
        std::string word ;
        word_info info ;
        while( std::getline( file >> std::ws, word, TAB ) && file >> info )
            dictionary.emplace( word, info ) ;
    }

    // add two americanglish synonyms for disembark
    const std::string disembark = "disembark" ;
    dictionary[disembark].synonyms.push_back( "deplane" ) ;
    dictionary[disembark].synonyms.push_back( "detrain" ) ;

    // lookup and print synonyms for "disembark"
    std::cout << "synonyms for " << disembark << ": " ;
    for( const std::string& synonym : synonyms( disembark, dictionary ) )
        std::cout << synonym << SPACE ;
    std::cout << NEWLINE ;
}

http://coliru.stacked-crooked.com/a/ad47cf1034950585
Topic archived. No new replies allowed.