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.
First of all always check I/O
first check if there are enough number of arguments to your program since you use argv[1]
then check whether you successfully opened the file
eg
if ( --argc )
{
infile = fopen(argv[1], "r");
if ( infile != NULL )
{
// do your fgets stuff
fclose(infile);
}
}
OP uses fill()
, which reads infile
both after infile = fopen(argv[1], "r");
and qfile = fopen(argv[2], "r");
OP needs to pass stream pointer to fill()
function.
Also OP calls find()
instead of expected fill()
. Is this typo in posting or the problmem?
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;
}
}
关闭第一个文件,然后再打开main中的第二个文件。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.