[英]what is wrong with this program.? what is causing the infinite loop. C
The function supposed to read each line from a file and send each line to an another function. 该函数应该从文件中读取每一行并将每一行发送到另一个函数。 but am getting an error on the commented part of the code. 但在代码的注释部分出现错误。 why is that? 这是为什么?
I am storing each character in a character array. 我将每个字符存储在一个字符数组中。 It should be simple enough. 它应该足够简单。 I don't understand why it isn't working. 我不明白为什么它不起作用。 Is it because its an array that its not storing it in it? 是不是因为它是一个数组而不将其存储在其中?
I think I added enough details about the program, but I get the red thing saying I need to add more. 我想我已经添加了有关该程序的足够详细信息,但是我得到了一个红色的东西:我需要添加更多内容。 Hopefully this is enough. 希望这足够了。
void exercise3(void)
{
FILE * inputFile;
char line[64];
int number_of_conversions=1;
int i = 0;
printf("BEGIN EXERCISE 3\n");
// Open the file /public/lab10ex3.txt for reading. Store the handle in the
// variable inputFile.
// (WRITE THE CODE HERE)
inputFile=fopen("/public/lab10ex3.txt","r");
// Test to ensure the file was successfully opened. If opening the file
// failed, display an error message, and exit the program with an exit
// code of 1.
// (WRITE THE CODE HERE)
if (inputFile==NULL){
printf("bad stuff \n");
exit(1);
}
// Read each line from the file into the character array 'line'. Pass
// each line of text to the processDrawCommand function.
// (WRITE THE CODE HERE)
while (number_of_conversions!=0 && number_of_conversions!=EOF ){
number_of_conversions=fscanf(inputFile,"%c",&line[i]); //Error Here
if (line[i]=='\n'){
i=0;
processDrawCommand('\n');
}
processDrawCommand(line);
i++;
}
One thing that I can notice is that the value of i
in &line[i]
can surpass the allocated array size of 64 that you have declared above : char line[64];
我注意到的一件事是&line[i]
中i
的值可以超过您在上面声明的64分配的数组大小: char line[64];
This would mean that if you are analyzing a line that has more than 64 characters, you will get an out-of-bounds error and your program will fail. 这意味着,如果您要分析的行数超过64个字符,则会出现越界错误,并且程序将失败。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.