0

i am working in c language and i have to debug my code as i want to see the behavior of my program.

i tried these command in my linux terminal:

    gcc readFile.c
    gdb a.out
    b main
    run
    next

below is my code file

   void main()
   {
          struct voter v[10];   
          FILE* fin;
          fin=fopen("voters.txt","r");
          if(fin == NULL) {
               perror("Error opening file");
               return(-1);
          }
          char buffer[2000];
          char arr[10][100];
          int i=0;
          while(fgets(buffer,2000,fin)!=NULL)
               strcpy(arr[i++],buffer);        
          fclose(fin);
          char del[2]="/";
          char * token;
          token=strtok(arr[0],del);
          i=0;
          while(1)
          {
               strcpy(v[i].name,token);

               token = strtok(NULL, del);

               strcpy(v[i].id,token);

               printf("%s\t%s\n",v[i].name,v[i].id);

               token=strtok(arr[i+1],del);

               if(token==NULL)
                    break;
           }

     }

after trying the above commands the programs runs completey to end to the file. what i was expecting that it should run next line just after main(). I don't have any idea what to do to debug my program. Also it is not any break point on any line like this

    break 22

but to no avail. so i'm struck in it that how to debug my code

majid bhatti
  • 83
  • 12
  • Possible duplicate of [How to debug a C program](https://stackoverflow.com/questions/2590931/how-to-debug-a-c-program) – Evyatar Cohen Oct 13 '19 at 04:49
  • 1
    that does not solve my problem it still asks use file command. i already used that method – majid bhatti Oct 13 '19 at 04:57
  • Did you compile with debug symbols? Add `-ggdb` to your compile command. – Shahe Ansar Oct 13 '19 at 05:12
  • No i did not try that i compiled my code using -g flag – majid bhatti Oct 13 '19 at 05:42
  • Can you share you compile command. Do you get any warnings/errors ?. On gcc, code does not compile (void main tries to return -1). – dash-o Oct 13 '19 at 07:25
  • 2
    debugger and compiler version totally incompatible maybe some ABI change in between? Which versions of gcc and gdb you are using? – Klaus Oct 13 '19 at 07:50
  • 1
    yes @dash-o there were some warnings, but they were stopping code to compile. also my problem was solved using -ggdb flag, thanks – majid bhatti Oct 13 '19 at 10:59
  • 1
    Without a working source, it will be hard to provide good advice. Consider updating answer to compile without errors/warnings. Consider sharing first few lines of voter.txt – dash-o Oct 13 '19 at 12:54
  • voters.txt contain "Muhammad Yousuf/43304-2042545-5 Atif Aslam/43344-6052684-2 Shoaib Malik/43224-5662735-4" – majid bhatti Oct 13 '19 at 13:21

0 Answers0