Thread: Repetitive Bug with C++ Program - Needs Help

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348

    Repetitive Bug with C++ Program - Needs Help

    Hello!
    I have been working a small program as a way to practice C++. Here is a quick sequence of how the program works and does:

    - do-loop menu (2 choices: 1) read/write 2) exit)
    - switch
    case 1: read/write function
    case 2: return 0;
    - read/write function
    1)enter name of read file
    2)enter name of write file
    3)fstream.open(read.....)
    4)fstream.open(write...)
    5)read.getline(temp, 50);
    6)write << read // copy everything from data that were saved in memory from read file to write file

    For some reason, the work only works: the first time it starts AND the user must enter the correct file name (file exists)

    Case: If user enters the name of a file (always stalls upon read file, not write file) that exists the first time the program starts, it works. However, it works only once. Once the user returns to the mean menu and tries the same sequence, the program would say the "file" does not exists even if the user enters the exact same file name. I have tried cout the array used to store the file name. It holds the correct file name each time.

    Case: If user enters a file name that does not exists, the program would not work, period.

    Does anyone know what is going on? I have tried using dynamically allocated memory to make sure the file name is stored clean each time. I have tried to use different technique like trying different loops and if/else. Nothing has worked. Is there something static about the fstream?

    Thanks,
    Kuphryn

  2. #2
    Registered User kitten's Avatar
    Join Date
    Aug 2001
    Posts
    109
    Do you close the file after you've finisher reading/writing it? The OS won't open same file twice a row.
    Making error is human, but for messing things thoroughly it takes a computer

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Yes. The program closes both input file and output file:

    input.eof() <--- I use that member function as a sign to close files.

    The only thing I could think of is that fstream is declared in main(), however, the actually openning of the file is done in a class. I pass fstream from main() to a class member function which opens it as well as check end-of-file. Lastly, I use try/catch algorithms to check file.fail(). I do not know if that has anything to do with the repetitive program bug. I have tried disabling try/catch, but I saw the same response, so i enabled it. I believe either something is not right with the dynamically allocated memory, pointers or loops. I have no idea what. If anyone wants me to post a part of the code, please request. Please include exactly what you want to see.

    Thanks,
    Kuphryn

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Here is the section that asks the user to enter the name of the read file and the write file. Notice that the read file and the write file are in two seperate sections.

    Code:
    // Definition of function filterIP
    void filterIP(bool &veri1, fstream &source, fstream &output, AddIP &aIP, DNS &dns)
    {
    	char opt;
    	do
    	{
    		char *SFile = NULL;
    		SFile = new char[15];
    		veri1 = true;                          
    		cout << "\nEnter source file: ";
            cin >> SFile;
            cin.ignore();
    		try
    		{
    			aIP.getSFile(source, SFile);
    		}
    		catch(AddIP::NoFile)
    		{
                cout << "\nERROR: File \"" << SFile << "\" not found!\n"
    		         << "\nPress (C)ontinue or (E)xit: ";
    		    cin >> opt;
    		    cin.ignore();
    		    while (toupper(opt) != 'C' && toupper(opt) != 'E')
    		    {
    			    cout << "\nInvalid choice. Press (C)ontinue or (E)xit: ";
    			    cin >> opt;
    			    cin.ignore();
    		    }
                if (toupper(opt) == 'E')
    		        exit(0);
    	    	else
                    veri1 = false;
            }		
    		delete [] SFile;
    	}while(!veri1);
    	do
    	{
    		char *OFile = NULL;
    		OFile = new char[15];
    		veri1 = true;
    		cout << "\nEnter output file: ";
            cin >> OFile;
            cin.ignore();
            try
    		{
    			aIP.getOFile(output, OFile);
    		}
    		catch(AddIP::NoFile)
    		{
                cout << "\nERROR: File \"" << OFile << "\" not found!\n"
    			     << "Do you want to create an output file? (Y/N): ";
    		    cin >> opt;
    		    cin.ignore();
    		    while (toupper(opt) != 'N' || toupper(opt) != 'Y')
                {
    		        cout << "\nInvalid choice. Press (C)ontinue or (E)xit: ";
    			    cin >> opt;
    			    cin.ignore();
    		    }
                if (toupper(opt) == 'Y')
    		    {
    			    output.open(OFile, ios::out | ios::app);
    			    break;
    		    }								  
    		    else
    		        veri1 = false;
            }		
    		delete [] OFile;
    	}while(!veri1);
    	veri1 = false;
    	cout << "\nScanning source file...\n"
             << "Writing IP to output file...\n\n";
        aIP.seekIP(source, output);
    	cout << "\nProgram has reached the end of source file. Press any key to"
             << " continue";
    	cin.get();
    	system("cls");
    }
    Here are the sections that opens the read file and write file:

    Code:
    void AddIP::getSFile(fstream &source, char *sFile)
    {
    	source.open(sFile, ios::in);
    	if (source.fail())
    		throw NoFile();
    }
    
    void AddIP::getOFile(fstream &output, char *oFile)
    {
        output.open(oFile, ios::out | ios::app);
    	if (output.fail())
    		throw NoFile();
    }
    Do you see anything that could cause the repetive "file does not exists" problem even if the user enter a file name that does exist? Remember, the program works perfect when: 1)program first starts AND 2)user enters correct file name

    Otherwise, the program either says that the files not exist. I have tried cout the file name right before "source.open" and "output.open." The stored value are valid. Something is keeping it from openning. Maybe "source.fail()" and "output.fail()" do not work right.

    Kuphryn

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    412
    Maybe I'm missing it, but where do you do

    source.close();

    and

    output.close();

    ?

    If you're not doing this, that's your problem.

  6. #6
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    The V:

    source.close();
    output.close();

    were both called inside a class member function. main() passes source and output to that class member function later down the code.

    ---------------------------------------------------------------------------------

    Great news. I figured out the problem. What I am about to say is based on personal experience. Whether it is true for everything is arbitrary.

    => passing fstream to a class member function *while in a loop* (do-while, while-loop and for-loop) could potentially produce problems.

    From my experience a variable must be "refresh," preferably at the beginning of the loop, if you want the variable to have *the same logic* the second reiteration and beyond.

    Example:

    int num = 0;

    do
    {
    cout
    << "1) Yes\n"
    << "2) No\n"
    << "3) Exit"\n\n";
    cin >> num;
    }while(num < 1 || num > 2)

    In the example above, the program will work the first time around, but it will act weird the second time around. For example, let say the above do-while loop is in another loop and the loop above is just to ensure the user enters a valid option. Let say the user enters "1," the program outs "Okay" and then returns to the loop above since the loop above is inside another loop. When the program returns the second time around, "num" would still hold the value "1" and the program would open "Okay" no matter what the user enters.

    The statement above is probably due to me not using some type of ignore command. Nonetheless, it was something I noticed.

    The way I would get around something like that is I would initialize "num" first in the loop. That was how I fix the fstream problem. Rather than declaring fstream in main(), now I declear:

    fstream source, output;

    right in the function where I use the do-loop. I do not like the way it is right now. For every function that uses the fstream as well as class member function with fstream as one of its parameters, I have declares instances of fstream as instances of the class whose member function use called with fstream parameter. Nonetheless, it does undoubtedly fix a major bug in the program.

    Maybe someone have a slick way to get around that. Remember, the problem is occurs only if there is some type of loop.

    Kuphryn

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 10:58 PM
  2. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  3. my server program auto shut down
    By hanhao in forum Networking/Device Communication
    Replies: 1
    Last Post: 03-13-2004, 10:49 PM
  4. Bug in Program - please help!
    By muffin in forum C Programming
    Replies: 4
    Last Post: 08-31-2001, 09:33 AM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM