Solved

Reading a text file

Posted on 2003-11-10
4
3,661 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
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 50 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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
This is about my first experience with programming Arduino.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
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 …

743 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now