Killer_B
Killer_B

Reputation: 63

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++;                                                            
    }

Upvotes: 1

Views: 103

Answers (1)

Corb3nik
Corb3nik

Reputation: 1177

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];

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.

Upvotes: 1

Related Questions