C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old March 7th, 2006, 06:12 PM
ecit12's Avatar
ecit12 ecit12 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 418 ecit12 User rank is Corporal (100 - 500 Reputation Level)ecit12 User rank is Corporal (100 - 500 Reputation Level)ecit12 User rank is Corporal (100 - 500 Reputation Level)ecit12 User rank is Corporal (100 - 500 Reputation Level) 
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
  1. #include <stdio.h>
  2. char initXMLDoc(int systemNumber, char *fileName);
  3. int main()
  4. {
  5.     FILE *fp;
  6.     printf("\nInit XML doc");
  7.     initXMLDoc(1234"files/test.xml");
  8.     if((fp = fopen("files/test.xml", "a+b"))==NULL){
  9.         printf("\nFailed to open file");
  10.         return -1;
  11.     }
  12.     fseek(fp, 9, SEEK_END);
  13.     fwrite("<a>HELLO</a>", 1, 12,fp);
  14.     fclose(fp);
  15.     return (0);
  16. }
  17.  
  18. char initXMLDoc(int systemNumber, char *fileName){
  19.     FILE *fp;
  20.     if((fp=fopen(fileName, "w"))==NULL){
  21.         printf("Failed to create initial XML document");
  22.         return 0;
  23.     }
  24.     // write encoding msg
  25.     //fprintf(fp, "<?xml version=\"1.0\" encoding=\"ASCII\"?>");
  26.     fprintf(fp, "<system number=\"%d\"></system>",systemNumber );
  27.     fclose(fp);
  28.     return 1;
  29. }


any ideas as to what I'm doing wrong?

my output looks like this
XML Code:
Original - XML Code
  1. <system number="1234"></system><a>HELLO</a>

but I want it to look like this

XML Code:
Original - XML Code
  1. <system number="1234"><a>HELLO</a></system>

Last edited by ecit12 : March 7th, 2006 at 06:31 PM.

Reply With Quote
  #2  
Old March 7th, 2006, 06:39 PM
ecit12's Avatar
ecit12 ecit12 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 418 ecit12 User rank is Corporal (100 - 500 Reputation Level)ecit12 User rank is Corporal (100 - 500 Reputation Level)ecit12 User rank is Corporal (100 - 500 Reputation Level)ecit12 User rank is Corporal (100 - 500 Reputation Level) 
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.

Reply With Quote
  #3  
Old March 7th, 2006, 09:41 PM
Nathan87 Nathan87 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2006
Location: Missouri
Posts: 59 Nathan87 User rank is Private First Class (20 - 50 Reputation Level)Nathan87 User rank is Private First Class (20 - 50 Reputation Level) 
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)

Reply With Quote
  #4  
Old March 8th, 2006, 01:31 PM
ecit12's Avatar
ecit12 ecit12 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 418 ecit12 User rank is Corporal (100 - 500 Reputation Level)ecit12 User rank is Corporal (100 - 500 Reputation Level)ecit12 User rank is Corporal (100 - 500 Reputation Level)ecit12 User rank is Corporal (100 - 500 Reputation Level) 
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.

Reply With Quote
  #5  
Old March 8th, 2006, 03:56 PM
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Apr 2004
Posts: 1,676 Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level)Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level)Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level)Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level)Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level)Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level)Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level)Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Month 3 Days 8 h 23 m 46 sec
Reputation Power: 132
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.

Reply With Quote
  #6  
Old March 8th, 2006, 06:15 PM
ecit12's Avatar
ecit12 ecit12 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 418 ecit12 User rank is Corporal (100 - 500 Reputation Level)ecit12 User rank is Corporal (100 - 500 Reputation Level)ecit12 User rank is Corporal (100 - 500 Reputation Level)ecit12 User rank is Corporal (100 - 500 Reputation Level) 
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Fseek Backwards

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap