Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Need help : C programming language (Segmentation fault Core dumped)

+2 votes
asked Sep 12, 2018 by (140 points)
Hello there,

Nice to be here with you on this Forum. I need your help to solve the issue with my source C code below. It worked fine before on Ubuntu 14.04, the problem appears when I upgraded my comp with the latest Ubuntu 18.04 LTS. It's a string separator : A file named "Fac.txt" is opened and will be readed line by line, then two files named "Date.txt" and "Turn.txt" should be created which contain : 1st, the date ; 2nd, the last string behind the date. I really don't know what is matter but on my first comp I had a 32 bit cpu and on the second one it's a 64 bit cpu. My compiler is Gcc 7. Thank you for your understanding.

Regards.

Hervé.

The source Code:

main.c

  

#include <stdio.h>

#include <stdlib.h>

#include <ctype.h>

#define INPUT "Fac.txt"

#define DATE "Date.txt"

#define TURN "Turn.txt"

#define LONG 128

int main()

{

 FILE *f_in, *fa_out, *fb_out;

 char line[LONG];

 char date[6];

 char turn[20];

 int i,j,k,l;

 if ((f_in = fopen(INPUT,"r")) == NULL)

 {

      fprintf(stderr, "\nErr Can't open file %s\n",INPUT);

      return(EXIT_FAILURE);

    }

  if ((fa_out = fopen(DATE,"w")) == NULL)

    {

      fprintf(stderr, "\nErr Can't write on file %s\n",DATE);

      return(EXIT_FAILURE);

    }

  if ((fb_out = fopen(TURN,"w")) == NULL)

    {

      fprintf(stderr, "\nErr Can't write on file %s\n",TURN);

      return(EXIT_FAILURE);

    }

  while (fgets(line,LONG,f_in) != NULL)

   {

   k=0;

   l=0;

   for(i=0;line[i]!='\n';i++)

    {

      j=0;

      if(isdigit(line[i]) && line[i+2]==47 && line[i+5]==59)

{

          for(j=i;j<=5;j++)

   {

             date[k]=line[j];

             k++;

   }

          for(j=0;j<5;j++)

            fprintf(fa_out,"%c", date[j]);

  fprintf(fa_out,"\n");

          

          for(j=i+7;line[j]!='\n';j++)

           {

             if(line[j]!=34)

              turn[l++]=line[j];

           }

          fprintf(fb_out," ");

          for(j=0;j<l;j++)

            fprintf(fb_out,"%c", turn[j]);

          fprintf(fb_out,"\n");

          

       }       

    }   

   }

  fclose(fa_out);

  fclose(fb_out);

  return(EXIT_SUCCESS);

}

- an example of Fac.txt

02/08; ELIS TRAPPES; 78190 TRAPPES 02/08; ELIS ST THIBAULT; 77400 ST THIBAULT DES VIGNES

01/08; ELIS ST THIBAULT; 77400 ST THIBAULT DES VIGNES 01/08; ELIS / TRAPPES; 78190 TRAPPES

01/08; CCF; 92000 NANTERRE 01/08; ; 92130 ISSY LES MOULINEAUX

01/08; KONICA CONSOMMABLES; 78420 CARRIERES SUR SEINE 01/08; ; 93600 AULNAY SOUS BOIS

02/08; CCF; 92000 NANTERRE 02/08; ; 94130 NOGENT SUR MARNE

02/08; RESO COLLEGIEN; 77090 COLLEGIEN 02/08; ; 93600 AULNAY SOUS BOIS

1 Answer

0 votes
answered Jun 25, 2025 by Jerry Jeremiah (2,040 points)
On line 50 of the program it tries to access turn while turn is "ELIS ST THIBAULT; 77", but l is currently 1409 which is outside the array because turn is only 20 characters long.

(gdb) p turn
$1 = "ELIS ST THIBAULT; 77"
(gdb) p l
$2 = 1409

So there is a logic problem in your program.
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and receive answers from other members of the community.
...