943,969 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 5235
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Jun 29th, 2006
0

Writing to certain position in a file

Expand Post »
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

  1. #include<stdlib.h>
  2.  
  3. main()
  4.  
  5. {
  6. FILE *stream;
  7. char buffer[20]="XX";
  8. fpos_t file_pos;
  9.  
  10. stream = fopen("ste.txt", "a+" );
  11.  
  12. fseek(stream, 10, SEEK_SET );
  13.  
  14. fgetpos(stream, &file_pos );
  15. fsetpos(stream, &file_pos );
  16. printf("File Pos :%ld\n", file_pos );
  17.  
  18. fwrite(&buffer,1,file_pos,stream );
  19.  
  20. fclose(fp);
  21.  
  22. exit(1);
  23. }
Last edited by Dave Sinkula; Jun 29th, 2006 at 10:04 am.
Similar Threads
Reputation Points: 9
Solved Threads: 0
Junior Poster in Training
sgriffiths is offline Offline
61 posts
since Jun 2006
Jun 29th, 2006
1

Re: Writing to certain position in a file

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...
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jun 29th, 2006
0

Re: Writing to certain position in a file

Quote 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
Reputation Points: 9
Solved Threads: 0
Junior Poster in Training
sgriffiths is offline Offline
61 posts
since Jun 2006
Jun 29th, 2006
1

Re: Writing to certain position in a file

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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jun 29th, 2006
0

Re: Writing to certain position in a file

I thanks

I have tried that but its putting the data to the end of the file, and not the position i want
Reputation Points: 9
Solved Threads: 0
Junior Poster in Training
sgriffiths is offline Offline
61 posts
since Jun 2006
Jun 29th, 2006
1

Re: Writing to certain position in a file

Then post your current code, the file you're using, and tell use what compiler and OS you're using.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jun 29th, 2006
0

Re: Writing to certain position in a file

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.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Jun 29th, 2006
0

Re: Writing to certain position in a file

Quote originally posted by Narue ...
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



  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. main()
  5.  
  6.  
  7. {
  8.  
  9. FILE *fp;
  10. char buffer[]="STEPHEN";
  11. fpos_t file_pos;
  12. char c;
  13.  
  14. fp = fopen("ste.txt", "rb+" );
  15.  
  16. fseek(fp, 20, SEEK_SET );
  17.  
  18. fgetpos(fp, &file_pos );
  19.  
  20. printf("File Pos :%ld\n", file_pos );
  21.  
  22. c = getc(fp);
  23. printf("Character is : %c\n", c );
  24.  
  25. fwrite(buffer,sizeof(buffer),1,fp);
  26.  
  27. fclose(fp);
  28.  
  29. }
Attached Files
File Type: txt ste.txt (924 Bytes, 50 views)
Last edited by Dave Sinkula; Jun 29th, 2006 at 10:35 am.
Reputation Points: 9
Solved Threads: 0
Junior Poster in Training
sgriffiths is offline Offline
61 posts
since Jun 2006
Jun 29th, 2006
0

Re: Writing to certain position in a file

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. main()
  5.  
  6.  
  7. {
  8.  
  9. FILE *fp;
  10. char buffer[]="STEPHEN";
  11. fpos_t file_pos;
  12. char c;
  13.  
  14. fp = fopen("ste.txt", "rb+" );
  15.  
  16. fseek(fp, 18, SEEK_SET );
  17. fwrite(buffer, sizeof(buffer), 1, fp);
  18.  
  19. fgetpos(fp, &file_pos );
  20.  
  21. printf("File Pos :%ld\n", file_pos );
  22.  
  23. c = getc(fp);
  24. printf("Character is : %c\n", c );
  25.  
  26.  
  27. fclose(fp);
Last edited by andor; Jun 29th, 2006 at 10:38 am.
Reputation Points: 251
Solved Threads: 29
Posting Whiz in Training
andor is offline Offline
274 posts
since Jun 2005
Jun 29th, 2006
0

Re: Writing to certain position in a file

Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Integer Separation!!
Next Thread in C Forum Timeline: Help me recognize function?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC