如何用vc读取txt文档数据

如何用vc读取txt文档数据,第1张

CFile读取txt结构

CDaoWorkspace来动态创建Access表结构

CDaoDatabase打开Access表 并通过它的Execute方法执行插入语句 如"Insert Into (,,) Values ()"来添加

牵涉的内容不少啊!

文件在当前目录下,名为123txt。成功打开文件后,建立一个循环,从文件中读取一个字符并判断其是否为'\n',若不是则输出这个字符并继续读取下一下字符;若是'\n'则跳出循环,停止读取、关闭文件。代码如下:

//#include "stdafxh"//If the vc++60, with this line

#include "stdioh"

#include "stdlibh"

int main(void){

    FILE fp;

    char ch;

    if((fp=fopen("123txt","r"))==NULL){

        printf("Open the file failure\n");

        exit(0);

    }

    while((ch=fgetc(fp))!='\n')

        printf("%c",ch);

    fclose(fp);

    printf("\n");

    return 0;

}

读取每行内容,查找Z,获取后面的数据

CString strFileNamePath = "//xxxtxt";

int nFileNum = 0;

CStdioFile myFile;

CStringArray strPDFFileName;

if(myFileOpen(strFileNamePath,CFile::modeRead))

{

//读取;

CString strLine;

while(myFileReadString(strLine))

{

int nPos=strLineFind(_T("Z"));

CString strFileName = strLineMid(nPos+2,1);

strPDFFileNameAdd(strFileName);

nFileNum++;

}

myFileClose();

}

给你一个思路先遍历这个文件夹下的txt文件,然后逐个创建比较时间,并保存最新的文件名到一个变量上面,然后用CFile::read 读取内容。

给你一段伪代码  自己修改试试

HANDLE hSearch; 

WIN32_FIND_DATA FileData, tempFileData;

        hSearch = FindFirstFile("E:\\XX\\txt", &FileData); //首先找到的是“”

if (hSearch == INVALID_HANDLE_VALUE)return 0;

if (! FindNextFile(hSearch, &FileData))  return 0;     //然后找到的是“”

FindNextFile(hSearch, &tempFileData);//把第一个文件的信息保存在tempFileData上

while (1) 

if (!FindNextFile(hSearch, &FileData)) 

break;

else

{

if(CompareFileTime(FileDataftCreationTime ,tempFileDataftCreationTime )==-1){

                         tempFileDataFileData

                         }

}

}

FindClose(hSearch);

        CFile file;

        fileopen("路径+\\tempFileDatacFileName");

        char buf[xx];

        fileread(buf);

        fileclose();

//参数 Filename 为要读的文件名,如:readtxt 。

//ctrlCombox 为一CComboBox 控件,即要用来把文件内容显示到CComboBox 的控件。

//使用举例:CComboBox m_ctrlCOMBOX; //定义一控件。

// ReadFileToCOMBOX("dddtxt",m_ctrlCOMBOX); 即可

void ReadFileToCOMBOX(char Filename,CComboBox & ctrlCombox)

{

ctrlComboxResetContent();

FILE fp=fopen(Filename,"r");

if(fp == NULL)

return ;

char buf[256];

CString strcom;

int index;

while(!feof(fp))

{

memset(buf, 0, 256);

fgets(buf, 256, fp);

//去掉最后一个字符,可能是换行符

index=strlen(buf);

if(index>0)

{

if(buf[index-1]=='\n' || buf[index-1]=='\r')

buf[index-1]=0x00;

}

strcomEmpty();

strcomFormat("%s",buf);

strcomTrimLeft(" "); //去掉左右空格,所一行全是空格,则不显示

//

strcomTrimRight(" ");

if(!strcomIsEmpty())

{

ctrlComboxAddString(strcom);

}

}

int count =ctrlComboxGetCount();

if(count>0)

ctrlComboxSetCurSel(0);

fclose(fp);

}

以上就是关于如何用vc读取txt文档数据全部的内容,包括:如何用vc读取txt文档数据、c语言读取txt文件第一行并显示的源程序、VC中读取txt文件中的内容求助等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/9869886.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-02
下一篇2023-05-02

发表评论

登录后才能评论

评论列表(0条)

    保存