有人能帮我找出C语言中的逻辑错误吗?我正在尝试 fscanf 到一个数组和一个变量。

发布于 2024-12-11 09:05:44 字数 1540 浏览 0 评论 0 原文

我正在编写一个程序,该程序调用 main.c 中的几个其他函数。在 main.c 中,我要打开一个包含 33 个整数的文件,并将前 10 个整数放入一个数组(a [])中。尺寸是十。第 11 个 int 将存储在一个变量中,以便稍后用作搜索目标,这就是我将其称为目标的原因。我尝试了几种不同的方法来尝试将第 11 个整数放入目标。这给了我一个逻辑错误。有人可以向我解释一下错误在哪里吗?我的理解是,逻辑错误在语法上有意义,但代码中的某些内容没有意义,例如尝试使用动词而不是介词。我不确定我是否对此太陌生而看不到错误,或者我是否已经盯着它看了太久。输出显示数组已正确填充,但目标始终为 0,因此第 11 个 int 被放置在下一个数组中。代码只是一个片段。

感谢您的帮助!

int main (void)
    {
         int a[SIZE];
         int i;
         int j;
         int trgt[1];
         int target;
         FILE* fpmyfile;
         int closeResult;

    printf("Function working 1.\n");

         fpmyfile = fopen("MYFILE.DAT", "r");                     // open file myfile
    printf("Function opening file.\n");
            if(!fpmyfile)
                {
                     printf("Could not open input file.\n");
                     exit (101);
                }
            else printf("The file opened.\n");

    printf("Starting for loop.\n");

         for(j = 1; j <= 3; j++)
            {
                 for(i = 0; i <= SIZE - 1; i++)                    //get ints from myfile into array
                     {
                       if(i != SIZE)
                          fscanf(fpmyfile, "%d", &a[i]);
                       else if(i = SIZE)
                            fscanf(fpmyfile, "%d", &trgt[i]);
                     }

            target = trgt[1];

    printf("Scan a done.\n");
    printf("\nScanned into a[]");
    printf("Target is %3d\n.", target);   //This print statement says that target is 0

I am writing a program that calls several other functions from main.c. In main.c, I'm to open a file of 33 ints and put the first ten into an array (a []). SIZE is ten. The 11th int is to be stored in a variable to be used later as a seach target, which is why I called it target. I have tried a few different methods of trying to get the 11th int into target. This gives me a logic error. Could someone explain to me where the error is? My understanding is that a logic error makes syntactical sense, but something in the code doesn't make sense, like trying to use a verb instead of a preposition. I'm not sure if I'm too new at this to see the error or if I've been staring at it for too long. The output shows the array is filled correctly, but the target is always 0, so the 11th int gets placed in the next array. The code is just a fragment.

Thanks for any help!!

int main (void)
    {
         int a[SIZE];
         int i;
         int j;
         int trgt[1];
         int target;
         FILE* fpmyfile;
         int closeResult;

    printf("Function working 1.\n");

         fpmyfile = fopen("MYFILE.DAT", "r");                     // open file myfile
    printf("Function opening file.\n");
            if(!fpmyfile)
                {
                     printf("Could not open input file.\n");
                     exit (101);
                }
            else printf("The file opened.\n");

    printf("Starting for loop.\n");

         for(j = 1; j <= 3; j++)
            {
                 for(i = 0; i <= SIZE - 1; i++)                    //get ints from myfile into array
                     {
                       if(i != SIZE)
                          fscanf(fpmyfile, "%d", &a[i]);
                       else if(i = SIZE)
                            fscanf(fpmyfile, "%d", &trgt[i]);
                     }

            target = trgt[1];

    printf("Scan a done.\n");
    printf("\nScanned into a[]");
    printf("Target is %3d\n.", target);   //This print statement says that target is 0

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

得不到的就毁灭 2024-12-18 09:05:44
 for(i = 0; i <= SIZE - 1; i++)

通常的写法是for(i = 0; i 。注意小于而不是小于或等于。在 C 中使用前增量而不是后增量并不重要。

 if(i != SIZE)
     fscanf(fpmyfile, "%d", &a[i]);
 else if(i = SIZE)
     fscanf(fpmyfile, "%d", &trgt[i]);

在 else 中,如果您正在执行赋值:i = SIZE。这在条件上下文中被解释为 ( i = SIZE ) != 0。您实际上想要进行比较:i == SIZE,但 i 永远不会等于 SIZE,因为循环仅在 SIZE 之前结束。

要将 n 个 int 读入数组,并将后续 int 读入不同位置,请执行以下操作:

for( i = 0; i < n; ++i ) // reads n ints
{
     fscanf(fpmyfile, "%d", &a[i]);
}
fscanf(fpmyfile, "%d", &target); // reads an additional int into target
 for(i = 0; i <= SIZE - 1; i++)

The usual way to write this is for(i = 0; i < SIZE; ++i). Note the less than instead of less or equal. Using preincrement instead of postincrement is not really important in C.

 if(i != SIZE)
     fscanf(fpmyfile, "%d", &a[i]);
 else if(i = SIZE)
     fscanf(fpmyfile, "%d", &trgt[i]);

In your else if you are performing an assignment: i = SIZE. This is interpreted in the context of the conditional as ( i = SIZE ) != 0. You actually want a comparisson there: i == SIZE, but still i will never equal SIZE since the loop ends just one before SIZE.

To read n ints into an array, and a following int in a different place, do this:

for( i = 0; i < n; ++i ) // reads n ints
{
     fscanf(fpmyfile, "%d", &a[i]);
}
fscanf(fpmyfile, "%d", &target); // reads an additional int into target
爱的十字路口 2024-12-18 09:05:44

我相信你的问题是,你写的 &trgt[i] 的意思是 &trgt[0] (大多数程序员会使用语义上等效的 trgt),而且当你重新分配给目标时,你可能意味着 target=trgt[0] 。

同样在你的第二个中,如果你使用了一个=,我怀疑你想要一个==。

不知道你是如何结束这里的,但在我看来,如果你愿意的话,你可以直接扫描到目标变量。

I believe your problem is that where you wrote &trgt[i] you meant &trgt[0] (and most programmers would use the semantically equivalent trgt), also when you reassign to target you probably mean target=trgt[0].

Also in your second if you used a single = where I suspect you intended a ==.

Not sure how you ended up here but it seems to me that you can scanf directly into the target variable if you want to.

倥絔 2024-12-18 09:05:44

您的错误在于以下代码:

             for(i = 0; i <= SIZE - 1; i++)                    //get ints from myfile into array
                 {
                   if(i != SIZE)
                      fscanf(fpmyfile, "%d", &a[i]);
                   else if(i = SIZE)
                        fscanf(fpmyfile, "%d", &trgt[i]);
                 }

else if(i = SIZE) 行有两个错误,首先您需要 == 运算符,而不是 = 运算符。

该行的第二个错误是它应该读取else if(i == SIZE - 1)

前面的 if if(i != SIZE) 也是不正确的,我猜你想要 if(i != SIZE - 1)。如果您这样做了,您可以将最后的 else if( i == SIZE - 1) 简化为 simlply else

附带说明一下,最好用 {} 包围 if 和 else 块,以防止将来插入第二行时出现其他逻辑错误。

总之,将该部分更改为:

             for(i = 0; i <= SIZE - 1; i++)                    //get ints from myfile into array
                 {
                   if(i != SIZE - 1) {
                      fscanf(fpmyfile, "%d", &a[i]);
                   }
                   else {
                        fscanf(fpmyfile, "%d", &trgt[i]);
                   }
                 }

Your error is with this code:

             for(i = 0; i <= SIZE - 1; i++)                    //get ints from myfile into array
                 {
                   if(i != SIZE)
                      fscanf(fpmyfile, "%d", &a[i]);
                   else if(i = SIZE)
                        fscanf(fpmyfile, "%d", &trgt[i]);
                 }

The line else if(i = SIZE) has two mistakes, first you need the == operator, not the = operator.

The second error with the line is that it should read else if(i == SIZE - 1).

The if before it, if(i != SIZE), is incorrect as well, I am guessing you want if(i != SIZE - 1). If you did this you can simplify your last else if( i == SIZE - 1) to simlply else.

As a side note, it is always a good idea to surround your if and else blocks with {}'s to prevent other logical errors if you insert a second line in the future.

So in summary change that section to this:

             for(i = 0; i <= SIZE - 1; i++)                    //get ints from myfile into array
                 {
                   if(i != SIZE - 1) {
                      fscanf(fpmyfile, "%d", &a[i]);
                   }
                   else {
                        fscanf(fpmyfile, "%d", &trgt[i]);
                   }
                 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文