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!

Need help with a function....

Status
Not open for further replies.

eyesrglazed

Programmer
Jul 14, 2005
20
US
This function is supposed to read from an HTML file saved as "temp.txt"... Unfortunately, it doesn't work more than once running in a loop. I'm not very good at troubleshooting, but it looks like it isn't reading from the beginning of the fiel the second, etc, times... Can someone have a look?

Code:
void Search()
{
	fnd = 0;
	oFile << fileName1 << fileName2 << endl;
	iTemp.open("temp.txt");
	iTemp >> temp;
	while (temp != "")
	{
		if (temp == srch1)
		{
			oFile << "Members : " << prev << endl;
			cout << "Found Members." << endl;
			fnd++;
		}
		if (temp == srch2)
		{
			oFile << "Guests : " << prev << endl;
			cout << "Found Guests." << endl;
			fnd++;
		}
		prev = temp;
		iTemp >> temp;
	}
	iTemp.close();
}
 
Since there aren't any types declares anywhere, I'm guessing you're using global variables galore.

These are the types I'm assuming based on their usage:

fnd = int
oFile = ofstream
iTemp = ifstream
temp, prev, srch1 & srch2 = string

If those assumptions are correct, you could try this after you open the file:
Code:
iTemp.seekg( 0 );
 
Sorry about the globals, but you got them right.

I tryed the "iTemp.seekg( 0 );", but it still doesn't seem to be working...

Is there a specific place to put the piece of code?

And, I was also wondering, is the code decent to do what it's supposed to do?
 
The first thing I'd do is get rid of the global variables:
Code:
void Search()
{
    int fnd = 0;  // Nothing here seems to be using fnd.
    ofstream oFile( "output.txt" );
    oFile << fileName1 << fileName2 << endl;

    ifstream iTemp( "temp.txt" );

    while ( iTemp >> temp )
    {
        if ( temp == srch1 )
        {
            oFile << "Members : " << prev << endl;
            cout << "Found Members." << endl;
            ++fnd;
        }
        else if ( temp == srch2 )
        {
            oFile << "Guests : " << prev << endl;
            cout << "Found Guests." << endl;
            ++fnd;
        }
        else
        {
            // What if temp != srch1 or srch2?
        }

        prev = temp;
        iTemp >> temp;
    }
}
 
I thought it would help you, even if it is not exactly what you intended in your cpp source.

Code:
// console_060106_6pm.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <stdio.h>
#include <tchar.h>
#include <stdlib.h>
#include <fstream.h>

int _tmain(int argc, _TCHAR* argv[])
{
	char str[200] ;
	ifstream in("fileI.html") ;
	if(in.fail())
	{
		perror(str) ;
		cout <<"\nin->error:" << str ;
		return 0 ;
	}//if
	ofstream out("fileO.txt") ;
	if(out.fail())
	{
		perror(str) ;
		cout <<"\nout->error:"<< str ;
		return 0 ;
	}//if

	while(in >> str)
	{
		if(strcmp(str, "<member")==0)	
		{
			in >> str ; cout << "\n " << str ; //member name from html
			out << str << "  " ;
			in >> str ; cout << str ; //member title 
			out << str << endl ;
		}//if
		else if(strcmp(str, "<guest")==0)
		{
			in >> str ; cout << "\n " << str ; //guest name from html
			out << str << "  ";
			in >> str ; cout << str ; //guest title 
			out << str << endl ;
		}//else if
	}


	//close streams
	in.close() ;
	out.close() ;	
	return 0;
}

//fileI.html
/*
<html>
<head>
<title>c++ ideas</title>
</head>
<font size=-1><a href="../../index.html">c++ contents</a></font>
<HR SIZE=3 NOSHADE>
<!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<center>
           <h1>HTML BAD IDEA </h1>
</center>
<p>
<font size=+2><b>mihk C++ Reference</b></font><br>
<font size=-1>(mihk folders)</font>
<ul>
   <LI><b><a href="../../my/badideas.html">badideas.html</a></b>
</ul>
</BODY>
</HTML>
<member member1=alice title=Mrs. >
<member member2=claude title=Mr. >
<guest  guest1=mates title=Mr. >
*/
 
Yeah, you see.... I have several problems when it comes to this stuff. I'm a 16 year old guy who's writing this program for my mom, who in turn will use it for her business, and I'll get a cut. Therefore, I supposedly can't post the entire code on this website. Also, my knowledge of C++ (which allowed me to write the function above) has come from two of my mom's college books on "problem solving with C++".

Therein, I apologise for not being able to understand half of the code previously listed.

But back to the problem at hand.

I tried using cpjust's form of the function, and changed some of the ifstream and ofstream stuff in the rest of the code around, but I'm still getting the same problem...

If needed, though, I will post the full code up here
 
Have you tried stepping through the program in the debugger to see exactly what code is being executed? Once it gets to the point where you see the problem, open the input file and see if it looks like it's supposed to. The only thing I can think of is that the input file isn't formatted properly the second time when it doesn't find the strings that you're looking for...
 
Nope, it can't be a problem with the input file, because I copied the same information to each line, since it already worked.

Also, I'm not sure if I'm thinking of the right program debugger, but this program runs. It just doesn't do what it's supposed to.
 
<Also, I'm not sure if I'm thinking of the right program debugger, but this program runs. It just doesn't do what it's supposed to.>

Welcome to programming...
 
I've made similar assumptions about things that I was SURE couldn't be the problem, but after many hours of trying other things, I found out I was wrong.

Since this is a Visual C++ forum, I'm assuming that's what you're using. In that case I'm talking about the debugger in Visual Studio. You can set a breakpoint on a line of code where you want it to pause, then you can run one line at a time and watch the values of the variables change after each line.
To set a breakpoint, put the cursor on the line you want to stop at, then press F9 (press F9 again if you want to remove it).
To start debugging, press F5. Then to step to the next line of code, press F10. To step into a function (rather than running the whole function) press F11. If you want to step out of a function, press Shift+F11.
To stop debugging, press Shift+F5.
While you're debugging, the window at the bottom of the screen should show all the variables you've declared and what their values are (or you can hold your mouse over the variable name to see its value).

If all else fails, you could switch from using the convenient fstream objects for your files to using the raw C functions like open(), read(), write(), seek(), close()... But you should spend some time in the debugger before you resort to the low level C functions.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top