Hello
I am trying to write some data to a certain position in a file, but cannot get it to work
I want to write "XX" to position 6 of a specified file, but the data is just being written to the end of file.
see below

#include<stdlib.h> 

main() 

{ 
FILE *stream; 
char buffer[20]="XX"; 
fpos_t file_pos; 

stream = fopen("ste.txt", "a+" ); 

fseek(stream, 10, SEEK_SET ); 

fgetpos(stream, &file_pos ); 
fsetpos(stream, &file_pos ); 
printf("File Pos :%ld\n", file_pos ); 

fwrite(&buffer,1,file_pos,stream ); 

fclose(fp); 

exit(1); 
}

The "a" mode of fopen always appends, regardless of any seeking you do afterward. You want to open the file with "w+b" for write/update access in binary. You have to use binary because text doesn't allow arbitrary seeking like you're trying to do.

You may also want to read up on how file streams work in C...

The "a" mode of fopen always appends, regardless of any seeking you do afterward. You want to open the file with "w+b" for write/update access in binary. You have to use binary because text doesn't allow arbitrary seeking like you're trying to do.

You may also want to read up on how file streams work in C...

the file i am updating already exists, with data, when i try the w+b option, the file is nulled

My brain hasn't been working to well lately, sorry about that. :p You want "r+b" because the "w" mode always empties the file if it already exists.

I thanks

I have tried that but its putting the data to the end of the file, and not the position i want

Then post your current code, the file you're using, and tell use what compiler and OS you're using.

Then post your current code, the file you're using, and tell use what compiler and OS you're using.

SunOS jcmunx3 5.9 Generic_118558-23 sun4u sparc SUNW,Sun-Fire-V240

#include<stdio.h>     
#include<stdlib.h>    
                      
main()                
                                       
 
{                                          
                                           
   FILE *fp;                               
   char buffer[]="STEPHEN";                
   fpos_t file_pos;                        
   char c;                                 
                                           
   fp = fopen("ste.txt", "rb+" );          
                                           
   fseek(fp, 20, SEEK_SET );               
                                           
   fgetpos(fp, &file_pos );                
                                           
   printf("File Pos :%ld\n", file_pos );   
                                           
   c = getc(fp);                           
   printf("Character is : %c\n", c );      
                                           
   fwrite(buffer,sizeof(buffer),1,fp);     
                                           
   fclose(fp);                             
                                           
}
Attachments
B3 9AZ            ALAN                BLOGGS                        000000005000000005000000001010100                              
B4 9AZ            ADAM                BLOGGS                        000000005000000005000000001010100                              
B4 9AZ            A                   BLOGGS                        000000005000000005000000001000001                              
B4 9AZ            ALAN                BLOGGS                        000000005000000005000000001010101                              
B4 9AZ            BARRY               BLOGGS                        000000005000000005000000001010100                              
B4 9AZ            BRIAN               BLOGGS                        000000005000000005000000001010100                              
B4 9AZ            JOHN                BLOGGS                        000000005000000005000000001000001
#include<stdio.h>
#include<stdlib.h>

main()


{

FILE *fp;
char buffer[]="STEPHEN";
fpos_t file_pos;
char c;

fp = fopen("ste.txt", "rb+" );

fseek(fp, 18, SEEK_SET );
fwrite(buffer, sizeof(buffer), 1, fp);

fgetpos(fp, &file_pos );

printf("File Pos :%ld\n", file_pos );

c = getc(fp);
printf("Character is : %c\n", c );


fclose(fp);
This question has already been answered. Start a new discussion instead.