locked
Reading File using MFC, CFile

    Question

  • Can someone please help me with reading a text file with CFile and parsing the large list of numbers in it into an array.

    I need to read numbers from a file simillar to:

    2041

    1312

    3143

    ...

     

    So, far I could do this:

     

    CFile pfile;

    if(pfile.Open("c:\\xx.dat", CFile::modeRead))

    {

    char pBuf[1600];

    DWORD dwLen=0;

    UINT uiReadSize=0;

     

    dwLen = pfile.GetLength();

    // get the smaller of the two, filesize or input buffer size

    int r=pfile.Seek(0,CFile::end);

    uiReadSize = __min(dwLen, sizeof(pBuf));

    // read the data in to our buffer

    uiReadSize = pfile.Read(pBuf, uiReadSize);

    pfile.Close();

    }

     

    Is there an easy way to do what I wanted than to read each character and interpret it if it is a new line etc..

     

    Thanks in advance..

     

    Wednesday, August 22, 2007 7:32 PM

Answers

  • You might use CStdioFile instead of CFile, CStdioFile provides methods for text handling, you might use method ReadString and then parse int. Hope this helps:

    Code Snippet

    #define N 50
    void ReadFile() {

    inr arr[N];
    CString string;
    CStdioFile input(L"c:\\input.txt",CFile::modeRead);
    for(int i=0;i<N;i++) {
    input.ReadString(string);
    arr[i]=_ttoi(string.GetString());
    }

    }


    Thursday, August 23, 2007 4:40 AM

All replies

  • You might use CStdioFile instead of CFile, CStdioFile provides methods for text handling, you might use method ReadString and then parse int. Hope this helps:

    Code Snippet

    #define N 50
    void ReadFile() {

    inr arr[N];
    CString string;
    CStdioFile input(L"c:\\input.txt",CFile::modeRead);
    for(int i=0;i<N;i++) {
    input.ReadString(string);
    arr[i]=_ttoi(string.GetString());
    }

    }


    Thursday, August 23, 2007 4:40 AM
  • How to write text file using MFC?

     

    Thank you very much..

    Monday, March 24, 2008 7:15 AM