I am new to the C and writing a code which I'll be taking flight data and simply write it to a text file.
I debugged my code,and saw the values are saved to Flight array and when I open the file at r+
mode, it writes the data and moves the pointer perfectly. But in a weird way, my fseek()
functions doesn't work in my code, especially in the "w" or "w+" mode. I looked at the position of my pointer both in debugging window and with ftell()
function, the position doesn't change. It is the same way with my other writing functions as well.
In the while block where I print my values with printf()
, I used ftell
to use in fseek()
. Before that, I tried to use *fseek(wfile,0,SEEK_END)*
. But my pointer still stands at the beginning and doesn't move, also printf
doesn't work.
Here is my code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <Windows.h>
#define MAX_CAPACITY 20
#define NEW_FLIGHT { "", "","","","","",MAX_CAPACITY,{'0'}}
typedef struct Flight
{
char airlineCompany[31];
char flightNo[11]; //10 digit flight code
char departureAirport[51];
char arrivalAirport[51];
char departureTime[6];
char arrivalTime[6];
int passengerCapacity;
int passengers[MAX_CAPACITY]; //this array holds the value of 1/0:if seat is taken 1,else 0 .
} Flight;
void addFlight()
{
system("cls");
char another='y'; //variable to add another record
int recordnumber=0;
Flight flightarr[20]; //assuming 20 new record is written at a time,this is the flight array
struct Flight flight=NEW_FLIGHT; //flight record
FILE* wfile;//Writing a .txt database for flights
wfile = fopen("flights.txt","r+"); //if cannot open the file...
if(wfile == NULL)
{
wfile = fopen("flights.txt","w+"); //create one
if(wfile= NULL)
{
printf("File not found !\n");
return;
}
}
int pos=ftell(wfile);
printf("First pos:%d",&pos);
while(another == 'y')
{
printf("Enter 10 Digit Flight Number:");
scanf("%s",flight.flightNo);
//printf("%s",flight.flightNo);
printf("Airline Company:");
// getchar();
// fgets(flight.flightNo,30,stdin);
scanf("%s",flight.airlineCompany);
//printf("%s",flight.airlineCompany);
printf("Departure Airport:");
scanf("%s",flight.departureAirport);
//printf("%s",flight.departureAirport);
printf("Arrival Airport:");
scanf("%s",flight.arrivalAirport);
//printf("%s",flight.arrivalAirport);
printf("Departure Time:");
scanf("%s",flight.departureTime);
//printf("%s",flight.departureTime);
printf("Arrival Time:");
scanf("%s",flight.arrivalTime);
//printf("%s",flight.arrivalTime);
printf("Passenger Capacity:");
scanf("%d",&flight.passengerCapacity);
//printf("%d",&flight.passengerCapacity);
//assign the values to the flight array----------------
strcpy(flightarr[recordnumber].flightNo,flight.flightNo);
strcpy(flightarr[recordnumber].airlineCompany,flight.airlineCompany);
strcpy(flightarr[recordnumber].departureAirport,flight.departureAirport);
strcpy(flightarr[recordnumber].arrivalAirport,flight.arrivalAirport);
strcpy(flightarr[recordnumber].departureTime,flight.departureTime);
strcpy( flightarr[recordnumber].arrivalTime,flight.arrivalTime);
flightarr[recordnumber].passengerCapacity=flight.passengerCapacity;
recordnumber++;
//------------------------------------------------------
printf("\n Add another record? (Y/N)");
fflush(stdin);
another=getchar();
}
int i=0;
while(i<recordnumber) //print to the file from array
{
fprintf(wfile, "%s %s %s %s %s %s %d \n", flightarr[i].flightNo,flightarr[i].airlineCompany,flightarr[i].departureAirport,flightarr[i].arrivalAirport,flightarr[i].departureTime,flightarr[i].arrivalTime,flightarr[i].passengerCapacity);
int t = ftell(wfile);
printf("Last pos:%d",&t);
i++;
}
fclose(wfile); //close the file
}