Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/58.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.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
C 我在读取文件的语法方面遇到问题,请帮助?_C_File - Fatal编程技术网

C 我在读取文件的语法方面遇到问题,请帮助?

C 我在读取文件的语法方面遇到问题,请帮助?,c,file,C,File,我必须编写一个程序,打开文件并读取每一行,直到达到EOF。文件中的每一行都有两个双精度值,表示二维空间中的一个点。对于每一条线,读取两个值,创建一个点,如果该点位于坐标平面的第一象限,则打印该点 好的!这是我的代码: #include <stdlib.h> #include "point.h" FILE *open_file(char const name[], char const mode[]) { FILE *file; file = fopen("test_po

我必须编写一个程序,打开文件并读取每一行,直到达到EOF。文件中的每一行都有两个双精度值,表示二维空间中的一个点。对于每一条线,读取两个值,创建一个点,如果该点位于坐标平面的第一象限,则打印该点

好的!这是我的代码:

#include <stdlib.h>
#include "point.h"

FILE *open_file(char const name[], char const mode[])
{
   FILE *file;
   file = fopen("test_points.txt", "r");

   if (file == NULL)
   {
      perror("Error!\n");
   }
   return file;
}

int point is_positive(double x, double y)
{
   struct point p;
   int a;

   if (p.x >= 0 && p.y >= 0)
   {
      a = 1;
   }
   else
   {
      a = 0;
   }
   return a;
}

void point_in_Q1(FILE *in, FILE *out)
{
   struct point p;
   double check, x, y;

   check = fscanf(in, "%lf%lf", &p.x, &p.y);
   p = create_point(x, y);

   while (check != EOF)
   {
      if (is_positive(x, y) == 1)
      {
         fprintf(out, "%lf%lf", p.x, p.y);
         check = fscanf(in, "%lf%lf", &p.x, &p.y);
      }
   }
}
第4行的编译错误是因为您没有包含,所以编译器不知道文件*是什么

第16行的编译错误是因为有两个类型名int和point作为返回类型。你需要:

int is_positive(double x, double y)
第32行的语法错误再次为FILE*

将文件名和打开模式传递到函数中,然后忽略它们,这似乎很奇怪

#include <stdlib.h>
#include <stdio.h>
#include "point.h"

FILE *open_file(char const name[], char const mode[])
{
   FILE *file = fopen(name, mode);
   if (file == NULL)
       perror("Error!\n");
   return file;
}
由于您尚未初始化p,您将以不可靠答案的形式陷入麻烦。表面上,你可以简单地写:

int is_positive(double x, double y)
{
    return (x >= 0.0 && y >= 0.0);
}
或者您可以使用:

int is_positive(double x, double y)
{
    point p = { x, y };
    if (p.x >= 0.0 && p.y >= 0.0)
        return 1;
    else
        return 0;
}
或者你可以通过一个点:

int is_positive(point p)
{
    if (p.x >= 0.0 && p.y >= 0.0)
        return 1;
    else
        return 0;
}
或者再次缩写为:

int is_positive(point p)
{
    return (p.x >= 0.0 && p.y >= 0.0);
}
您的其他功能基本正常,但有些地方有点古怪:

void point_in_Q1(FILE *in, FILE *out)
{
   struct point p;
   double check, x, y;

   check = fscanf(in, "%lf%lf", &p.x, &p.y);  // fscanf() returns an int
   p = create_point(x, y);                    // What does this do?

   while (check != EOF)
   {
      if (is_positive(x, y) == 1)
      {
         fprintf(out, "%lf%lf", p.x, p.y);
         check = fscanf(in, "%lf%lf", &p.x, &p.y);
      }
   }
}
注意,p是通过调用fscanf初始化的,但是x和y都没有初始化。因此,创建_点的调用将造成严重破坏。如果读入x和y,那么使用create_point会有一些意义。您的输出将同时运行这两个数字,并且在末尾不包含换行符。输入检查应该是读取两个值。如果在输入流中输入一个字母,循环将运行很长时间,因为fscanf将返回0而不是EOF。同样,如果你有一个非正的点,你会进入一个无限循环,因为当你跳过输出时你没有读到一个新的点。你也有x和y与p.x和p.y的问题

最好写为:

void point_in_Q1(FILE *in, FILE *out)
{
    struct point p;

    while (fscanf(in, "%lf %lf", &p.x, &p.y) == 2)
    {
        if (is_positive(p) == 1)
            fprintf(out, "(%lf, %lf)\n", p.x, p.y);
    }
}

显然,您可以根据自己的需要改变输出格式。此代码使用的是int is_positivepoint p;is_正数函数的版本。

包含应该可以解决您的问题,因为此头文件中定义了文件

您如何知道它不太正确?你犯了什么错误?编译时还是运行时?函数的哪一部分语法不正确?因为缺少函数和结构,所以整个程序无法编译。你能给出一个编译器错误最少的完整示例吗?因为这似乎就是问题所在。其他函数都不是从main调用的-它们将如何运行?我知道如何打开文件,但我不确定如何读取文件。读取文件的语法是否有通用格式??当然,我会继续研究这个问题。main上的相关注释:它声明为返回int,那么为什么要返回文件*?另外,正确的参数列表是int argc,char**argv。
void point_in_Q1(FILE *in, FILE *out)
{
   struct point p;
   double check, x, y;

   check = fscanf(in, "%lf%lf", &p.x, &p.y);  // fscanf() returns an int
   p = create_point(x, y);                    // What does this do?

   while (check != EOF)
   {
      if (is_positive(x, y) == 1)
      {
         fprintf(out, "%lf%lf", p.x, p.y);
         check = fscanf(in, "%lf%lf", &p.x, &p.y);
      }
   }
}
void point_in_Q1(FILE *in, FILE *out)
{
    struct point p;

    while (fscanf(in, "%lf %lf", &p.x, &p.y) == 2)
    {
        if (is_positive(p) == 1)
            fprintf(out, "(%lf, %lf)\n", p.x, p.y);
    }
}