Filestream Data Checking

This is a discussion on Filestream Data Checking within the C++ Programming forums, part of the General Programming Boards category; Hi All, Back again. This time my problem lies in checking whether the end user has requested the proper file ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    47

    Filestream Data Checking

    Hi All,

    Back again. This time my problem lies in checking whether the end user has requested the proper file for data processing. In other words, the program prompts the end user for a file name. If the file name is correct then the program will proceed and cout the entire file; if file name is not correct program re-prompts end user for file name. Here's what I came up with that doesn't work:

    Code:
    while (loop > 1)
    {
           cout << endl << endl << " Please enter the name of file: ";
           cin >> fileName;
           inData.open(fileName.c_str());
    			
           // Get Data 
    
           inData >> inventorFile;
    
           if (inventorFile != " ")
     	cout << endl << inventorFile << endl; 
    	loop++;
           else
     	loop = 0;			
    }
    Any direction would be appreciated

    bob2509

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    47
    I have modified the loop to the following, which still does not work:

    Code:
    while (loop < 1)
    {
         cout << endl << endl << " Please enter the name of file: ";
         cin >> fileName;
         inData.open(fileName.c_str());
    			
         // Get Data 
    
         inData >> inventorFile;
    
         if (!inData)
        {
    	cout << endl << inventorFile << endl; 
    	loop++;
         }
         else
    	loop = 0;
    }

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    47
    Sorry, but modified again soon:

    Code:
    while (loop < 1)
    {
         cout << endl << endl << " Please enter the name of file: ";
         cin >> fileName;
         inData.open(fileName.c_str());
    			
         // Get Data 
    
         if (!inData)
              loop = 0;              
         else
         {
               inData >> inventorFile;
               cout << endl << inventorFile << endl; 
               loop++;
          }
    }

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    47
    Nevermind, got it to work --

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    You may consider throwing in a cin.clear(); statement to clear error flags in the event that open() fails.

    Code:
    while (loop < 1)
    {
         cout << endl << endl << " Please enter the name of file: ";
         cin >> fileName;
         inData.open(fileName.c_str());
    			
         // Get Data 
    
         if (!inData)
         {
              cin.clear();  // clear error flags
              loop = 0;
         }              
         else
         {
               inData >> inventorFile;
               cout << endl << inventorFile << endl; 
               loop++;
          }
    }
    May not be an issue, but, on the other hand, you could find that upon re-prompting and inputting a valid file name, you get prompted again.

    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Lame null append cause buffer to crash
    By cmoo in forum C Programming
    Replies: 8
    Last Post: 12-29-2008, 02:27 AM
  2. data structure design for data aggregation
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 05-20-2008, 06:43 AM
  3. Replies: 3
    Last Post: 04-18-2008, 10:06 AM
  4. C diamonds and perls :)
    By Carlos in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 05-16-2003, 10:19 PM
  5. C Programming Question
    By TK in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 07-04-2002, 07:11 PM

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