我要读一个文本文件,对文本里面的数据操作后再写回去.
过程是先把文本里数据读进来用atoi转换成整型,修改完了难道还要用_itoa转回字符型再输出,是这样的吗?
我下面这程序为什么不行了?写出来的全是0. 谁能指点指点一下啊!
读入的文本文件是: 1 1 0
1 2 0
2 1 0
2 2 0typedef struct tag_Point
{
int x;
int z;
int flag;
}Point;Point place [4],flag[4];void Read()
{
int i;
TCHAR filename[20]=_T("d:\\temp.txt");
FILE *fp = _tfopen(filename, _T("r") ); for(i=0; i<4; i++)
fread(&place[i], sizeof(struct tag_Point ),1,fp); fclose(fp);
for(int i=0;i<4;i++)
flag[i] = atoi(place[i].flag);
}
void Write()
{
int i;
TCHAR str[255]; TCHAR filename[20]=_T("d:\\temp1.txt");
FILE *fp = _tfopen(filename, _T("w+") ); for(i=0; i<4; i++)
fread(&flag[i] ,sizeof(struct tag_Point ),1,fp);
fclose(fp);
}
过程是先把文本里数据读进来用atoi转换成整型,修改完了难道还要用_itoa转回字符型再输出,是这样的吗?
我下面这程序为什么不行了?写出来的全是0. 谁能指点指点一下啊!
读入的文本文件是: 1 1 0
1 2 0
2 1 0
2 2 0typedef struct tag_Point
{
int x;
int z;
int flag;
}Point;Point place [4],flag[4];void Read()
{
int i;
TCHAR filename[20]=_T("d:\\temp.txt");
FILE *fp = _tfopen(filename, _T("r") ); for(i=0; i<4; i++)
fread(&place[i], sizeof(struct tag_Point ),1,fp); fclose(fp);
for(int i=0;i<4;i++)
flag[i] = atoi(place[i].flag);
}
void Write()
{
int i;
TCHAR str[255]; TCHAR filename[20]=_T("d:\\temp1.txt");
FILE *fp = _tfopen(filename, _T("w+") ); for(i=0; i<4; i++)
fread(&flag[i] ,sizeof(struct tag_Point ),1,fp);
fclose(fp);
}
atoi _itoa
分别对应的转换整数和字符串
但你是对结构体转化???
flag[i] = atoi(place[i].flag);