954,496 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Writing to certain position in a file

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); 
}
sgriffiths
Junior Poster in Training
61 posts since Jun 2006
Reputation Points: 9
Solved Threads: 0
 

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

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 
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

sgriffiths
Junior Poster in Training
61 posts since Jun 2006
Reputation Points: 9
Solved Threads: 0
 

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.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

I thanks

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

sgriffiths
Junior Poster in Training
61 posts since Jun 2006
Reputation Points: 9
Solved Threads: 0
 

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

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

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.

Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
 
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 ste.txt (0.9KB)
sgriffiths
Junior Poster in Training
61 posts since Jun 2006
Reputation Points: 9
Solved Threads: 0
 
#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);
andor
Posting Whiz in Training
276 posts since Jun 2005
Reputation Points: 251
Solved Threads: 29
 
Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

WORKING!!!

My thanks to the people who assisted me.

Much appreciated

Best Regards

sgriffiths
Junior Poster in Training
61 posts since Jun 2006
Reputation Points: 9
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You