求助:如何读写*.text文件?
期望能给出实例!
期望能给出实例!
解决方案 »
- ON_THREAD_MESSAGE(WM_CALCULATE,OnCalculate)为什么不对呢
- 仍然是灰度图像的纹理贴图问题!
- 在多文档多视图的程序中,怎么让一个指定窗口在前,让另外一个窗口隐藏(不是关闭!)
- 请大家帮帮忙看一下这个CListCtrl的InsertColumn问题
- 求助:为何调用CString的~CString()时会出错?
- 我想做个.cab文件,放在页面上自动下载,我如何写特定位置和内容的注册表数据
- ODBC中CRecordSet中有没有可以得到某个字段的最大最小值的函数
- 给一个关于word的例子好吗?
- 如何实现打印预览?
- MFC串口界面
- 已经没有可用分了,大家还是帮帮忙吧,谢谢了!!!!!
- 如何在打印和预览中分页?
例如: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文件中了
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();
#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);
}
ReadString Reads a single line of text.
WriteString Writes a single line of text.