我现在要对一文本内容进行修改,文本内容:
HI x=3.1 y=4;
Nihao
HI x=4.2 y=5;
HI x=5 y=6;这时候我要读进一个数j(如j=0.1),要求我此时输出变为:HI x=3.2 y=4;
Nihao
HI x=4.3 y=5;
HI x=5.1 y=6;即所有x=后的数字全部加0.1,该如何做?头疼好几天,望达人指教.

解决方案 »

  1.   

    FILE *stream =NULL;
       int  y;
       float x;
       char ch;
       char s1[3];
       char s2[3];
       char s3[3];
       char s4[3];
       CString str = "";
       CString strTemp = "";
       stream = fopen( "fscanf.txt", "r+" );
       for(int i= 0;i<4;i++)
       {
          if(i!= 2)
          {
              fscanf( stream, "%s%2s", s1,s2);
      fscanf( stream, "%s",s3);
      x = atof(s3);
      x += 0.1;
      fscanf(stream,"%2s",s3);
      fscanf(stream,"%s",s4);
      y = atoi(s4);
      strTemp.Format("%s %s%1.1f %s%d\n",s1,s2,x,s3,y);
              str+=strTemp;
          }
          else
          {
              strTemp = "Nihao\n";
              str += strTemp;
           }
       }
       fseek(stream,0L,SEEK_SET);
       fprintf(stream,str);
    }
      

  2.   

    对了, 把 
    stream = fopen( "fscanf.txt", "r+" );
    改为 
    stream = fopen( "fscanf.txt", "w+" );
      

  3.   

    http://community.csdn.net/Expert/TopicView.asp?id=3990536
    是不是作业哦~~
      

  4.   

    int  y;
    float x;
    char s[MAX_LENGTH];
    FILE *fin = fopen( "fscanf.txt", "r" );
    FILE *fout = fopen( "fprintf.txt", "w" );
    while(!feof(fin))
    {
    if(2==fscanf( fin, "HI x=%f y=%d;\n", &x,&y))
    {
    fprintf(fout, "HI x=%.1f y=%d;\n",x+0.1,y);
    }
    else 
    {
    fscanf(fin,"%s\n",s);
    fprintf(fout, "%s\n",s);
    }
    }
    fclose(fin);
    fclose(fout);