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:
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;
}
}
}