Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

fwrite fread problem

Posted on 2003-02-28
12
Medium Priority
?
683 Views
Last Modified: 2010-04-15
how to read this xml file(ex: dudu.xml), edit it and write it back.. using c / c++

read this:

<dudu>
<data>
<name>sss</name>
<msg>ddd</msg>
</data>
<data>
<name>ccc</name>
<msg>rrr</msg>
</data>
</dudu>


add this to the top of the xml:
name= qqq
msg= www


write this:

<dudu>
<data>
<name>qqq</name>
<msg>www</msg>
</data>
<data>
<name>sss</name>
<msg>ddd</msg>
</data>
<data>
<name>ccc</name>
<msg>rrr</msg>
</data>
</dudu>
0
Comment
Question by:god_bless_IT
  • 3
  • 2
  • 2
  • +4
12 Comments
 
LVL 8

Expert Comment

by:Exceter
ID: 8044240
#include <stdio.h>
#incldue <stdlib.h>

int main( void )
{
     FILE *stream, *stream2;
     char line[100];

     stream = fopen( "dudu.xml", "r" );
     stream2 = fopen( "dudu.tmp", "w" );

     while( fgets( line, 100, stream ) != NULL )
     {
          printf( "%s", line);
          fprintf( stream2, "%s", line );
     }
     fclose( stream );
     fclose( stream2 );

        system( "del dudu.xml" );
        system( "ren dudu.tmp dudu.xml" );

        return 0;
}
0
 

Author Comment

by:god_bless_IT
ID: 8044692
your solutions will read 100 characters from 0 to 99
is that correct?

what I want is to read from the 6th character (after <dudu> ) through the end (unlimited char)

---------------------------------------------------------

because I want to add
x=
"<dudu><data><name>newname</name><msg>newmsg</msg></data>"


-------------------------------------------------------
for example, it wont read "<dudu>"
<dudu><data><name>sss</name><msg>ddd</msg></data><data>
<name>ccc</name><msg>rrr</msg></data></dudu>

var buffer = "<data><name>sss</name><msg>ddd</msg></data><data>
<name>ccc</name><msg>rrr</msg></data></dudu>"

---------------------------------------------------------
then I add x and the buffer:
the result will be:

<dudu><data><name>newname</name><msg>newmsg</msg></data><data><name>sss</name><msg>ddd</msg></data><data>
<name>ccc</name><msg>rrr</msg></data></dudu>




0
 
LVL 8

Expert Comment

by:Exceter
ID: 8044834
>> what I want is to read from the 6th character

That may be what you wanted but it is not what you aksed for. :-)

Exceter
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 8

Expert Comment

by:akshayxx
ID: 8047095
if u r planning to make most of XML's extensibility and structured-ness.. then u better use some XML parser.. like Xerces and libxml.
look for xerces on  xerces.apache.org
and xmlsoft.org for libxml.. u can find examples for getting started .. once u get used to it u can find so much stuff u can do with XML
0
 
LVL 1

Expert Comment

by:TheBeaver
ID: 8049168
Try this then...it takes...
// filename_i = name of input file
// filename_o = name of output file
// name = what you want to replace name field with
// msg = what you want to replace msg field with

ChangeXML(char *filename_i, char *filename_o , char *name, char *msg)
{
  FILE *fi, *fo;
  char ch, tag=0, tagpos=0, ignore=0;
  char tagstr[8]={0};

  fi = fopen(filename_i, "r");
  fo = fopen(filename_o, "w");

  ch = fgetc(fi);
  while( !feof(fi) )
  {
    switch( ch )
    {
      case '<': // Start of tag
        tagpos=0;
        tag=1;
        ignore=0;
        fputc( ch, fo);
        break;

      case '>': // End of tag
        tagstr[tagpos] = '\0';
        tagpos=0;
        tag=0;
        if( !strcmp(tagstr,"name") ) { ignore=1; fprintf(fo, ">%s", name); }
        if( !strcmp(tagstr,"msg") )  { ignore=1; fprintf(fo, ">%s", msg); }
        break;

      default:
        if( !ignore )
        {
          fputc( ch, fo);
          if( tag )
          {
            tagstr[tagpos] = ch;
            tagpos++;
          }
        }
    }
    ch = fgetch(fi);
  }

  fclose(fo);
  fclose(fi);
}
0
 

Expert Comment

by:manu2020
ID: 8078430
#include <stdio.h>
#incldue <stdlib.h>
#include<string.h>

int main( void )
{
    FILE *stream, *stream2;
    char line[10],name[10],msg[20];

    strcpy(name,"qqq");
    strcpy(msg,"www");

    stream = fopen( "dudu.xml", "r" );
    stream2 = fopen( "dudu.tmp", "w" );
   
    fscanf(stream,"%[^>]>\n",line);
    fprintf(stream2,"%s>\n",line);

    /* now adding new record */
    fprintf(stream2,"<data>\n<name>");
    fprintf(stream2,"%s</name>\n<msg>%s </msg> \n </data>\n",name,msg);
    while(!feof(stream))
    {    
        fscanf(stream,"%[^>]>\n",line);
        fprintf(stream2,"%s>\n".line);
     }
    fclose( stream );
    fclose( stream2 );

       system( "del dudu.xml" );
       system( "ren dudu.tmp dudu.xml" );

       return 0;
}
0
 
LVL 12

Expert Comment

by:rajeev_devin
ID: 8086151
open the file in read/write mode

FILE *stream = fopen("xxx.xml","r+");

Give comments



0
 
LVL 8

Accepted Solution

by:
akshayxx earned 300 total points
ID: 8086181
if u really want to do some serious XML work .. then i cant stress more..
USE THE XML PARSER LIBRARIES.. like xerces or libxml
0
 
LVL 20

Expert Comment

by:jmcg
ID: 10024336
Nothing has happened on this question in over 9 months. It's time for cleanup!

My recommendation, which I will post in the Cleanup topic area, is to
accept answer by akshayxx [grade B].

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jmcg
EE Cleanup Volunteer
0
 
LVL 8

Expert Comment

by:akshayxx
ID: 10024633
jmcg: you forgot to mention reason for B.
0
 
LVL 20

Expert Comment

by:jmcg
ID: 10025210
Akshayxx,

I recommended a B grade because this overall thread did not strike me as a prime example for our PAQ database. I hope you're not offended. Your advice seemed to me to be the best response, but it was pretty generic.
0
 

Author Comment

by:god_bless_IT
ID: 10040776
Sorry didnt notice I put a question in this web for monhts
just notice recently when I got a notice email :D

and yes I use XML Parser now.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Suggested Courses

569 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question