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.

I don't do much in the way of I/O using C. However, this reference:

http://www.cppreference.com/

indicates that

"rb+"

opens a binary file for read/write operations.

Good luck.

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);                             
                                           
}
#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);

WORKING!!!

My thanks to the people who assisted me.

Much appreciated

Best Regards

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.