简体   繁体   English

在MFC中读取.CSV文件内容

[英]Read .CSV file contents in MFC

Hi everyone I am a C++ MFC beginner and I want some help about how to read a .CSV file contents in C++ MFC by inherited the CStdioFile class. 大家好,我是C ++ MFC的初学者,我想要一些有关如何通过继承CStdioFile类在C ++ MFC中读取.CSV文件内容的帮助。

the file has data like that: 该文件具有如下数据:

AAAAAAAAA;BBBBBBB;CCCCCCC;DDDDDDDD; AAAAAAAAA; BBBBBBB; CCCCCCC; DDDDDDDD; EEEEEEEE;FFFFFFFF; EEEEEEEE; FFFFFFFF; etc... 等等...

AAAAAAAA is directly the string without the "" for example like: test1;test2;... AAAAAAAA直接是不带“”的字符串,例如:test1; test2; ...

and represent values in a excel in reality. 并在现实中表现出卓越的价值。

What I want is to create a new reader function with two parameters(line and column) that read into the file and extracts the good value and return it as a CString 我想要的是用两个参数(行和列)创建一个新的读取器函数,该函数读入文件并提取好的值并将其作为CString返回

For the moment this is the piece of code I have: 目前,这是我的一段代码:

CString ReadData(int rl, int rc)
{
    CString sLine;
    LPCTSTR p = sLine;
    unsigned int i=1;
    unsigned int j=1;
    CString s;

    while(*p!= EOF)
    {
        if(*p==';' && i==rl)
            s.AppendChar(*p);
        if(*p=='\n')
            i++;
        p++;
    }
    return s;
}

This is not working and not finished yet. 这不起作用,尚未完成。

What I want is some help to know a method to do that in the file, how to detect the end of the file( it is the EOF value normally ?), an end of line( '\\n' ?), Do I have to stock the differents strings in one line ? 我想要的是一些帮助,您可以知道在文件中执行此操作的方法,如何检测文件的末尾(通常是EOF值?),行尾(“ \\ n”?),我是否有在一行中存储差异字符串? and of course some help in the code is also welcome. 当然也欢迎在代码中提供一些帮助。

Reading the file one character at a time is slow, you can read in larger blocks in to one string. 一次读取一个字符的文件速度很慢,您可以将较大的块读取为一个字符串。 This works as long as file is not too large. 只要文件不是太大,此方法就起作用。 Then tokenize that string in to different lines, and tokenize each line in to different fields. 然后,将该字符串标记化为不同的行,并标记每一行的不同字段。

By the way, you are asking for "semi-colon separated value" not csv, so change ',' to ';' 顺便说一句,您要的是“分号分隔值”而不是csv,因此请将','更改为';'

void tokenize(vector<CStringA> &v, const CStringA &source, char token)
{
    for (int start = 0, len = source.GetLength(); start < len;)
    {
        int end = source.Find(token, start);
        if (end < 0) end = len;
        v.push_back(source.Mid(start, end - start));
        start = end + 1;
    }
}

void foo()
{
    CStringA source; //read the whole file in to one string "source"
    const int bufsize = 512 + 1;
    char buf[bufsize];
    CFile file("files.csv", CFile::modeRead|CFile::typeBinary);
    int count;
    while ((count = file.Read(buf, bufsize - 1)) > 0) {
        buf[count] = 0;
        source += buf;
    }

    source.Remove('\r');
    vector<CStringA> lines; //tokenize in to separate lines and store in vector
    tokenize(lines, source, '\n'); 

    for (int row = 0; row < (int)lines.size(); row++) {
        vector<CStringA> fields; //tokenize in to different fields
        tokenize(fields, lines[row], ',');
        for (int column = 0; column < (int)fields.size(); column++)
            TRACE("%s|", fields[column]); //print each field for testing
        TRACE("\n");
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM