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

Recommended Answers

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 …

Jump to Post

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.

Jump to Post

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

Jump to Post

All 10 Replies

Re: Writing to certain position in a file 80 80

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...

Re: Writing to certain position in a file 80 80

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

Re: Writing to certain position in a file 80 80

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.

Re: Writing to certain position in a file 80 80

I thanks

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

Re: Writing to certain position in a file 80 80

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

Re: Writing to certain position in a file 80 80

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.

Re: Writing to certain position in a file 80 80

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);                             
                                           
}
Re: Writing to certain position in a file 80 80
#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);
Re: Writing to certain position in a file 80 80
Re: Writing to certain position in a file 80 80

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 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.