c - Storing numbers as (x, y) cordinates from a file at a specific point -


i have instance file need store num_pt , respective co-ordinates in form of 2d array system (personal choice can access them easily). able retrieve num_pt stuck @ reading successive cordinates array.

here have done

/* assignment 2 */  #include <stdio.h> #include <string.h> #include <stdlib.h> #include <time.h> #include <ctype.h>    #define maxs 256  int main(int argc, char *argv[])  {    int num_pt;   int inputfile = 0, outputfile = 0, i;    (i = 1; < argc; i++)     {       if (strcmp (argv[i], "-i") == 0)           inputfile = i+1;       if (strcmp (argv[i], "-o") == 0)           outputfile = i+1;     }   if (inputfile == 0)     {       /* invalid command line options */       printf("\nincorrect command-line...\n");       printf("> %s [-i inputfile [-o outputfile]]\n\n", argv[0]);       exit(0);     }    file *fp;   fp = fopen(argv[inputfile], "r");    int count = 0;         if (fp == 0)     {       printf("\ncould not find %s\n", argv[inputfile]);       exit(0);     }    char line[maxs];   while (fgets(line, sizeof line, fp) != null)     {       if (count == 4)         {          fscanf(fp, "%d", &num_pt);          break;         }       else         count++;      }    int arr[num_pt][1];   while (fgets(line, sizeof line, fp) != null)     {       if (count == 5)         {           int k, j, cord;           (k = 0; k < num_pt; k++)              {               (j = 0; j < num_pt; j++)                  {                    while (fscanf(fp, "%d%d", &cord) > 0)                         {                          arr[k][j] = cord;                          j++;                         }                  }               }         }     }   fclose(fp)   return 0;  } 

after retrieving num_pt tried reinitializing count 5 because cordinates start **line 6* in file.

error compilerubuntu bash script

language: c99 ; compiler: gcc

sample "storing numbers (x, y) cordinates file" (it better not fix reading position)

#include <stdio.h>  typedef struct point {     int x, y; } point;  int readpoint(file *fp, point *p); int readint(file *fp, int *n);  int main(void){     file *fp = fopen("instance10_001.txt", "r");     point p;     int max_x, max_y;      readpoint(fp, &p);     max_x = p.x;     max_y = p.y;     printf("max_x:%d, max_y:%d\n", max_x, max_y);      int num_pt;     readint(fp, &num_pt);     printf("num_pt:%d\n", num_pt);      point arr[num_pt];     for(int = 0; < num_pt; ++i){         readpoint(fp, &arr[i]);         printf("point(%d, %d)\n", arr[i].x, arr[i].y);     }     fclose(fp); }  int readline(file *fp, char *buff, int buff_size){     while(fgets(buff, buff_size, fp)){         if(*buff == '#' || *buff == '\n')             continue;         return 1;     }     return 0; }  #define line_max 128 int readpoint(file *fp, point *p){     char buff[line_max];     if(readline(fp, buff, sizeof buff)){         return 2 == sscanf(buff, "%d %d", &p->x, &p->y);     }     return 0; } int readint(file *fp, int *n){     char buff[line_max];     if(readline(fp, buff, sizeof buff)){         return 1 == sscanf(buff, "%d", n);     }     return 0; } 

Comments

Popular posts from this blog

sql server - Cannot query correctly (MSSQL - PHP - JSON) -

php - trouble displaying mysqli database results in correct order -

C++ Linked List -