2016-11-10 82 views
0

我有一個Instance File從中我需要存儲NUM_PT並在2D陣列系統的形式所有相應座標(個人選擇,所以我可以輕鬆訪問它們)。我能夠檢索到NUM_PT,但我堅持將連續的座標讀入我的數組中。在特定點從文件存儲號碼爲(x,y)的cordinates

這裏是我做了什麼

/* 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; 

    for (i = 1; i < 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; 
      for (k = 0; k < num_pt; k++) 
      { 
       for (j = 0; j < num_pt; j++) 
       { 
        while (fscanf(fp, "%d%d", &cord) > 0) 
         { 
         arr[k][j] = cord; 
         j++; 
         } 
       } 
       } 
     } 
    } 
    fclose(fp) 
    return 0; 

} 

檢索NUM_PT我試圖重新初始化count到5後,因爲cordinates從** LINE 6 *文件中啓動。

ERROR FROM編譯ubuntu bash script

語言:C99;編譯器:GCC

+0

所以你有一堆警告和錯誤。你已經做了什麼來解決它們? –

+0

我遇到的警告是第一個和第二個。第三個警告我還沒有開始執行輸出文件的部分。我初始化了'arr [num_pt] []',並在掃描座標時在我的第二個for循環中使用它,但它告訴我它沒有被使用。 –

+0

那麼麻煩是什麼?你能找到它抱怨的路線嗎?你能看到有什麼不對嗎?你期望有多少號碼到達那裏?你傳遞給它多少個變量? –

回答

1

樣品「存儲號碼從一個文件(X,Y)cordinates」(最好是不固定的讀取位置)

#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 i = 0; i < 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; 
} 
+0

我還沒有學過c中的'struct'聲明。這幫助了我很多,我打算更多地研究'struct',因爲我覺得它對我的任務有用。謝謝。 –

+0

案例使用二維數組,'詮釋ARR [num_pt] [1];' - >'INT ARR [num_pt] [2];' – BLUEPIXY

+0

你是說,我將能夠訪問我的數組類型爲'INT ARR [ num_pt] [1]'還是你的方法是替代品,並且是解決這個問題的更好方法? –

相關問題