Click to See Complete Forum and Search --> : Reading Text Files


Doug Greene
June 2nd, 1999, 12:33 AM
Hi, I'm new to Visual C++, and I'm trying to learn to work with text files. I'm doing pretty good so far with the help of the MSDN Library. In my program that I'm making just to help me learn about reading and writing to text files, I've run into a problem. The program is supposed to read a text file line by line, and make changes to it based on what's there. I also need the program to skip to the next line when the two characters '//' are encountered. Here's what I've got so far:

void CParseDataApp::OnOpenText()
{
FILE *textFile;
char getText[100];
char firstTwo[3];
CString fileName = "C:\\Doug\\Programs\\R6\\test.txt";
textFile = fopen(fileName,"r+");
if (textFile != NULL)
{
int counter = 0;
while (counter != 4)
{
fgets(firstTwo,3,textFile);
if (firstTwo != "//")
{
fgets(getText,99,textFile);
MessageBox(NULL,firstTwo,"Result",MB_ICONASTERISK);
counter++;
} else {
MessageBox(NULL,firstTwo,"Comment",MB_ICONSTOP);
counter++;
}
}
} else {
MessageBox(NULL,"The text file "+fileName+"\ncould not be opened.","Error",MB_ICONSTOP);
}
}



the text file looks like this:

//
**
^^
hey hey

Ok, now here's the problem. When the program is run, it doesn't recognise the first line '//' as a comment. It's like the statement

if (firstTwo != "//")



doesn't work. Is it because the variable firstTwo is a char? When run, I get four MessageBoxes, each one having an asterisk as an icon, each one from the 'if' bracket of the statement. But yet the first line of the text file is "//". The messageBox even tells me it's "//". Yet it doesn't execute the 'else' bracket of the statement. Does anyone know why not? This gets really frustrating when everything looks right, but it doesn't work. If anyone has a guess as to why this doesn't work, please let me know. Any help is greatly appreciated. Thanks!

Chao
June 2nd, 1999, 01:07 AM
Hi,

The variable first two is a pointer to a character, and you tried to compare it to a string in the if statement. Try modifying your code to:

void CParseDataApp::OnOpenText()
{
FILE *textFile;
char getText[100];
char firstTwo[3];
CString fileName = "C:\\Doug\\Programs\\R6\\test.txt";
textFile = fopen(fileName,"r+");
if (textFile != NULL)
{
int counter = 0;
while (counter != 4)
{
fgets(firstTwo,3,textFile);
if (firstTwo[0] != '/' && firstTwo[1]!='/')
{
fgets(getText,99,textFile);
MessageBox(NULL,firstTwo,"Result",MB_ICONASTERISK);
counter++;
} else {
MessageBox(NULL,firstTwo,"Comment",MB_ICONSTOP);
counter++;
}
}
} else {
MessageBox(NULL,"The text file "+fileName+"\ncould not be opened.","Error",MB_ICONSTOP);
}
}

Hope this is helpful

Chao

Franky Braem
June 2nd, 1999, 02:55 AM
use the strcmp function to compare char-pointers.

Your program doesn't work if the '//' are not at the beginning of a line. You can read a line and then use strstr to find the substring '//'.

Another tip : see to the getline function. It's a function defined in STL. You don't have to worry anymore about the linelength. STL will do it for you. You can also use the string-implementation of STL.Of course this means you have to dig in STL and that's not always easy. A very good startpoint for this is reading the chapters of Thinking In C++, which you can find here at Codeguru.