I have a problem with file reading. In particular, I have to find some keyword in text file and then I have to do different operations.
I make an example, consider the file:
keyword_1 word {
keyword_2 name_1,name_2
keyword_3 name_3
}
keyword_4 : name_3 keyword_5 name_2
I don't know how to read this file, if I have to read line by line or character by character, or if it's possible an hybrid approach.
If i read line by line, how can I separate different words of a line ? How can I ignore ' ' (blank space)?
I just reported you for being "lazy".
You're in trouble now!
Seriously, though, if you read through carefully and OF COURSE look up any functions that you don't understand, you should be able to figure out how it works. Why should I exert myself if you won't?
H00G0 : Sorry, but I did not report dutch.. he helped me, why did I report him?
Moreover, I saw this post 5 minutes ago after I asked to explain the code.. When did I report?
dutch : I'm not lazy, i just asked for an help because i tried to understand by myself but i didn't understand it at all, for example the function myispunct..
But it's ok, sorry if I hurt you.
ispunct is a (dreaded C) function that checks if a character is a punctuation character
Another function that's useful is strtok() - I suggest you look it up - there is a sample program and you can modify the separators (delimiters) as required.
Reading line by line is a good move. But there again you can read the whole file in as a string and analyse that.
If you stay with C functions then one you could easily need is strcmp() - look that up too because it enables you to check whether a token (read word) is equal to the one you are looking for. (C++ <strings> just use a == b)
Here is another possibility, to especially demonstrate the use of the reference material and tutorials which form part of this site.
strtok() and a few other functions are easily found there.
Most of the code below is a direct liftout/copy of the examples.
The only job still to be done is filter out which words are keywords and which aren't. As a starting point strcmp() might be useful.
BTW The purists will say this uses C-style strings - strings are read in and converted to char arrays via the c_str() function - and is therefore treasonous. But ignore that because strtok() has a convenient ability to have multiple delimiters which save a lot of stuffing around with streams and strings, but fill your boots with that if you want to.