我们没有发现错误这个函数。只显示main中的[0]。 [英] we did not found error this function.display only record[0] in main.
本文介绍了我们没有发现错误这个函数。只显示main中的[0]。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
#include<stdio.h>
#include<string.h>
#define ROWCAP 200
#define COLCAP 1000
int readRecords(char filename[], char records[][100], int* , int, int);
int main ()
{
int i;
char records[ROWCAP][COLCAP];
int numOfRec;
readRecords("input.txt",records,&numOfRec,ROWCAP,COLCAP);
printf("%d",numOfRec);/* testing */
for(i=0;i<numOfRec;i++)
{
printf("%s\n",records[i]);
}
}
int readRecords(char filename[], char records[][100], int* numOfRecords, int rowCap, int colCap)
{
FILE *inp;
int flag = 0;
int numberRow = 0;
int status;
int i=0;
inp = fopen(filename,"r");
status = fscanf(inp,"%s",records[numberRow]);
printf("%s\n",records[numberRow]);/* testing */
for( i = 1 ;status != EOF; i++ )
{
status = fscanf(inp,"%s",records[i]);
printf("%s\n",records[i]);/* testing */
}
numberRow = i-1 ;
*numOfRecords = numberRow;
return flag;
}
推荐答案
你需要结合你的数据来看一下:我建议你使用调试器来单步执行代码并检查变量以找出每个阶段发生的事情。
大概你从printf语句中获得了合理的值?但是我会对代码保持警惕:当你将值填入记录
时,你似乎忽略了为内存分配设置的大小*(使用ROWCAP和COLCAP)变量,所以你可能会破坏其他内存。
You need to look at this in conjunction with your data: I would suggest that you use a debugger to single step through your code and examine variables to find out what is happening at each stage.
Presumably you are getting sensible values from your printf statements? But I would be wary of the code: you appear to be ignoring the sizes you set for the memory allocation *(using ROWCAP and COLCAP) when you fill your values into therecords
variable, so it is possible that you are corrupting other memory.
这篇关于我们没有发现错误这个函数。只显示main中的[0]。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文