Take a look at the std::(o/i)fstream classes. They have everything you need. Once you have some specific code, we can help determine where you went wrong. Basically you will want to pull the numbers out of the time/dates (basically by ignoring the ./: characters) then translate them into the ints you want. there are many ways to convert strings to integers if you merely search for them (atoi and stringstream come to mind).
one way to do it is to use strtok() to separate the original string into its individual parts (tokens). Call atoi() or atol() to convert each part to integer. Next, multiply the year and month by some factor of 10 so as to shift them left and make room for the next integer. For example, if the year is 2003 then multiply it by 10000 making it 20030000 which leaves room for month and day.
I was actually thinking of a more straightforward approach. Read the whole block into a string (so the string contains, for example, "12:30"). Then remove the punctuation (so "12:30" becomes "1230") then use atoi() or atol() or stringstream() to convert it to your integers. The benefit of this method is that you will not have to shift the numbers at all, and all of your dates/times should be whitespace seperated, which lends itself nicely to fstream or stringstream >> operators. Removing characters from a string is fairly simple:
//don't quote me on this, it is from memory
void removeFromString(std::string &str, char ch)//removes all occurances of ch in str
Ok guys ,thanks for responses.
I've found a way to convert a char into an integer: let's say char x=5 then int i = x-'0'
In other words we have to compute the difference between the char we want to convert into a number and 0 (zero). (Even though I might follow Labdabeta's advice, I'll need to convert chars into numbers anyway because columns №3,4,5,6 of the text file contain numbers.)
Is this a correct way?
That is the correct MANUAL way. However standard headers provide simple easy ways that are likely faster. For example atoi() converts a string like "1234" into an int like 1234 for you. Yes, you could write it yourself like so:
int atoi(char *s)
for (int i=0; s[i]; ++i)
if (s>='0'&&s<='9')//if s is a digit
The benefit of using the standard libraries is that they are fast and guaranteed to work. Both methods work fine, but as far as I know, it is generally considered best practice to use whatever standard libraries make your task easier. (although personally I prefer to rewrite the functions myself just so that I am sure that I fully understand exactly what they do)
In summary: Yes, that is the correct way (although if char x=5 then int i=(int)x; works better, but if char x='5' then int i=(int)(x-'0'); is correct).