Hi! I'm a newbie in C++ and encountered myself with a problem:
how to transfer data from a text file (containing data in "yyyy.mm.dd" and "09:00" format) into a two dimensional array ?

What I want:to convert
2003.12.20 into 20031220 and 10:15 or 09:00 or 00:00 convert into 1015 900 and 0 respectively.
I almost forgot to mention: the output should be in "int" format, not "char".

I hope it won't be hard for you, thanks beforehand.

sbr.shahoriar.1 commented: Good +0

Sorry, but you have to try first, and then we'll help. How would you approach the problem? Show some work...

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
{
    int index;
    while ((index=str.find_first_of(ch))!=std::string::npos)
        str.erase(index,index+1);
}

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)
{
    int ret=0;
    for (int i=0; s[i]; ++i)
    {
        if (s>='0'&&s<='9')//if s is a digit
        {
            ret*=10;//shift
            ret+=s[i]-'0';//and add
        }
    }
    return ret;
}

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).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.