我有一个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 *文件中启动。
语言:C99;编译器:GCC
所以你有一堆警告和错误。你已经做了什么来解决它们? –
我遇到的警告是第一个和第二个。第三个警告我还没有开始执行输出文件的部分。我初始化了'arr [num_pt] []',并在扫描坐标时在我的第二个for循环中使用它,但它告诉我它没有被使用。 –
那么麻烦是什么?你能找到它抱怨的路线吗?你能看到有什么不对吗?你期望有多少号码到达那里?你传递给它多少个变量? –