Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CString and CFile C++

Status
Not open for further replies.

One2b

Programmer
May 29, 2003
14
US
I am trying to read in a text file that contains information in list form. Basically output from a lisp program that contains data. I need to ignore comments which in this case are ;;. I am trying to create a class which is derived from the CString class that when is used to create objects creates a string containing everything in the text file (except the comments of course). At this point I am just stuck on how to setup the class because I am not used to inheritance in C++. I have some stuff started like my constructor but get stuck on what it should do. I am going to need to keep track of parenthesis also which i can figure out later. Right now I just want to be able to create this object and know it contains eveything from the text file. I have been trying to do it using CFile, and look to others for assitance. C++ is new to me and I am just trying to get a grip on ideas such as Inheritance and setting up classes that can be used to create more complex objects later. Also, do I create this derived class as .h or a .cpp file? Thanks
 
Anyone know anything bout these two classes?
 
I wouldn't derive from CString. I would probably set up a new class that uses CString for its internal storage.

Maybe like:

[tt]class CLispParser
{
CString m_strFileContents;

public:
CLispParser(LPCTSTR lpszFileName);
// other parsing methods.
}

////////////////////

CLispParser::CLispParser(LPCTSTR lpszFileName)
{
CStdioFile file(lpszFileName,CFile::read|CFile::shareDenyNone|CFile::typeText);
CString strLine;
int i;
while (file.ReadString(strLine)) {
for (int i=0; i<strLine.GetLength(); i++) {
if (strLine == ';' && strLine[i+1] == ';') {
strLine = strLine.Left(i);
break;
}
}
}
}[/tt]

Another note: You may notice that I used CStdioFile instead of CFile (its base class.) CStdioFile lets you read the text one line at a time... as you would with the C runtime library's IO functions (hence the name CStdioFile).

[sub]I REALLY hope that helps.[/sub]
Will
 
thx...im gonna look it over and see what i can do....what .h file do i need to include?
 
If you did your project with the MFC AppWizard, there is no need to include anything. Otherwise #include <afxwin.h> and under Project Settings make sure you link to Microsoft Foundation Class library.

Oh and by the way... sorry, I messed up on the previous code. The line

Code:
if (strLine == ';' && strLine[i+1] == ';') {

should read

Code:
if (strLine
Code:
[i]
Code:
 == ';' && strLine[i+1] == ';') {

[sub]I REALLY hope that helps.[/sub]
Will
 
Ya...ill have to take a look at the code when i get to work on monday, but that looks like it will be helpful. Thx :)
Im sure ill have some more questions
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top