There is the following code:
#include <stdio.h> #include <conio.h> #include <stdlib.h> #include <string.h> #define BICYCLE 1 #define MOTORCYCLE 2 #define CAR 4 #define TRUCK 8 typedef struct { int orderNumber, type, passengers, cost; } TRANSPORT; void printAllTransports(TRANSPORT*); int countOfTransportByType(TRANSPORT*, const int); int getIncome(TRANSPORT*); void shellSort(TRANSPORT* transports); void swap(TRANSPORT*, TRANSPORT*); int main(int argc, char* argv[]) { char path[100]; int orderNumber, type, passengers, count = 0, i; FILE* inputFile; TRANSPORT* transports; if (argc < 2) { printf("Enter, please, file path.\n"); scanf_s("%s", path); if ((inputFile = fopen(path, "r+")) == NULL) { printf("ERROR: file isn't opened.\n"); _getch(); return -1; } free(path); } else { if ((inputFile = fopen(argv[1], "r+")) == NULL) { printf("ERROR: file isn't opened.\n"); _getch(); return -1; } } while ((i = fscanf(inputFile, "%d%d%d", &orderNumber, &type, &passengers)) != EOF) { if (type != BICYCLE && type != MOTORCYCLE && type != CAR && type != TRUCK) { printf("INPUT DATA ERROR: wrong type of transport.\n"); _getch(); return -1; } printf("while: order: %d, type: %d, passengers: %d\n", orderNumber, type, passengers); count++; } rewind(inputFile); transports = (TRANSPORT*) malloc(sizeof(int)*4*count); for (i = 0; i < count; i++) { fscanf(inputFile, "%d%d%d%d\n", &orderNumber, &type, &passengers); transports[i].orderNumber = orderNumber; transports[i].passengers = passengers; transports[i].type = type; printf("for: order: %d, type: %d, passengers: %d\n", orderNumber, type, passengers); switch (type) { case BICYCLE: transports[i].cost = 1; break; case MOTORCYCLE: transports[i].cost = 2; break; case CAR: transports[i].cost = 5; break; case TRUCK: transports[i].cost = 10; break; } } printAllTransports(transports); printf("Shell sorting transports by passengers...\n\n"); shellSort(transports); printAllTransports(transports); printf("Number of bicycles: %d\n", countOfTransportByType(transports, BICYCLE)); printf("Number of motorcycles: %d\n", countOfTransportByType(transports, MOTORCYCLE)); printf("Number of cars: %d\n", countOfTransportByType(transports, CAR)); printf("Number of trucks: %d\n", countOfTransportByType(transports, TRUCK)); printf("\n\n"); printf("Money incoming: %d\n", getIncome(transports)); _getch(); return 0; } And here inside while some kind of magic is unclear to me, because of which the program does not want to function. There is the following file:
1 1 2 2 1 2 3 1 2 4 2 4 5 2 2 6 4 4 7 4 6 8 8 10 9 8 16 The program successfully reads all the data. Through printf all data is successfully output to the console, BUT after the data is output, fscanf gives an error:
Unhandled exception at 0x0f88e42e (msvcr100d.dll) in Usachev.exe: 0xC0000005: Access violation writing location 0x00000000. And points to the input.c file, the following two lines:
if (longone) *(long UNALIGNED *)pointer = (unsigned long)number; And some more information:
I tried to use instead of checking! = EOF just == 3, however this error is still there.
If you remove the check for anything at all and leave just fscanf in while, then there will be no error, but the cycle will become infinite.
Can someone explain how to solve this problem and make the data read successfully?
fscanf. Obviouslypointerhas a zero value. Why did you bring this stranger code snippet withfscanf? And why the code which really concerns the given problem is not given? - AnT