Solved

Reading a text file

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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 seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

776 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