Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » trouble writing file

This thread is locked; no one can reply to it. rss feed Print
trouble writing file
William Labbett
Member #4,486
March 2004
avatar

al_fseek( file, 4, ALLEGRO_SEEK_SET );
   
   pf(" writing version as 2.\n");
   
   al_fwrite32le( file, (int32_t) 2);
   
   al_fseek( file, 4, ALLEGRO_SEEK_SET );
   
   v = al_fread32le( file );
   
   pf(" version is after write is %d.\n",v);

Hi,

in this code, I'm trying to change a number from 1 to 2 in the file.

Since it's an int32_t and the version number is the second number in the file, I call al_fseek(file, 4, ALLEGRO_SEEK_SET) to get to the second int32_t.

After the write, I seek back to the position of the number and read it to v and print it out but it remains 1, not 2 like I'm trying to achieve.

Any ideas why it isn't working ?

Arthur Kalliokoski
Second in Command
February 2005
avatar

Try an al_fflush() after the write?

“Throughout history, poverty is the normal condition of man. Advances which permit this norm to be exceeded — here and there, now and then — are the work of an extremely small minority, frequently despised, often condemned, and almost always opposed by all right-thinking people. Whenever this tiny minority is kept from creating, or (as sometimes happens) is driven out of a society, the people then slip back into abject poverty. This is known as "bad luck.”

― Robert A. Heinlein

William Labbett
Member #4,486
March 2004
avatar

Cheers Arthur. I put one in but it hasn't changed anything.

#SelectExpand
1void convert_file_from_v1_to_v2( ALLEGRO_FILE *file ) 2{ 3 4 int num_reps; 5 6 int rep_num; 7 8 int v; 9 10 F_REP frep; 11 12 if(file == NULL) 13 { 14 pf(" convert_file_from_v1_to_v2 : file is NULL.\n"); 15 system("PAUSE"); 16 exit(1); 17 } 18 19 al_fseek( file, 0, ALLEGRO_SEEK_SET ); 20 21 num_reps = al_fread32le( file); 22 23 /* Write 2 as the version number. */ 24 25 al_fseek( file, 4, ALLEGRO_SEEK_SET ); 26 27 pf(" writing version as 2.\n"); 28 29 al_fwrite32le( file, (int32_t) 2); 30 31 al_fseek( file, 4, ALLEGRO_SEEK_SET ); 32 33 v = al_fread32le( file ); 34 35 pf(" version is after write is %d.\n",v); 36 37 pf(" converting file from version 1 to version 2....\n"); 38 pf(" num_reps = %d.\n", num_reps); 39 40 41 al_fseek( file, 8, ALLEGRO_SEEK_SET); 42 43 44 for( rep_num = 0; rep_num < num_reps; ++rep_num ) 45 { 46 al_fread( file, &frep, sizeof(F_REP)); 47 48 if( frep.type > 1 ) 49 { 50 frep.type += 1; 51 52 write_f_rep_( file, rep_num, frep); 53 } 54 } 55 56 pf("converted file.\n\n"); 57 58 al_fflush(file); 59 60}

Matthew Leverton
Supreme Loser
January 1999
avatar

What mode are you opening the file as?

William Labbett
Member #4,486
March 2004
avatar

#SelectExpand
1if(type == LOADED_FILE) 2 { 3 load : 4 5 a_file = al_fopen( fn, "a + b"); 6 if( a_file == NULL ) 7 { 8 printf(" edit_feature_data() : couldn't load file %s\n", fn); 9 return QUIT_DUE_TO_PROBLEM; 10 } 11 else 12 { 13 pf("loaded %s.\n", fn); 14 } 15 16 al_fseek( a_file, 0, ALLEGRO_SEEK_SET); 17 18 num_reps = al_fread32le(a_file); 19 20 eof_check_1 21 22 frd_file_version = al_fread32le(a_file); 23 24 eof_check_1 25 26 feature_selected = num_reps - 1; 27 28 load_fr = 1; 29 30 pf(" num_reps is %d.\n", num_reps); 31 pf("loaded frd file. Version is %d.\n", frd_file_version); 32 33 if( frd_file_version == 1 && CURRENT_VERSION == 2) 34 { 35 convert_file_from_v1_to_v2( a_file ); 36 37 38 al_fclose( a_file ); 39 40 goto load; 41 } 42 43 44 45 }

Matthew Leverton
Supreme Loser
January 1999
avatar

I think you want "r+b". Did you check to see if all of your new integers are being appended at the end of the file?

William Labbett
Member #4,486
March 2004
avatar

Thanks. It was "r + b" but I thought I needed to change it to "a + b" as I was trying to write to the file, not just to read from it.

I've changed it back and it's working now :) .

On to the next problem ::)

/** EDIT : solved :) */

Thomas Fjellstrom
Member #476
June 2000
avatar

yeah, when you add a + to the mode, it means to open in read/write mode. r+ means to open rw, with the file pos at the start of the file, and a+ means to open with the file pos at the end. w+ is a bit different, it'll open the file for reading and writing, but it'll truncate the file first.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Go to: