Visual Studio error: Unable to start the program (Unable to start program)

0

I would like to know why, when you run the following code, there is a little window in which it says:

  

can not start the program

followed by:

  

can not find the specified file

It also gives these errors:

  

'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS.

     

'fprintf': unknown type field character '' in format specifier

The purpose of the program:

  • Open and read a .txt file
  • Sort the information in ascending order according to the airplane number.
  • Save the sorted data in a new file.
  • Search the data according to the type of aircraft.
  • Could you please help me, is there an error in the code?

    #include "stdafx.h"
    #include "iostream"
    #include "fstream"
    #include "conio.h"
    #define n 7
    
    using namespace std;
    
    struct AEROFLOT
    {
        char name[20];
        int number;
        char plane[10];
    } *pm;
    
    void Print(AEROFLOT*);
    void WriteFile(AEROFLOT*, char*);
    void Sort(AEROFLOT*);
    void InputType(AEROFLOT*);
    
    int main()
    {
        const char *fname = "C:\Users\User\Documents\aeroflot.txt";
    
        FILE* file = fopen(fname, "rb");
        if (file != NULL) {
            pm = new AEROFLOT[n];
            fread(pm, sizeof AEROFLOT, n, file);
            fclose(file);
        }
        else
        {
            cout << "Error opening file!" << endl;
            return 1;
        }
        cout << "List of airplanes: ";
        Print(pm);
        Sort(pm);
        cout << "Sorted list: ";
        Print(pm);
        WriteFile(pm, "C:\Users\User\Documents\aeroflot1.txt");
        InputType(pm);
        delete [] pm;
        _getch();
    }
    
    void Print(AEROFLOT array)
    {
        for (int i = 0; i < n; i++) 
        {
            cout << "Destination: " << array.name << endl;
            cout << "Number of airplane: " << array.number << endl;
            cout << "Type of airplane: " << array.plane << endl;
            cout << "-----------------------------------\n" << endl;
        }
    }
    
    void WriteFile(AEROFLOT* array, char* fname) 
    {
        FILE* file;
        file = fopen(fname, "wt");
        if (!file) cout << "Error opening file!" << endl;
    
        for (int i = 0; i < n; i++)
        {
            fprintf(file, "%10s -- %5 -- %10s\n", array[i].name, array[i].number, array[i].plane);
        }
        fclose(file);
    }
    
    void Sort(AEROFLOT* array)
    {
        int index;
        for (int i = 0; i < n; i++)
        {
            index = i;
            AEROFLOT tmp;
            for (int j = i + 1; j < n; j++)
            {
                if (array[index].number < array[j].number)
                    index = j;
            }
            if (index != i)
            {
                tmp = array[i];
                array[i] = array[index];
                array[index] = tmp;
            }
        }
    }
    
    void InputType(AEROFLOT* array)
    {
        char* type;
        cout << "\nEnter type of airplane: "; cin >> type; 
        cout << endl;
    
        for (int i = 0; i < n; i++)
        {
            if (type == array[i].plane)
            {
                cout << "Destination: " << array[i].name;
                cout << "Number of airplane: " << array[i].number << endl;
            }
    
            else
            {
                cout << "There isn't an airplane of this type." << endl;
            }
        }
    }
    
        
    asked by Neon 01.11.2016 в 15:05
    source

    2 answers

    1
      

    I would like to know why, when you run the following code, there is a little window in which it says:

         
        

    can not start the program

      
         

    followed by:

         
        

    can not find the specified file

      

    This error can occur because in the VS project configuration, the generation path of the executable and its execution path do not match.

    If you open the project properties panel :

    You can see a panel with several options whose title will be tu proyecto Property Pages .

    The options that you should review are:

    • Configuration Properties.
      • General.
      • Output Directory : path will be copied to the executable file that your application generates.
      • Debugging.
      • Command : order will be given to the operating system to run the program.
      • Working Directory : your program's execution path, that is: the path from which the program will see the files and folders.

    If you have not touched the highlighted options they should have the default values $(OutDir) , $(TargetPath) and $(ProjectDir) respectively and everything should work normally.

    If you have touched any of the options, they will be bold and if the routes are incorrect VS can not find the executable file, showing errors similar to those you describe.

    It could happen that you have not touched these options but that they are still incorrectly configured ... have you downloaded the project online or have you generated it with a tool like Make?

      

    It also gives these errors:

         
        

    'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS.

             

    'fprintf': unknown type field character '' in format specifier

      

    These errors are quite self-explanatory.

    The first error indicates that the fopen function is considered insecure and advises using fopen_s instead. fopen belongs to the C libraries adapted to C ++ (in this case it is part of the header <cstdio> ).

    Generally many of the functions of C libraries are considered insecure because they can not be used in concurrent environments , because they do not check for buffer overflow or invalid / null pointers or for all those reasons both; to try to solve these problems, secure functions are offered that usually have the suffix _s .

    The second error indicates that you have added a mark of unknown format in the function fprintf , the most common format marks you can see them here .

    Among the allowed formatting marks is not the %5 that you are using here:

    fprintf(file, "%10s -- %5 -- %10s\n", array[i].name, array[i].number, array[i].plane);
    

    What surely generates the error.

        
    answered by 02.11.2016 в 09:59
    0

    Because of the error "can not find the specified file" Are you sure the path is correct and have the necessary permissions?

    By this warning "'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS."

    Add as the first line of code this:

    #ifdef _WIN32 
    #define _CRT_SECURE_NO_DEPRECATE 
    #endif
    

    or use fopen_s

    For "'fprintf': unknown type field character '' in format specifier"

    note that the 2nd parameter of the fprint says% 5 you are missing the d = > % 5d

        
    answered by 01.11.2016 в 17:23