0

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
}
codewario
  • 19,553
  • 20
  • 90
  • 159
  • 2
    The lines [`fflush(stdin);`](https://stackoverflow.com/questions/2979209/using-fflushstdin) and `printf("Last pos:%d",&t);` will invoke *undefined behavior*. – MikeCAT Jan 10 '21 at 03:18
  • 2
    `if (wfile = NULL)` ==> `if (wfile == NULL)` – WhozCraig Jan 10 '21 at 03:21
  • @MikeCAT `printf(“Last pod: %d”, &t);` isn’t necessarily undefined but it is almost certainly not what you want. The `&` should just be removed to print the value itself. – B. Morris Jan 10 '21 at 03:22
  • @MikeCAT okay, i deleted the 'fflush(stdin);' but now it is not scanning value 'another' – Özlem Karabulut Jan 10 '21 at 03:30
  • 1
    @B.Morris `printf("Last pos:%d",&t);` is undefined because data having wrong type is passed. (`int*` is passed where `int` is expected) `printf(“Last pod: %d”, &t);` has only little chance to cause undefined behavior because it will lead to compilation error due to non-standard quotation marks. – MikeCAT Jan 10 '21 at 03:34
  • @MikeCAT when I delete fflush() , it still doesn't write.When I delete fflush() and don't delete the printf("Last pos:%d",&t); it writes but this time,it owerwrites the file when I add new records.So, I deleted both of them and used fseek(),it worked this time but it kept adding the and of file without new line.Now I am back at where I started...I don't understand why – Özlem Karabulut Jan 10 '21 at 03:52

0 Answers0