Click here to Skip to main content
15,902,492 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How do I use CStdioFile?

I read MSDN and it is kindof vague, I would like to convert from CFile to CStdioFile so I can use the CStdioFile::ReadString member. Do I read the file line by line into a buffer until the buffer holds the whole file or do I read one line into the buffer then parse? Below is working with about 85% efficiently along with the rest of the app.(unacceptable!) I believe the ReadString method will get me to 100%(when I learn the logic of CStdioFile)

C++
/************************************************************************/
/*  Create handle to my file and open it                                */
/************************************************************************/
    CFile myFile;
    CFileException e;       
    BYTE buffer[0x1000];    //4kb(4,096Bytes) buffer to hold csv file

    if(myFile.Open(_T("c:\\myDir\\subDir\\accounts.csv"), CFile::modeRead, &e) == 0)
    {
        e.ReportError();
        e.Delete();
    }

    DWORD dwBytesRemaining = myFile.GetLength();
/************************************************************************/
/*  Read file into buffer, while created a new buffer and adding to it  */
/*      4KB at a time.                                                  */
/************************************************************************/
    while(dwBytesRemaining)
    {
        UINT nBytesRead = myFile.Read(buffer, sizeof(buffer));
        for(int i = 0; i <= sizeof(buffer); i++)
        {
            myBuffer += buffer[i];
        }
        dwBytesRemaining -= nBytesRead;
    }


When I change CFile to CStdioFile and set it up the way I think it will work, it parses the first line at 100% and then I get an assertion error. I'm a little lost using CStdioFile.
_______________________________________________________________________

OK!

Here is the version with CStdioFile, it parses the last line in the file perfectly. How can I start at the beginning of the file? I have "logic brain block syndrome", I can not think of a way to process the whole file. I really need some pointers with this.

/************************************************************************/
/*  Create handle to file and open it   "CStdioFile Section             */
/************************************************************************/
    CFileException cStderr;
    CStdioFile cStdFile;

    if(!cStdFile.Open(_T("c:\\myDir\\subDir\\accountscsv.csv"), CFile::modeRead, &cStderr))
    {
        cStderr.ReportError();
        cStderr.Delete();
    }
/************************************************************************/
/*  Read file 1 line at a time and parse by CSV         "CStdioFile"    */
/************************************************************************/
    CString cStdstr(_T(""));

    while(cStdFile.ReadString(cStdstr))
    {
        int nValues = 0;
        LPCTSTR q = cStdstr;

        while (*q != '\0')  //Keep reading until end of line is reached
        {
            // String to hold parsed value
            CString t;
            if(*q == '"')
                q++;
            //Add characters to t until a comma or EOL
            while (*q != '\0' && *q != ',')
            {
                if(*q == '"')
                    q++;
                if(*q == ',')
                    break;
                t.AppendChar(*q++);
            }
            // Advance to next character (if not already end of string)
            if (*q != '\0')
                q++;
            // Add this string to value array
            if (nValues < cStdioArr.GetCount())
            {
                t.Trim(' ');
                cStdioArr[nValues] = t;
            }
            else
            {
                t.Trim(' ');
                cStdioArr.Add(t);
            }
            nValues++;
        }
    // Trim off any unused array values
    if (cStdioArr.GetCount() > nValues)
        cStdioArr.RemoveAt(nValues, cStdioArr.GetCount() - nValues);
    }
Posted
Updated 20-Dec-11 14:55pm
v3
Comments
Chandrasekharan P 19-Dec-11 0:47am    
Oh Did not see that your question was answered some 10 hours ago and its marked "SOLVED". Why again??
DrBones69 19-Dec-11 1:13am    
Because, I need help understanding CStdioFile. This was not answered 10hrs ago. I can get my app to function with CFile and parse the file with questions answered 10hrs ago. This is a different issue of using CStdioFile.
Chandrasekharan P 19-Dec-11 2:38am    
"I need help understanding CStdioFile". I think MSDN gives you a very good explanation on CStdioFile.

What have you tried using CStdioFile. Why dont you post that code snippet here so that experts here can have a look.

Create a variable of CStdioFile
1. Use this "myFile" to open the File which you want to access.

C++
CStdioFile myFile(csvFile,CFile::modeRead);


2. You can use a while loop to iterate through the file for that you can do something like this
C++
CString str;
while(myFile.ReadString(str))
	{
         //your Operations
        }
      return -1;
 
Share this answer
 
Comments
DrBones69 20-Dec-11 22:16pm    
For some reason this only loads the last line into my array. :(
Try using correct array bounds, e.g. instead of <= sizeof(buffer) use < sizeof(buffer). You are reading one byte past the size of array since you are doing 4097 iterations.

And what is myBuffer - just a checksum?
 
Share this answer
 
Comments
DrBones69 20-Dec-11 22:32pm    
Thanks for the heads up.
I feel a little embarrassed and somewhat relieved. In my CStdioFile version above, I had my counter initialized inside my while loop. So, everytime it would step through a line in the file my counter would be set back to 0. I was overwriting each line by the next.

Thanks for your help.

-DrB :-)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900