Search code examples
cfgets

fgets hangs in while loop


SOLVED: fixed when closing infile before opening qfile.

I'm trying to process two files in c. For the first file I read in lines then process the strings. I'm using fgets, however when the while loop is in the last iteration it just hangs. I have:

//Not complete code. While loop is all that is in method that uses the file. 
//File I/O error checking is in place, does not assume there is always a file in argv[2]
FILE *infile;
FILE *qfile;

struct tree{
    char *name;
    char *id;
    char *permission;
    struct tree *next;
 };
 struct tree *root;
 struct tree *current;

int main(int argc, char *argv[]){
 //proper file i/o error checking in place 
  infile = fopen(argv[1], "r");
  if(infile != NULL)
     fill();//process first file
   qfile = fopen(argv[2], "r");
   if(qfile != NULL)
      find();//process second file
   }

void fill(void){
   char buffer[256];
   int first = 1;
   while(fgets(buffer,sizeof(buffer),infile) != NULL)
   {
      if(first && buffer[0] == "X")
      {
         root = (struct tree *)malloc(sizeof(struct tree));
         current = root;
         process(buffer, current);
         first = 0;
      }
      else if(buffer[0] == 'F')//siblings
      {
         current->next = (struct tree *)malloc(sizeof(struct tree));
         current = current->next;
         process(buffer, current);
      }
}

void process(char buffer[], struct tree *current)
{
    char *left;
    char *right;
    left = buffer;
    right = buffer;
    while(*left == 'F')
    {
            left++;
    }
    while(*right != ':')
    {
            right++;/*
            if(*right == ' ')
                    continue;*/

    }

    //assign name to current node
    current->name = (char *)malloc(sizeof(char)*(right-left));
    strncpy(current->name, left, right-left);
    left = right + 1;
    while(*right != '=')
    {
            right++;
    }

    //assign property to current node
    current->property = (char *)malloc(sizeof(char)*(right-left));
    strncpy(current->property, left, right-left);

    left = right + 1;
    while(oright != '\n')
    t = (struct tree *)malloc(sizeof(struct tree));
                    current = root;
                    process(buffer, current);
                    //root->name = buffer;

                    first = 0;
            }
            else if(buffer[0] == 'F')//siblings
            {
                    current->next = (struct tree *)malloc(sizeof(struct tree));
                    current = current->next;
                    process(buffer, current);

    {       right++;}

    //assign value to current node
    current->value = (char *)malloc(sizeof(char)*(right-left));
    strncpy(current->value, left, right-left);
    printf("Name =%s\nProperty=%s\nValue=%s\n", current->name,current->property,current->value);
    }

It goes through the entire file and then just hangs in the while loop. This only processes the first file. When I don't include a second file(process stdin) it doesn't hang. When it does hang it checks all the conditionals and stops at the bottom of the loop. The last line of the file is a newline character by itself.

What could be causing the loop to hang? Using 64-bit linux.

EDIT: Proper file I/O checking in place. Not complete code as I narrowed problem down to while loop in fill(). fill() only processes first file. Second file is processed by find(), a different method. Problem is with first file only. First file only used in fill().

EDIT2: posted complete code, print statements are for tracking. Sorry about all the confusion.


Solution

  • Close first file before opening second file in main.