将数字作为(x,y)坐标从文件存储在特定点 [英] Storing numbers as (x, y) cordinates from a file at a specific point
本文介绍了将数字作为(x,y)坐标从文件存储在特定点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个
语言:c99;编译器:gcc
解决方案
最好不要固定阅读位置)
#include< stdio.h>
typedef struct point {
int x,y;
}点;
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 \\\
,NUM_PT);
Point arr [NUM_PT];
for(int i = 0; i< NUM_PT; ++ i){
readPoint(fp,& arr [i]);
printf(Point(%d,%d)\\\
,arr [i] .x,arr [i] .y);
}
fclose(fp);
$ b $ readLine(FILE * fp,char * buff,int buff_size){
while(fgets(buff,buff_size,fp)){
if( * buff =='#'|| * buff =='\\\
')
continue;
返回1;
}
return 0;
#define LINE_MAX 128
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;
}
I have an Instance File from which I need to store the NUM_PT and all the respective co-ordinates in the form of a 2D array system (personal choice so I can access them easily). I am able to retrieve the NUM_PT but I am stuck at reading the successive cordinates into my array.
HERE IS WHAT I 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;
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;
}
After retrieving NUM_PT i tried reinitializing the count
to 5 because the cordinates start from **LINE 6* in the file.
ERROR FROM COMPILER
Language: c99 ; Compiler: gcc
解决方案
sample for "Storing numbers as (x, y) cordinates from a file" (It is better not to fix the 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 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;
}
这篇关于将数字作为(x,y)坐标从文件存储在特定点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文