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!
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!