求助:如何读写*.text文件?

gaoxiaopai 2003-10-05 10:39:29
求助:如何读写*.text文件?
期望能给出实例!
...全文
130 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
akiko 2003-10-05
  • 打赏
  • 举报
回复
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();
ssbull 2003-10-05
  • 打赏
  • 举报
回复
读写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文件中了
flinming 2003-10-05
  • 打赏
  • 举报
回复
和其他文件操作是一样的。。。。。。。。。。。
jijuzheng 2003-10-05
  • 打赏
  • 举报
回复
CStdioFile
ReadString Reads a single line of text.
WriteString Writes a single line of text.
iampolaris 2003-10-05
  • 打赏
  • 举报
回复
一个幻方问题

运行 一下!!!!!!!!!!!
iampolaris 2003-10-05
  • 打赏
  • 举报
回复
#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);
}

gaoxiaopai 2003-10-05
  • 打赏
  • 举报
回复
a

16,551

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Creator Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧