I have a file with record times, i need take each time and compare it whit the actual player time, so if the player time is < then the best time:input player time on top, if is >, then compare whit the next time and so...

the problem is until now i was using a switch to convert char to int, like:

case '1': myint =1; break;

but thats very boring, long and not efficient...(and if i use this method, i will have to create 60 cases for seconds, hours and min)

Now Im very confused..my recordes file is a text file:




So my first question is, theres a way where i can get a char and pass it to a int whithout getting the ascii value but the number itself?(and vice-versa)
If i want write or read int values to a file, this file can be a text file?
Is that the situation where i have to use binary i/o?

What i want to do is get the int hour,int min and int sec from the file, and
write a int hour, int min and int sec to the file.

Recommended Answers

All 8 Replies

Here is one of several ways to do it. Run this little program so you can see what it is doing.

#include <iostream>
#include <sstream>
using namespace std;

int main()
    size_t pos;
    string tm = "01:02:03";
    char c;
    int h = 0, m = 0, s = 0;
    stringstream str(tm);
    str >> h >> c >> m >> c >> s;
    cout << h << " " << m << " " << s << "\n";

>>Is that the situation where i have to use binary i/o?
No. The c++ fstream class will make that conversion for you, just use the << and >> operators to save/restore the integers

int main()
    ifstream in("myfile.txt");
    int n;
    in >> n; // get int from the file
commented: good clear response +7

Tanks a lot for the help, that works perfectly.

Theres something else bothering me now..
Theres a way to input things in a certain position in the file, whithout overwrite the content? Like put a new line, or a title at the top without erasing the next characteres .Or i will have to rewrite everything?

To add a new line, you have to rewrite the whole file. There is no way to insert data into the middle of a file. Ancient Dragon made a snippet which does something similar, but instead of adding a line, it deletes one. If you study the code, i'm sure you could figure out how to make it add a line, instead of deleting one. http://www.daniweb.com/code/snippet777.html

You'll have to re-write everything. Just make a new file, read the file you want, and write the lines to the new file, with the changes you want. Afterwards, erase the old file and rename the new file to the old file's name.
There's something similar (erasing a line from a file) in the code snippets. Feel free to dig there ;)

Whoops, I guess I was a bit slow since I looked for the link to that snippet in the snippets section :)


now im having problem with this:

fstream arq_time;

arq_time<< "\t\t\t\t  RECORDS\n\n\n";

arq_time<<"A";//doesnt work (line_start =20)

Whats happening on this code? Why it writes just at first?

I realize that it do write, but not in 'real_time', it waits to finish the loop to update the file...weird

what's the value of line_start that's on line 7 of the code you posted?

>what's the value of line_start that's on line 7 of the code you posted?
Uhm... look at the comment on line 8 ;)

I don't understand your problem. The code you posted works perfectly ok for me. Is there other code you didn't post? If yes, you will probably have to post it all so we can see what is going on.

Be a part of the DaniWeb community

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