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:

name-----------------------------------00:00:00

 name-----------------------------------00:00:00

 name-----------------------------------00:00:00

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.

Dani AI

Generated

is spot on: you do not need binary I/O here. For single digits already read as chars, convert like this (and validate first):

char c = ...;                 // e.g. '7'
if (std::isdigit(static_cast<unsigned char>(c))) {
    int d = c - '0';          // 7
}
int d = 5;
char c2 = static_cast<char>('0' + d); // '5'

For your lines, read text and parse the clock field. An easy alternative to stringstreams is std::sscanf:

std::string line = "...00:00:00";
int h=0, m=0, s=0;
if (std::sscanf(line.c_str(), "%d:%d:%d", &h, &m, &s) == 3) {
    int total = h*3600 + m*60 + s;  // compare totals, then rewrite formatted
}

On inserting into the middle of a text file: and are right — there is no in-place insert. You either rewrite to a new file, or design fixed-width records so you can overwrite bytes without shifting the rest. For example, keep each line a constant width (padded name + exactly 8 chars for hh:mm:ss + newline). Then the offset of line i is i * record_size, and the time field sits at a known byte offset you can overwrite safely.

About your seekp and "not in real time": streams buffer output. If you need to see the data immediately (e.g., while looping), flush or switch the stream correctly:

arq_time.seekp(0);
arq_time << "RECORDS\n\n" << std::flush;   // force write now

// when mixing reads and writes on one fstream:
arq_time.flush();                           // after writing, or
arq_time.clear();                           // clear EOF flags
arq_time.seekg(arq_time.tellp());           // resync get/put positions

Also remember seekp(pos) overwrites at pos; it never inserts.

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

Thanks

now im having problem with this:

fstream arq_time;
arq_time.open("recordes.gsp",ios::in|ios::out);

arq_time.seekp(0);
arq_time<< "\t\t\t\t  RECORDS\n\n\n";

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

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

Edit:
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, networking, learning, and sharing knowledge.