[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1121
  • Last Modified:

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
0
ptrennum
Asked:
ptrennum
  • 7
  • 5
  • 2
1 Solution
 
AxterCommented:
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!
0
 
ptrennumAuthor Commented:
Start of loop and yes I am opening it,

fUsers.Open("C:\\UserID.inf", CFile::modeCreate || CFile::modeNoTruncate || CFile::modeRead, &e);
0
 
AxterCommented:
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 :-}
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
AxterCommented:
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 :-}
0
 
ptrennumAuthor Commented:
Yes you are certainly right about that one,
However I am still getting the same crash if debugging it takes me into wincore.cpp
0
 
ptrennumAuthor Commented:
I am passing the file to this function:
GetListFromFile(CStdioFile &fUserList)

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

0
 
AxterCommented:
ptrennum,
> >if(!lstUsers.GetListFromFile(fUsers))

Please post the full code for function GetListFromFile

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

      if(arUsers.GetSize() > 0)
            return true;
      else
            return false;
0
 
AxterCommented:
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.

0
 
jkrCommented:
Actually, you're incorrectly setting the file flags when opening:

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

should be

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

since you want to use a binary OR, not a boolean.
0
 
ptrennumAuthor Commented:
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);

0
 
jkrCommented:
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.
0
 
AxterCommented:
>>since you want to use a binary OR, not a boolean.

Good catch!
0
 
AxterCommented:
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);
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 7
  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now