有人能帮我找出C语言中的逻辑错误吗?我正在尝试 fscanf 到一个数组和一个变量。
我正在编写一个程序,该程序调用 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
通常的写法是
for(i = 0; i。注意小于而不是小于或等于。在 C 中使用前增量而不是后增量并不重要。
在 else 中,如果您正在执行赋值:
i = SIZE
。这在条件上下文中被解释为( i = SIZE ) != 0
。您实际上想要进行比较:i == SIZE
,但 i 永远不会等于SIZE
,因为循环仅在SIZE
之前结束。要将
n
个 int 读入数组,并将后续 int 读入不同位置,请执行以下操作: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.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 equalSIZE
since the loop ends just one beforeSIZE
.To read
n
ints into an array, and a following int in a different place, do this:我相信你的问题是,你写的 &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.
您的错误在于以下代码:
else if(i = SIZE)
行有两个错误,首先您需要==
运算符,而不是= 运算符。
该行的第二个错误是它应该读取
else if(i == SIZE - 1)
。前面的 if
if(i != SIZE)
也是不正确的,我猜你想要if(i != SIZE - 1)
。如果您这样做了,您可以将最后的else if( i == SIZE - 1)
简化为 simlplyelse
。附带说明一下,最好用
{}
包围 if 和 else 块,以防止将来插入第二行时出现其他逻辑错误。总之,将该部分更改为:
Your error is with this code:
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 wantif(i != SIZE - 1)
. If you did this you can simplify your lastelse if( i == SIZE - 1)
to simlplyelse
.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: