The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Fseek Backwards
Discuss Fseek Backwards in the C Programming forum on Dev Shed. Fseek Backwards C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

March 7th, 2006, 06:12 PM
|
 |
Contributing User
|
|
Join Date: Jun 2003
Posts: 418
  
Time spent in forums: 2 Days 13 h 53 m 25 sec
Reputation Power: 11
|
|
|
Fseek Backwards
Basically, I am trying to go back x amount of spaces in the file and start writing from there.
Here is the code I wrote.
C Code:
Original
- C Code |
|
|
|
#include <stdio.h> char initXMLDoc(int systemNumber, char *fileName); int main() { FILE *fp; initXMLDoc(1234, "files/test.xml"); if((fp = fopen("files/test.xml", "a+b"))==NULL){ printf("\nFailed to open file"); return -1; } fseek(fp, 9, SEEK_END); fwrite("<a>HELLO</a>", 1, 12,fp); fclose(fp); return (0); } char initXMLDoc(int systemNumber, char *fileName){ FILE *fp; if((fp=fopen(fileName, "w"))==NULL){ printf("Failed to create initial XML document"); return 0; } // write encoding msg //fprintf(fp, "<?xml version=\"1.0\" encoding=\"ASCII\"?>"); fprintf(fp, "<system number=\"%d\"></system>",systemNumber ); fclose(fp); return 1; }
any ideas as to what I'm doing wrong?
my output looks like this
XML Code:
Original
- XML Code |
|
|
|
<system number="1234"></system><a>HELLO</a>
but I want it to look like this
XML Code:
Original
- XML Code |
|
|
|
<system number="1234"><a>HELLO</a></system>
Last edited by ecit12 : March 7th, 2006 at 06:31 PM.
|

March 7th, 2006, 06:39 PM
|
 |
Contributing User
|
|
Join Date: Jun 2003
Posts: 418
  
Time spent in forums: 2 Days 13 h 53 m 25 sec
Reputation Power: 11
|
|
|
hmm... a fix seems to be to open the file in "rb+" mode..
the end component gets overwriteen, but thats ok.
|

March 7th, 2006, 09:41 PM
|
|
Contributing User
|
|
Join Date: Feb 2006
Location: Missouri
Posts: 59

Time spent in forums: 13 h 34 m 28 sec
Reputation Power: 8
|
|
if you open a file in append mode, you can not seek...found that out the hard way!
And to seek backwards you supply a negative offset.
fseek(fp, -9, SEEK_END)
|

March 8th, 2006, 01:31 PM
|
 |
Contributing User
|
|
Join Date: Jun 2003
Posts: 418
  
Time spent in forums: 2 Days 13 h 53 m 25 sec
Reputation Power: 11
|
|
|
yup... I kind of figured it out the hard way.. lol..
I'm just disappointed that my book
"C the complete reference manual" by Herbert Schildt failed to mention that.
|

March 8th, 2006, 03:56 PM
|
 |
Contributing User
|
|
|
|
Quote: | Originally Posted by ecit12 I'm just disappointed that my book
"C the complete reference manual" by Herbert Schildt failed to mention that. | Sorry to chuckle, but get used to it.
http://www.catb.org/jargon/html/B/bullschildt.html
[BTW, it was my first C book. I've since done my penance however.]
__________________
Any advertisement triggered by IntelliTxt in this post is not endorsed by the author of this post.
|

March 8th, 2006, 06:15 PM
|
 |
Contributing User
|
|
Join Date: Jun 2003
Posts: 418
  
Time spent in forums: 2 Days 13 h 53 m 25 sec
Reputation Power: 11
|
|
|
Actually, my bad, the book I am using is the C++ the complete reference which includes a big section on C (basically stripped out from the original C book he wrote)
well, I've learned most of my C through this book and the devShed community. Its a decent book to start with when you just want to do the basics, but beyond that its far from the COMPLETE REFERENCE
I had no idea that ppl hated it that much to make a word for it..Interesting site none the less.. lol
Last edited by ecit12 : March 8th, 2006 at 06:18 PM.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|