#include<stdio.h>
#include<stdlib.h>
int main()
{
  struct {
    char origin[50];
    char destination[50];
    char flight[10];
    char aircraft[3];
    char days_of[7];
    char departure[4];
    char arrival[4];
    char begin_date[10];
    char end_date[10];
  }flight_details[1679];
  FILE *fp;
  fp=fopen("iaschedule-07(edit).csv","r");
  char buf[50];
  int i;
  while( fgets(buf,sizeof(buf),fp) != NULL)
    {
      strcpy(flight_details[i].origin, strtok(NULL,","));
      strcpy(flight_details[i].destination, strtok(NULL,","));
      strcpy(flight_details[i].flight, strtok(NULL,","));
      strcpy(flight_details[i].aircraft, strtok(NULL,","));
      strcpy(flight_details[i].days_of, strtok(NULL,","));
      strcpy(flight_details[i].departure, strtok(NULL,","));
      strcpy(flight_details[i].arrival, strtok(NULL,","));
      strcpy(flight_details[i].begin_date, strtok(NULL,","));
      strcpy(flight_details[i].end_date, strtok(NULL,","));
      ++i;
    }
  printf("print details\n");
  for (int j=0; j<10; j++)
    {
      printf("Origin : %s", flight_details[j].origin);
      printf(", Destination : %s\n",flight_details[j].destination);
    }
  return 0;
]

I am new to C, I get a list of warnings and errors saying. one of the warnings is, incompatible implicit declaration of built-in function strcpy.

could someone please help me clean this up I think i have alt of syntax problems.

>one of the warnings is, incompatible implicit declaration of built-in function strcpy.
You need to include <string.h>.

This compiles, and I fixed a few obvious logic errors (like the first call to strtok needing the string you want to tokenize instead of NULL):

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
  struct {
    char origin[50];
    char destination[50];
    char flight[10];
    char aircraft[3];
    char days_of[7];
    char departure[4];
    char arrival[4];
    char begin_date[10];
    char end_date[10];
  }flight_details[1679];
  FILE *fp;
  char buf[50];
  int i = 0, j;

  fp=fopen("iaschedule-07(edit).csv","r");

  while( fgets(buf,sizeof(buf),fp) != NULL)
  {
    strcpy(flight_details[i].origin, strtok(buf,","));
    strcpy(flight_details[i].destination, strtok(NULL,","));
    strcpy(flight_details[i].flight, strtok(NULL,","));
    strcpy(flight_details[i].aircraft, strtok(NULL,","));
    strcpy(flight_details[i].days_of, strtok(NULL,","));
    strcpy(flight_details[i].departure, strtok(NULL,","));
    strcpy(flight_details[i].arrival, strtok(NULL,","));
    strcpy(flight_details[i].begin_date, strtok(NULL,","));
    strcpy(flight_details[i].end_date, strtok(NULL,","));
    ++i;
  }

  printf("print details\n");

  for (j=0; j<10; j++)
  {
    printf("Origin : %s", flight_details[j].origin);
    printf(", Destination : %s\n",flight_details[j].destination);
  }

  return 0;
}

Thanks alot NARUE the code compiles nicely, i had been trying to get this bit of code working for a couple of days now and was going backwards if anything. :)

i now get a segmentation fault when i run the code, and idea why that is and how i fix it

Cheers
Richie2021

Just keep plugging away at it. The more you practice, the better you get. :)

> i now get a segmentation fault when i run the code, and idea why that is and how i fix it
Check the result of each strtok() call before trying to use it with strcpy().
If your input is mal-formed, then you will be in trouble.

Also, note that "hello,,world" will appear as TWO fields to your code, whereas it would be THREE fields to excel.
Also, Also, "Hello, world",0 would appear as THREE fields to your code, and only TWO to excel.

Are you sure buf[50] is long enough to hold any line? If it isn't, then fgets() will simply stop when the buffer is full and you'll end up tokenising only part of a line, with no doubt fewer than the expected number of tokens.

> while( fgets(buf,sizeof(buf),fp) != NULL)
In case your file is much larger than expected, add
while( i < 1679 && fgets(buf,sizeof(buf),fp) != NULL)

Make the array size a #define constant, to make this even easier to manage.

I trust your string components in your struct are actually large enough to include the data and a \0.

Question from an ignorant:
Why 1679?

A birthday, perhaps? I occasionally use 1178 as a dummy value.

there are 1679 lines in the file i am trying to read.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.