vc读取文本文件

本文介绍了在VC中处理文本文件的方法,包括使用CStdioFile和CFile类的读写操作,强调了CFile的Read和Write函数,并提醒注意行结束符和文件定位。示例代码展示了如何读取文件、更新行内容并写回文件。

CString   csLine;  
  try  
  {  
        CStdioFile   file("c://autoexec.bat",CFile::modeRead);  
        while(file.ReadString(csLine))  
              AfxMessageBox(csLine);  
  }  
  catch(...)  
  {  
            AfxMessageBox("打开文件错");  
  }  
参考一段代码:使用ifstream  
  char*   pszPath   =   NULL;  
  ::GetMacroBasePath(&pszPath);  
  ifstream   file;  
  file.open(pszPath,ios::nocreate|ios::in);  
  if(pszPath)   delete   pszPath;  
  if(!file.good())   return   rtnValue;  
   
  char   buffer[256];  
  file.getline(buffer,255);  
  if(_stricmp(buffer,"VERSION   =   1.0")   !=   0)  
  return   rtnValue;  
   
  CString   strTemp(_T(""));  
  file.getline(buffer,255);  
  while(_stricmp(buffer,"")   !=   0)  
  {  
  strTemp   =   CString(buffer);  
                                      。。。  
                                      file.getline(buffer,255);  
                      }  
CStdioFile   f;  
  CFileException   e;  
  char   *FileName   =   "temp.txt";  
  if(   !f.Open(   FileName,   CFile::modeRead,   &e   )   )  
  {   return;}  
  CString   line;  
  while   (f.ReadString(line))  
  {  
  }  
  f.Close();
char   szT[256];  
  memset(   szT,   0,   256   );  
  fp   =   fopen(   "test.txt",   "r"   );  
   
  fscanf(   fp,   "%s",   szT   );  
  strcpy(   szcc,   szT   );  
  while(   !feof(   fp   )   )  
  {  
  fscanf(   fp,   "%s",   szT   );  
  strcat(   szcc,   szT   );  
  }  
  fclose(   fp   );  
  CStdioFile   file;  
  CString   str;  
  file.Open("c:/1.txt",CFile::modeRead);  
  while(file.ReadString(str))  
  {  
          //从文件中读出的内容写到了str中;  
          //做想做的事情;  
  }  
FILE*   fp;  
  char   cRead[1024]={0};    
  fp=fopen("test.txt","wt");  
  while(!feof(fp))  
  {  
          fget(cRead,500,fp);  
          ......  
  }  
 FILE   *in;  
  char   *ret;  
  char   txtPath[200];////path   and   file   name  
  char   buff[200];  
  if((in=fopen(txtPath,"r"))==NULL)  
  {  
  MessageBox("/nOpen   txt   file   failure!/n");  
  return;  
  }  
  while(!feof(in))  
  {  
  ret=fgets(buff,200,in);////从文本文件中读取一行的文本  
  if(ret==NULL)   break;  
                    ////  
                    other   operation  
                    ////  
  }  
  fclose(in);  
   
#include<fstream>//使用C++标准库  
  using   namespace   std;  
   
  struct   Record  
  {  
      int   id;  
      char   name[256];  
      char   other[256];//CString类本身有指针,读写麻烦,容易出错。固用数组  
  };  
   
  //写入文件  
  struct   Record   info;  
  ofstream   outbal("test.bat",ios::out|ios::binary)//二进制打开,默认为文本  
  if(!outbal)  
  {  
      return   false;//打开文件失败  
  }  
  outbal.write((char   *)&info,sizeof(struct   info));  
  outbal.close();  
   
  //读取文件  
  struct   Record   info;  
  ifstream   inbal("test.bat",ios::in|ios::binary)//二进制打开,默认为文本  
  if(!inbal)  
  {  
      return   false;//打开文件失败  
  }  
  inbal.read((char   *)&info,sizeof(struct   info));//读取到变量info中,如果文件有多组值,可用循环  
  inbal.close();  
#include "stdafx.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

#define PATHNAME "E:/1.txt"
#define COUNT 2064

int getMark(char*, char[]);
int getAttr(char* , float[], int*);
int checkData(char*);

int main(int argc, char* argv[])
{
int* pMark = NULL;
float* pAttr[COUNT];
char* buff = NULL;
char ch[9];
float attr[24];
int i = 0;
int icnt;

FILE* pf = NULL;

if (!(pf = fopen(PATHNAME, "r+"))) {
printf("error!!!");
return 0;
}


pMark = new int[COUNT];
if (!pMark) {
printf("error!!!");
return 0;
}
memset(pMark, 0, COUNT*sizeof(int));

buff = new char[256];
if (!buff) {
printf("error!!!");
return 0;
}
memset(buff, 0, 256*sizeof(char));

while (!feof(pf) && i < COUNT) { // 1.判断文件是否到了结尾 2.判断读取行数是否超过COUNT
pAttr[i] = new float[24];
if (!pAttr[i]) {
printf("error!!!");
return 0;
}
memset(pAttr[i], 0, 24*sizeof(float));
i++;

fgets(buff, 256, pf); // 读取256字节数据或者到一行结束的数据

if (checkData(buff) == -1)
continue;

memset(ch, 0, 9*sizeof(char));
getMark(buff, ch); // 得到每行的第一个数据里
*(pMark + i - 1) = atoi(ch); // 写到第一个数组里

memset(attr, 0, 24*sizeof(float));
getAttr(buff, attr, &icnt); // 得到每行的其它数据后,存在数组attr里
memcpy(pAttr[i - 1], attr, (icnt+1)*sizeof(float)); // 将数组attr中的数据拷贝到第二个数组里

}


/*
1.txt:
1, 0.056183, 0.015906, 0.003513, 0.000283, 0.000003, 0.000828, 0.111811
1, 0.020147, 0.005431, 0.001509, 0.000059, 0.000003, 0.000797, 0.111808
2, 0.026044, 0.001199, 0.001091, 0.000081, 0.000003, 0.000794, 0.111806
2, 0.090909, 0.008505, 0.003210, 0.001535, 0.000004, 0.000909, 0.111562
3, 0.354464, 0.198575, 0.028486, 0.024844, 0.000663, 0.012116, 0.113298
3, 0.552498, 0.364998, 0.240247, 0.210239, 0.047230, 0.130700, 0.010317

*(pMark+0) = 1;
*(pMark+1) = 1;
*(pMark+2) = 2;
*(pMark+3) = 2;
*(pMark+4) = 3;
*(pMark+5) = 3;
...

*(pAttr[0]+0) = 0.056183
*(pAttr[0]+1) = 0.015906
...
*(pAttr[1]+0) = 0.020147
*(pAttr[1]+1) = 0.005431
...
*/
delete [] pMark;
delete [] buff;
for (int j = 0; j < i; j++) {
delete [] pAttr[j];
}

fclose(pf);
return 0;
}

int checkData(char* buff)
{
/*
此处为数据格式check,过程省略。
现在默认你的数据都是合法的,如果有非法数据此程序可能会出错,此时必须对数据格式进行check
buff:
1, 0.056183, 0.015906, 0.003513, 0.000283, 0.000003, 0.000828, 0.111811
*/
return 0;
}

int getMark(char* buff, char ch[])
{
int iLength = strlen(buff);
for (int i = 0; i < iLength; i++) {
if (buff[i] == ',') {
memcpy(ch, buff, i);
break;
}
}

return 0;
}

int getAttr(char* buff, float attr[], int* icnt)
{
char ch[9];
int iLength = strlen(buff);
*icnt = -1;
int istart = 0;
int iend = 0;
for (int i = 0; i < iLength; i++) {
if (buff[i] == ',') {
iend = i;
(*icnt)++;

if (*icnt < 1) {
istart = iend;
continue;
}

memset(ch, 0, 9*sizeof(char));
memcpy(ch, buff + istart + 2, 8);
attr[*icnt - 1] = (float)atof(ch);
istart = iend;
}

}
return 0;
}


VC对文本文件的操作- CFile没有ReadString和WriteString函数,只有CStdioFile才有,这也许是笔误。
但问题的解决方法恰巧在这里:不要用CStdioFile,而应该使用CFile,用Read和Write成员函数,虽然多一个参数,但是却能够实现你所说的功能。这是因为CStdioFile从严格意义上来说与CFile相差太大,其对文件操作的函数实际上都是调用的标准I/O函数,而CFile中调用的则是SDK函数,这就导致执行的效果和适用的范围上会有所差别。
另外要特别指出的是,要想将该读取的行全部置为0,还需要在调用了Read之后先调用Seek函数将文件指针移回到该行行首再调用Write函数执行写入的操作,否则会导致写入位置的错误。还要特别注意在CFile中读写文件必须处理回车符!
示范代码如下:
CFile file;
CString sTmp, t("0000000000/n");
int iLen = t.GetLength();
file.Open("data.txt", CFile::modeReadWrite);
file.Read(sTmp.GetBuffer(iLen), iLen);
sTmp.ReleaseBuffer();
file.Seek(-iLen, CFile::current);
file.Write(t, iLen);
file.Close();
 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值