Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Reading a text file

Posted on 2003-11-10
4
Medium Priority
?
3,715 Views
Last Modified: 2008-02-01
I am vc++ beginner .  please help me reading text file line by line from an MFC application
0
Comment
Question by:Gopakumar
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 11

Expert Comment

by:KurtVon
ID: 9715270
The simple method is ot open the file, read the lines (processing them however you want) and close the file.  Of course, there is a bunch of error-checking to do in C.  It would also help if you were a little more clear on exactly what you are doing. Is this just opening and reading a text file, or do you mean that you want the document object to be a text file read line-by-line?

Here's the code for doing it the simple way:

// Open the file for reading
FILE* file = fopen(strFilename, "r");
// make sure the open was successful
if (file != NULL)
{
    // Space for each line
    char nextline[MAX_LINE_LENGTH];

    // Read each line
    while (fgets(nextline, MAX_LINE_LENGTH, file) != NULL)
    {
        // Process the string in nextline, watching out for a null character.
        // If there is a null character in the file, it will appear in the middle
        // of the string, so be sure not to make assumptions about the line
        // length.
    }

    // Close the file
    fclose(file);
}
else
{
    // Do whatever you need to to let the user know the file didn't open
}

Hope that helps.
0
 
LVL 14

Accepted Solution

by:
Thandava Vallepalli earned 150 total points
ID: 9715314
Hi,

here an example source of reading information from a file (m_cSourceFile) and writing data into created new file (m_cDestFile). CStdioFile object has been used here.

CStdioFile m_cSourceFile;
CStdioFile m_cDestFile;


int CImportParam::ReadFile()
{
     CString sReadLine;
     int iLineNumber=1;
     CFileException* pError=NULL;
     if (m_cSourceFile.Open(m_sImportFileName,CFile::modeRead,pError) == 0)
     {
          return -1;
     }
     m_cSourceFile.SeekToBegin();
     while (m_cSourceFile.ReadString(sReadLine) == 1)
     {

          ... Do something with the retrived line (sReadLine) ....

          iLineNumber++;
     }
     m_cSourceFile.Close();
     return 2;
}

int CImportParam::CreateOutputFile()
{
     int iLineNumber=1;
     CFileException* pError=NULL;
     CString sReadLine;
     CString sWriteLine;
     int iFindChar;
     if (m_cSourceFile.Open(m_sImportFileName,CFile::modeRead,pError) == 0)
     {
          return -1;
     }
     m_cSourceFile.SeekToBegin();
     // Creates an empty file. if the file exist delete it.
     if (m_cDestFile.Open(m_sOutputPath,CFile::modeWrite | CFile::modeCreate,pError) == 0)
     {
          return -1;
     }
     while (m_cSourceFile.ReadString(sReadLine) == 1)
     {
          sWriteLine="";

          ... Do something...
          sWriteLine=sReadLine;
          m_cDestFile.WriteString(sWriteLine);
          iLineNumber++;
     }
     m_cSourceFile.Close();
     m_cDestFile.Close();
     return 1;
}

V.T.Krishna.
0
 
LVL 2

Expert Comment

by:venkaiah
ID: 10380195
Hi,

                CFile            File;
      CString            str;
      char            *pBuffer;
      CFileDialog OpenDlg(TRUE);
      if(OpenDlg.DoModal()!=IDOK)
            return;
      File.Open(OpenDlg.GetFileName(),CFile::modeRead);
      pBuffer=new char[File.GetLength()];
      File.Read(pBuffer,File.GetLength());
      pBuffer[File.GetLength()]='\0';
      str=pBuffer;
The above code is enough for reading the data from any type of file.
0
 
LVL 2

Expert Comment

by:jonnykeogh
ID: 10397550
KurtVon - Thank you SO much!! That really helped me finish my program!!

(I use Borland C++)

Jonny K
(I may be a kid - but I'm a mighty programmer!!)
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question