| | |
Writing to certain position in a file
Thread Solved |
•
•
Join Date: Jun 2006
Posts: 61
Reputation:
Solved Threads: 0
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
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
C Syntax (Toggle Plain Text)
#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); }
Last edited by Dave Sinkula; Jun 29th, 2006 at 10:04 am.
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...
You may also want to read up on how file streams work in C...
I'm here to prove you wrong.
•
•
Join Date: Jun 2006
Posts: 61
Reputation:
Solved Threads: 0
•
•
•
•
Originally Posted by Narue
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
•
•
Join Date: Jul 2005
Posts: 1,678
Reputation:
Solved Threads: 263
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.
http://www.cppreference.com/
indicates that
"rb+"
opens a binary file for read/write operations.
Good luck.
•
•
Join Date: Jun 2006
Posts: 61
Reputation:
Solved Threads: 0
•
•
•
•
Originally Posted by Narue
Then post your current code, the file you're using, and tell use what compiler and OS you're using.
C Syntax (Toggle Plain Text)
#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); }
Last edited by Dave Sinkula; Jun 29th, 2006 at 10:35 am.
C Syntax (Toggle Plain Text)
#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);
Last edited by andor; Jun 29th, 2006 at 10:38 am.
If you want to win, you must not loose (Alan Ford)
![]() |
Similar Threads
- Please I need help formatting an output file and sorting the names! (C++)
- writing to a file (Java)
- Checking if a file is in Writing/Saving mode (VB.NET)
Other Threads in the C Forum
- Previous Thread: Integer Separation!!
- Next Thread: Help me recognize function?
| Thread Tools | Search this Thread |
#include adobe ansi api array asterisks binarysearch changingto char character cm copyimagefile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax database directory dynamic execv feet fgets file fork forloop frequency function getlasterror givemetehcodez global grade graphics gtkgcurlcompiling hacking hardware highest histogram i/o include incrementoperators infiniteloop input interest kernel keyboard kilometer license linked linkedlist linux linuxsegmentationfault list locate logical_drives looping loopinsideloop. lowest match matrix meter microsoft motherboard mqqueue mysql number odf opensource owf pattern pdf performance pointer posix probleminc process program programming radix recursion recv repetition research reversing scanf segmentationfault sequential shape socket socketprograming standard string systemcall threads turboc unix user voidmain() wab windows.h windowsapi






