Saturday, July 5th 2008, 2:15pm UTC+1

You are not logged in.

  • Login
  • Register

Dear visitor, welcome to QtForum.org. If this is your first visit here, please read the Help. It explains how this page works. You must be registered before you can use all the page's features. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

1

Sunday, May 4th 2008, 11:49pm

Read/Write INT from Qfile

Hi, I was wondering if I can read an Int from a QFile and the write a new one?

More specificly, I am reading in an about 10 intergers; into an empty array of 11 Intergers; adding a number to the last position of the array; and then writing the first 10 values in that array back iinto the file, replacing the old values.
  • Go to the top of the page

2

Monday, May 5th 2008, 6:09pm

RE: Read/Write INT from Qfile

look at
qint64 QFile::pos()
bool QFile::seek( qint64 pos )
Nicolas
  • Go to the top of the page

3

Tuesday, May 6th 2008, 4:49am

  • Go to the top of the page

4

Tuesday, May 6th 2008, 8:39am

try something such as

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#defines NB_MEM 10
int   scores[NB_MEM+1]; // one more than number of already existing scores
int   moves_x;
int   moves_o;
bool  wonx;
bool  wono;
QFile file;
file.setFileName("highscores.txt");

file.open( QIODevice::ReadOnly );
QDataStream in( &file );
for( int count = 0; (count < NB_MEM) || (! file.atEnd()); count++ ) in >> scores[count];
file.close();

if     ( wonx ) scores[NB_MEM] = moves_x;
else if( wono ) scores[NB_MEM] = moves_o;
int_qsort( scores, NB_MEM+1 );

file.open( QIODevice::WriteOnly );
QDataStream out( &file );
for( int count = 0; count < NB_MEM; count++ ) out << scores[count];
file.close();

notice that QDataStream produces binary data
if you want text files, for instance to verify, you should better use QTextStream
Nicolas

This post has been edited 2 times, last edit by "Nicolas SOUCHON" (May 6th 2008, 8:47am)

  • Go to the top of the page

5

Wednesday, May 7th 2008, 7:50pm

Makin QDataStream files is better in my case, because the user can't edit the HS list with a text editor and cheat :-)

That seems to be working, thx

This post has been edited 1 times, last edit by "jpenguin" (May 7th 2008, 7:55pm)

  • Go to the top of the page

Rate this thread