Link to home
Start Free TrialLog in
Avatar of god_bless_IT
god_bless_IT

asked on

fwrite fread problem

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>
Avatar of Exceter
Exceter
Flag of United States of America image

#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;
}
Avatar of god_bless_IT
god_bless_IT

ASKER

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>




>> 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
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
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);
}
#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;
}
open the file in read/write mode

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

Give comments



ASKER CERTIFIED SOLUTION
Avatar of akshayxx
akshayxx
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
jmcg: you forgot to mention reason for B.
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.
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.