CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 44

Threaded View

  1. #23
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,923

    Re: Looking for a Mentor

    However, it's giving me an error that says "Cannot find or open the PDB file".
    The PDB file (Program Database) holds debugging and project state information that allows incremental linking of a Debug configuration of your program. A PDB file is created when you build with /ZI or /Zi (for C/C++). See http://msdn.microsoft.com/en-us/libr...=vs.90%29.aspx

    This message is an issue with the way the project has been set up. Have a look at
    http://social.msdn.microsoft.com/For...?forum=vsdebug

    Re your program. main() should be defined as
    Code:
    int main()
    VEHICLES.txt needs to exist in the same folder as the program if you don't specify a folder location. A way to open a file and test if was opened properly is like this from my previous example

    Code:
    ifstream infile("VEHICLES.txt");
    
        if (infile.is_open() == false) {
    		cout << "The file VEHICLES.txt cannot be opened.\n";
    		return 1;
        }
    
         cout << "The file was successfully opened." << endl;
    
         while (infile >> vehicleClass >> licensePlate >> registrationFee)
         {
                //process data
         }
    When you define the file variable, you can specifiy the file name as well
    http://www.cplusplus.com/reference/f...ream/ifstream/
    Using .open() is OK but as you know the name of the file when you define the file variable IMO specifying the name then is easier than having a separate .open() statement.

    The class function .is_open() returns true if there is a valid file opened to the file variable. If the open failed it returns false. As the program is concerned with file processing, the rest of the program is invalid if the file hasn't been opened OK. Another way of coding this could be

    Code:
    ifstream infile("VEHICLES.txt");
    int retval = 0;
    
        if (infile.is_open() == true)
        {
              cout << "The file was successfully opened." << endl;
              while (infile >> vehicleClass >> licensePlate >> registrationFee)
              {
                //process data
              }
         } else {
             cout << "The file VEHICLES.txt cannot be opened.\n";
             retval = 1;
         }
         return retval;
    }
    This way there is only 1 entry and 1 exit point to the function - rather than 2 exits as in the previous example.

    stream extraction (infile >> vehicleClass ...) returns a NULL if there is a problem performing the extraction (see http://www.cplusplus.com/reference/i...perator%3E%3E/). Hence the while condition is true if the extraction is successfull and is false if there is an error with the extraction and the while loop then terminates. The extraction can fail for three reasons 1) end-of-file has been reached 2) the characters extracted could not be interpreted as a valid value of the appropriate type (trying to read an int and finds an 'A' 3) an error occured during the extraction and data couldn't be read. See http://www.cplusplus.com/reference/ios/ios/good/
    Last edited by 2kaud; January 16th, 2014 at 07:54 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured