Thursday, August 28th 2008, 5:04pm UTC+1

You are not logged in.

  • Login
  • Register

1

Sunday, May 4th 2008, 2:25am

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

SD_R3veNG

Beginner

Posts: 32

Location: Belgium

Occupation: student

2

Monday, May 5th 2008, 9:30am

RE: Read/Write Int from QFile

Yes you can..

Try this:

QFile file( "myfile.txt" );
file.open( QIODevice::ReadOnly | QIODevice::Text );

// read your int's out of the file here and close the file afterwards:
file.close();

// now re-open the file in "truncate"-mode, emptying the file and enables you to write in the beginning of the file:
file.open( QIODevice::Truncate | QIODevice::Text );

// write your stuff to the file here

// don't forget to close your file at the end ;-)
file.close();
*/ No comment /*

This post has been edited 1 times, last edit by "SD_R3veNG" (May 5th 2008, 9:32am)

  • Go to the top of the page

3

Tuesday, May 6th 2008, 4:45am

Thanks, it looks like it should work; but I can't get it. By default, the file has 10 lines, each with a 9. Heres my code, can you take a look at it

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
int scores[10], moves_x, moves_o;
bool wonx, wono;[/CODE
][code]QFile file;
file.setFileName("highscores.txt");
file.open (QIODevice::ReadOnly | QIODevice::Text);
QDataStream in(&file);
        
for (int count=0; count<10 || !file.atEnd(); count++)
{
	in >> (scores[count]);
}
file.close();

if(wonx)
	scores[10]=moves_x;
if(wono)
	scores[10]=moves_o;
int_qsort(scores, 11);                   //Call funtion to sort in ascending order

file.open( QIODevice::Truncate | QIODevice::Text | QIODevice::ReadWrite);
QDataStream out(&file);
	
for (int count=0; count < 10; count++)    //I only want 10 highscores saved
	out << scores[count] << "\n";
file.close();

This post has been edited 1 times, last edit by "jpenguin" (May 6th 2008, 4:52am)

  • Go to the top of the page

SD_R3veNG

Beginner

Posts: 32

Location: Belgium

Occupation: student

4

Tuesday, May 6th 2008, 2:16pm

Quoted

Originally posted by jpenguin
Thanks, it looks like it should work; but I can't get it. By default, the file has 10 lines, each with a 9. Heres my code, can you take a look at it

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
int scores[10], moves_x, moves_o;
bool wonx, wono;[/CODE
][code]QFile file;
file.setFileName("highscores.txt");
file.open (QIODevice::ReadOnly | QIODevice::Text);
QDataStream in(&file);
        
for (int count=0; count<10 || !file.atEnd(); count++)
{
	in >> (scores[count]);
}
file.close();

if(wonx)
	scores[10]=moves_x;
if(wono)
	scores[10]=moves_o;
int_qsort(scores, 11);                   //Call funtion to sort in ascending order

file.open( QIODevice::Truncate | QIODevice::Text | QIODevice::ReadWrite);
QDataStream out(&file);
	
for (int count=0; count < 10; count++)    //I only want 10 highscores saved
	out << scores[count] << "\n";
file.close();


What goes wrong? Doesn't it compile or you simply don't understand why this should work? It looks fine to me.. What is your output?

ps: to sort in an ascending order.. isn't that the opposite of creating a highscore-list? Shouldn't you be sorting descending?

Hit me back with some info here plz
*/ No comment /*
  • Go to the top of the page

5

Wednesday, May 7th 2008, 7:52pm

I fixed another function in my code and it seems to be working now; thx
  • Go to the top of the page

Rate this thread