Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 将.txt输入处理为笛卡尔坐标_Objective C_C - Fatal编程技术网

Objective c 将.txt输入处理为笛卡尔坐标

Objective c 将.txt输入处理为笛卡尔坐标,objective-c,c,Objective C,C,练习: 使用Obj C和C创建一个命令行应用程序,该应用程序将: •以以下格式获取带有笛卡尔坐标的txt文件: (2;9) (5;6) ... •(其他技术要求) 问题:处理输入数据的最佳方式是什么? C具有诸如fscanffgetc和fgets等功能。 如果我理解正确-我们不能使用fgets-因为它用于字符而不是整数/浮点数/双精度。它将读取每个字符。 我们不能使用fgetc,因为它只返回1个字符(流中的下一个字符)转换为int 我有好几种方法,但迄今为止没有一种有效。 第一个-为循环使用。

练习: 使用Obj C和C创建一个命令行应用程序,该应用程序将: •以以下格式获取带有笛卡尔坐标的txt文件:

(2;9)
(5;6)
...
•(其他技术要求)

问题:处理输入数据的最佳方式是什么? C具有诸如
fscanf
fgetc
fgets
等功能。 如果我理解正确-我们不能使用
fgets
-因为它用于字符而不是整数/浮点数/双精度。它将读取每个字符。 我们不能使用
fgetc
,因为它只返回1个字符(流中的下一个字符)转换为int

我有好几种方法,但迄今为止没有一种有效。 第一个-为循环使用
。我想先计算坐标对的数量,然后将.txt文件中的数据整理成x和y坐标的两个数组:

float xCoordinate [MAXSIZE] = {0.0};
float yCoordinate [MAXSIZE] = {0.0};

float x;
float y;
FILE *handle;
handle = fopen ("points.txt", "r");
int ch;
int n;

ch=fgetc(handle)!=EOF; //error. This will read every character including ( ) and ; . 
//Maybe I omit this loop condition? 

for (n=0; n<=ch ; (fscanf(handle, "(%f;%f)", &x, &y) )
{
xCoordinate [n+1] = x;
yCoordinate [n+1] = y;
}
float xCoordinate[MAXSIZE]={0.0};
float yCoordinate[MAXSIZE]={0.0};
浮动x;
浮动y;
文件*句柄;
handle=fopen(“points.txt”、“r”);
int-ch;
int n;
ch=fgetc(手柄)=EOF//错误。这将读取每个字符,包括()和。
//也许我忽略了这个循环条件?

对于(n=0;n而言,只要fscanf返回两个已成功读取的项目且n小于MAXSIZE,则应读取该文件

float xCoordinate [MAXSIZE] = {0.0};
float yCoordinate [MAXSIZE] = {0.0};

float x;
float y;
FILE *handle;
int n = 0;

if ( ( handle = fopen ("points.txt", "r")) == NULL) 
{
    printf ("could not open file\n");
    return 1; // or exit(1);
}
while ( ( fscanf(handle, " (%f;%f)", &x, &y) ) == 2)
{
    xCoordinate[n] = x;
    yCoordinate[n] = y;
    n++;
    if ( n >= MAXSIZE)
    {
        break;
    }
}

fclose ( handle);

有人回答了这个问题,然后删除了它。 幸运的是,我保存了他的代码。 最好的部分-它工作:

    float xCoordinate [MAXSIZE] = {0.0};
    float yCoordinate [MAXSIZE] = {0.0};

    float x;
    float y;
    FILE *handle;
    int n = 0;

    if ( ( handle = fopen ("points.txt", "r")) == NULL)
    {
        printf ("could not open file\n");
        return 1; // or exit(1);
    }
    while ( ( fscanf(handle, " (%f;%f)", &x, &y) ) == 2)
    {
        xCoordinate[n] = x;
        yCoordinate[n] = y;
        n++;
        if ( n >= MAXSIZE)
        {
            break;
        }
    }

    fclose ( handle);

表达式
ch=fgetc(handle)!=EOF
将分配
fgetc(handle)的结果!=EOF
ch
,这意味着
ch
将是
0
1
。除非文件打开失败,否则
handle
将是
NULL
并且您有。是否允许使用NSFileHandle?是的,
fgetc
函数不属于此处。关于NSFileHandle-我可以使用任何内容。现在通过谷歌搜索。您对文件的描述并不表示包含以下行数的第一行。文件的第一行是否实际包含以下行数的计数?始终检查fopen()返回的值(!=NULL),以确保操作成功