fgets在while循环中挂起

时间:2013-10-14 20:07:30

标签: c fgets

求助:在打开qfile之前关闭infile时修复。

我正在尝试在c中处理两个文件。对于我在行中读取的第一个文件,然后处理字符串。我正在使用fgets,但是当while循环在最后一次迭代时它只是挂起。我有:

//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);
    }

它遍历整个文件,然后挂起while循环。这只处理第一个文件。当我不包含第二个文件(进程标准输入)时,它不会挂起。当它挂起时,它检查所有条件并停在循环的底部。该文件的最后一行是一个换行符。

可能导致循环挂起的原因是什么?使用64位Linux。

编辑:正确的文件I / O检查到位。不完整的代码,因为我将问题缩小到fill()中的while循环。 fill()只处理第一个文件。第二个文件由find()处理,这是一种不同的方法。问题仅在于第一个文件。第一个文件仅用于fill()。

EDIT2:发布完整代码,打印报表用于跟踪。 抱歉所有的困惑。

3 个答案:

答案 0 :(得分:0)

首先检查I / O

首先检查您的程序是否有足够数量的参数,因为您使用argv[1] 然后检查您是否成功打开了文件

e.g。

if ( --argc )
{
  infile = fopen(argv[1], "r");
  if ( infile != NULL )
  {
    // do your fgets stuff
    fclose(infile);
  }
}

答案 1 :(得分:0)

OP使用fill(),在infileinfile = fopen(argv[1], "r");之后读取qfile = fopen(argv[2], "r");

OP需要将流指针传递给fill()函数。

OP也会调用find()而不是预期的fill()。这是在发布错误或 问题吗?

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

void fill(FILE *f){
   char buffer[256];
   while((fgets(buffer,sizeof(buffer),f)) != NULL)
   {
      if(first time && line isn't empty or newline)initialize struct;
      else if(not first time && line isn't empty or newline)connect structs;
   }
}

答案 2 :(得分:0)

在main中打开第二个文件之前关闭第一个文件。