Click here to Skip to main content
15,887,464 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am getting all 10 lines (line by line ) in mfc project as CStringArray but recieve error on to getting 4th line string.

my text file is:

C:\Program Files (x86)\GRETECH\GOMPlayer\GOM.EXE
C:\Program Files\VideoLAN\VLC\vlc.exe
C:\Program Files (x86)\Google\Chrome\Application\chrome.exe
C:\Windows\notepad.exe
C:\Windows\System32\cmd.exe
C:\Windows\System32\mspaint.exe
C:\Windows\System32\calc.exe
C:\Windows\System32\calc.exe
C:\Windows\System32\osk.exe
C:\Windows\System32\msconfig.exe

Please check my function or alternate to this function. And please code me fuction for updating any line.

What I have tried:

    CString AMyClass::getStringfromText(CString filename, int linenumber)
    {
    CStdioFile file;	
    CStringArray aLineArray;
    CString All_Lines;
    CString out;
    CString strExePath;
    
    linenumber=linenumber-1;
    CString applicationPath;
    char fname[_MAX_PATH+1];
    
    ::GetModuleFileNameA(NULL,fname,_MAX_PATH);
    applicationPath = fname;
    applicationPath.MakeLower();
    strExePath = applicationPath.Left(applicationPath.ReverseFind('\\') + 1);

    file.Open(strExePath+filename, CFile::modeRead);

    while (file.ReadString(All_Lines))
    {
    aLineArray.Add(All_Lines);

    }
    file.Close ();
    out=aLineArray[linenumber];
    return out;
    }

**and code is:**

    CString str_s4;
    str_s4=AMyClass::getStringfromText (_T("MyApps.ini"),4);
    AfxMessageBox(str_s4);
Posted
Updated 28-Jun-18 20:13pm
v3
Comments
Richard MacCutchan 29-Jun-18 3:21am    
What error?

1 solution

You should use the debugger and step into the code. I guess from reading that the the file wasnt found in the directory and so the aLineArray is empty.

Good coding means also checking functions like the file.Open and some error handling:
C++
if( !file.Open(strExePath+filename, CFile::modeRead) ) {
  TRACE("File couldnt get opened");
  return "";
}
 
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