Link to home
Start Free TrialLog in
Avatar of ptrennum
ptrennum

asked on

Probelm with CStdioFile ReadString

I'm reading in a file line by line like this:

CString strLine;
while(!fUserList.ReadString(strLine))
{
     arUsers.Add(strLine);
}

the file itself looks like this:

mark,p
trennum,t
yello,y


when it hits the while loop it crashes and gives me the error "An unknown error occured While accessing file"  Not quite sure why this CStdioFile is giving me so many problems or why I'm getting that error.

PT
Avatar of Axter
Axter
Flag of United States of America image

Hi ptrennum,

Are you opening the file first?
Please post the code that opens the file.

Does it crash when it hits the start of the loop or within the loop itself?

David Maisonave :-)
Cheers!
Avatar of ptrennum
ptrennum

ASKER

Start of loop and yes I am opening it,

fUsers.Open("C:\\UserID.inf", CFile::modeCreate || CFile::modeNoTruncate || CFile::modeRead, &e);
ptrennum,

What do you have a ! (not) condition for your while loop?
I don't think that belongs there.
Should be something like the following:

while(fUserList.ReadString(strLine))
{
     arUsers.Add(strLine);
}

David Maisonave :-}
ptrennum,
> >fUsers.Open("C:\\UserID.inf", CFile::modeCreate || CFile::modeNoTruncate
> >|| CFile::modeRead, &e);

Why are you using modeCreate in your open command to open a file for read access?

David Maisonave :-}
Yes you are certainly right about that one,
However I am still getting the same crash if debugging it takes me into wincore.cpp
I am passing the file to this function:
GetListFromFile(CStdioFile &fUserList)

the call looks like this
if(!lstUsers.GetListFromFile(fUsers))

ptrennum,
> >if(!lstUsers.GetListFromFile(fUsers))

Please post the full code for function GetListFromFile

David Maisonave :-}
CString strLine;
      while(fUserList.ReadString(strLine))
      {
            
            arUsers.Add(strLine);
      }

      if(arUsers.GetSize() > 0)
            return true;
      else
            return false;
Please post the full code for function GetListFromFile,
to include function declaration.

Also can you post the code calling GetListFromFile?  Starting from the section that declares the CStdioFile object, to the point where you call GetListFromFile.

ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
BOOL GetListFromFile(CStdioFile &fUserList);  // declaration

//definition
GetListFromFile(CStdioFile &fUserList)
{
CString strLine;
     while(fUserList.ReadString(strLine))
     {
         
          arUsers.Add(strLine);
     }

     if(arUsers.GetSize() > 0)
          return true;
     else
          return false;
}
CStdioFile fUsers; // declared in .h file
fUsers.Open("C:\\UserID.inf", CFile::modeCreate || CFile::modeNoTruncate || CFile::modeRead, &e);

if(!lstUsers.GetListFromFile(fUsers))
            MessageBox("No Clients On File.","No Clients",MB_ICONERROR);

BTW, to elaborate - by using

fUsers.Open("C:\\UserID.inf", CFile::modeCreate || CFile::modeNoTruncate || CFile::modeRead, &e);

you're effectively setting 'modeWrite', which is certainly not what you want to do.
>>since you want to use a binary OR, not a boolean.

Good catch!
ptrennum,
I thought you change your code to get rid of modeCreate, as I previously pointed out?

You should just open it with CFile::modeRead

fUsers.Open("C:\\UserID.inf", CFile::modeRead, &e);