Writing to certain position in a file

Thread Solved

Join Date: Jun 2006
Posts: 61
Reputation: sgriffiths is an unknown quantity at this point 
Solved Threads: 0
sgriffiths sgriffiths is offline Offline
Junior Poster in Training

Writing to certain position in a file

 
0
  #1
Jun 29th, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,630
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 718
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Writing to certain position in a file

 
1
  #2
Jun 29th, 2006
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...
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 61
Reputation: sgriffiths is an unknown quantity at this point 
Solved Threads: 0
sgriffiths sgriffiths is offline Offline
Junior Poster in Training

Re: Writing to certain position in a file

 
0
  #3
Jun 29th, 2006
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
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,630
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 718
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Writing to certain position in a file

 
1
  #4
Jun 29th, 2006
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'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 61
Reputation: sgriffiths is an unknown quantity at this point 
Solved Threads: 0
sgriffiths sgriffiths is offline Offline
Junior Poster in Training

Re: Writing to certain position in a file

 
0
  #5
Jun 29th, 2006
I thanks

I have tried that but its putting the data to the end of the file, and not the position i want
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,630
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 718
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Writing to certain position in a file

 
1
  #6
Jun 29th, 2006
Then post your current code, the file you're using, and tell use what compiler and OS you're using.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,678
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 263
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Writing to certain position in a file

 
0
  #7
Jun 29th, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 61
Reputation: sgriffiths is an unknown quantity at this point 
Solved Threads: 0
sgriffiths sgriffiths is offline Offline
Junior Poster in Training

Re: Writing to certain position in a file

 
0
  #8
Jun 29th, 2006
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. }
Last edited by Dave Sinkula; Jun 29th, 2006 at 10:35 am.
Attached Files
File Type: txt ste.txt (924 Bytes, 5 views)
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 275
Reputation: andor has a spectacular aura about andor has a spectacular aura about andor has a spectacular aura about 
Solved Threads: 29
andor's Avatar
andor andor is offline Offline
Posting Whiz in Training

Re: Writing to certain position in a file

 
0
  #9
Jun 29th, 2006
  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.
If you want to win, you must not loose (Alan Ford)
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,358
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 237
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Writing to certain position in a file

 
0
  #10
Jun 29th, 2006
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC