求助:如何读写*.text文件?
期望能给出实例!

解决方案 »

  1.   

    读写txt文件,可以用fopen或者fstream类来读写呀
    例如:fopen()
    FILE *fp;
    fp=fopen("yourfilename.txt","r");
    while(!eof(fp)) {
      char ch=fgetc(fp);
    }
    这样就可以读取txt文件了。对于fstream类库:
    #include<string>
    #include<fstream>
    using namespace std;void main() {
    string str="This is a test";
    ofstream ofile;
    ofile.open("f:/test.txt",ios::out);
    ofile<<str;
    }
    这样就可以吧数据写入txt文件中了
      

  2.   

    CStdioFile cf;
    cf.Open("c:\\test.txt",CFile::modeReadWrite);
    CString strRead;
    cf.ReadString(strRead);
    cf.ReadString(strRead);
    cf.SeekToEnd();
    CString strWrite="\r\nwrite test";
    cf.WriteString(strWrite);
    cf.Close();
      

  3.   

    #include<stdio.h>
    #include<stdlib.h>
    int N;
    int *M=NULL;void main()
    {
    int i;
    int check(int,int);
    void out();
    void set_3_5(int,int,int,int);
    void set_4();
    void set_6();
    void swap(int,int);printf("Press any key to start:");
    getch();
    printf("\r");
     Begin:printf("Input the rank of the Magic Matrix:");
    scanf("%d",&N);
    if(N<=2){printf("\007\n\nInput Error!Try again!\n");goto Begin;}
    M=(int *)calloc(N*N+1,sizeof(int));
    if(M==NULL)
      {printf("\007\nError:Out of memory!\n");
       goto Next;
      }for(i=0;i<N*N;i++){*(M+i)=0;}
    switch(N%4)
      {case 0:set_4();break;
       case 2:set_6();break;
       default:i=N;set_3_5(0,0,i,0);
      }
    out();
    free(M); Next:printf("\nWould you like to go on?\nPress Esc to exit,any other key to continue:");
    i=getch();
    if(i!=27){putchar('\n');goto Begin;}
      else printf("\007\nThanks for use.\n");
    }int check(int i,int j)
    {switch(i%4)
      {case 0:
       case 3:if(j%4==0||j%4==3)return(1);
        else return(0);
       default:if(j%4==1||j%4==2)return(1);
          else return(0);
      }
    }void set_3_5(int i0,int j0,int mod,int base)
    {int i_try,j_try,k,i=0,j=mod/2;
     *(M+(i+i0)*N+j+j0)=1+base;
     for(k=2;!(k>mod*mod);k++)
       {i_try=i-1,j_try=j-1;
        if(i_try<0)i_try=mod-1;
        if(j_try<0)j_try=mod-1;
        if(*(M+(i_try+i0)*N+j_try+j0)||i==0&&j==0){i_try=i+1,j_try=j;}
        i=i_try;j=j_try;
        *(M+(i+i0)*N+j+j0)=k+base;
       }
    }void set_4()
    {int i,j,c=N*N;
     for(i=0;i<N;i++)
       {for(j=0;j<N;j++)
         {if(check(i,j)){*(M+i*N+j)=c-i*N-j;}
    else {*(M+i*N+j)=i*N+j+1;}
         }
       }
    }void set_6()
    {int i,j,m,u;
     u=N/2;
     m=(u-1)/2;
     set_3_5(0,0,u,u*u);
     set_3_5(0,u,u,2*u*u);
     set_3_5(u,0,u,3*u*u);
     set_3_5(u,u,u,0);
     for(i=0;i<m-1;i++)
       for(j=0;j<u;j++)
         swap(i,j);
     for(i=0;i<m;i++)
       for(j=0;j<u;j++)
         swap(N-1-i,j);
     swap(u+m,m);
     swap(N-1,m);
    }void swap(int i,int j)
    {int p=*(M+i*N+j);
     *(M+i*N+j)=*(M+i*N+j+N/2);
     *(M+i*N+j+N/2)=p;
    }void out()
    {int i,j;
     FILE *fp;
     float c=(float)N*((float)N*(float)N+1)/2; fp=fopen("Magic.txt","wb+");
     if(fp==NULL)
       {printf("Create file ERROR!\n");
        exit(0);
       }
     else
       printf("\007\nYou can find the result in the Magic.txt where you run the program.\n");  for(i=0;i<N;i++)
       {for(j=0;j<N;j++)
          fprintf(fp,"%8d",*(M+i*N+j));
        fprintf(fp,"\n");
       }  fprintf(fp,"Magic Matrix rank=%d,Magic constant=%.0f.\n",N,c);
     fclose(fp);
    }
      

  4.   

    CStdioFile 
    ReadString    Reads a single line of text. 
    WriteString    Writes a single line of text.