writing to file

This is a discussion on writing to file within the C++ Programming forums, part of the General Programming Boards category; My program is suppose to search a file for a deleted record, and depending where/if it finds one, it writes ...

  1. #1
    Registered User Cpro's Avatar
    Join Date
    Oct 2006
    Posts
    149

    writing to file

    My program is suppose to search a file for a deleted record, and depending where/if it finds one, it writes output to the file. My code:
    Code:
    int main()
    {
    	fstream inFile;	
    	int choice = 0;
    	string blank;
    	
    
    	cout << "Enter your choice: " << endl;
    	cout << "1) Append a record " << endl;
    	cout << "2) Delete a record " << endl;
    	cout << "3) Display all records " << endl;
    	cout << "Enter 1 2 or 3 " << endl;
    	cin >> choice;
    	cout << endl;
    
    	Person person1;
    
    	//Append a record.
    	if(choice == 1)
    	{
    		char status = '!';
    		inFile.open("DeleteAppend.txt");
    				
    		//Find deleted record.
    		while(status != '*')
    		{
    			inFile >> status;
    			if(inFile.fail()) break;
    
                            //skip line if record is not deleted
    			if(status != '*')
    			{
    				getline(inFile, blank);				
    			}			
    		}
    	
    		inFile.close();	
    		
    		inFile.open("DeleteAppend.txt", ios::app);
    		inFile << "Test";
            }
    }
    My input/ouput file.
    Code:
    ! Will       Smith      25 Place        Tulsa           OK 38135     
    ! Corey      Black      Elm Street      Dallas          TX 89148     
    * Tanner     Hall       Mount View      Denver          CO 52742     
    ! Matthew    Robins     Parkway         Chicago         IL 91234
    The "*" signifies a deleted record. So, when my program finds this, it will insert the new record here. This currently works. I did simplify it here (so it's easier to read), so it just appends to the end of the file. The problem is, if there is no deleted record (no "*" in the file), no output will be written. So, if there is a deleted record, it will append. If there is not a deleted record, nothing happens. With this code, "Test" should be appended in both cases. The only difference i see between finding a deleted record or not is inFile.fail(). Maybe this has something to do with it?
    (I can post the whole program if needed.)

    Thanks.
    IDE - Visual Studio 2005
    Windows XP Pro

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >The only difference i see between finding a deleted record or not is inFile.fail(). Maybe this has something to do with it?
    It could be. Closing and reopening inFile is supposed to clear the flags, but it's possible this isn't happening. Try adding an inFile.clear(); within the if (inFile.fail) statement, and see if this fixes the problem.

  3. #3
    Registered User Cpro's Avatar
    Join Date
    Oct 2006
    Posts
    149
    That fixed it. Thanks.
    IDE - Visual Studio 2005
    Windows XP Pro

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 01:44 PM
  2. File Writing Problem
    By polskash in forum C Programming
    Replies: 3
    Last Post: 02-13-2009, 10:47 AM
  3. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 04:53 AM
  4. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 01:13 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21