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?

  • one
    AND? The error crashes in code that has nothing to do with your fscanf . Obviously pointer has a zero value. Why did you bring this stranger code snippet with fscanf ? And why the code which really concerns the given problem is not given? - AnT
  • Direct relationship, because through it I read the data. I cannot solve the problem in order to read the data and not catch the error. Offer another solution if you cannot answer why fscanf leads to an error. - rapid88
  • Where is the rest of the code? Where at least declarations of variables? Where does your code fly out? - AnT
  • Made addition code. Departure occurs inside while, where fscanf. At what he reads all the data from the file, and then, when, in theory, should have returned EOF, it simply breaks the program with the error indicated above. - rapid88

1 answer 1

What is it

 fscanf(inputFile, "%d%d%d%d\n", &orderNumber, &type, &passengers); 

?

Four %d , but only three arguments. It is no wonder that fscanf will fall unpredictably.

There is also no point in specifying \n in the fscanf format. The %d specifier already skips whitespace.

  • Exactly, thanks a lot. A little inattentive after a few hours :) - rapid88