标题是我目前对问题的最佳猜测,但我并不完全确定。 我正在尝试从包含以字符开头的数字和字符串的文件中读取(仅字符串以字符开头)。该文件是由程序中较早创建的代码创建的,该代码应在需要该文件之前执行。
主要内容
int main(void){
//name of the file to accessed
char *fileName = "input.txt";
node *head, *tail;
node *symListHead = NULL;
head = tail = createNode();
//creates the file with no comments exits if the file is not created
if(cleanFile(fileName) == 0){
exit(1);
}
//create the head of the linked list containing all tokens
head = createSymbolList();
//this should create the file and fill in called "lexemelist.txt"
printTableAndList( head );
//create a sym list of ints for comparison to enumerated types
symListHead = generateSymList();
//recursiveParse( symListHead );
return 0;
}//end main
因此,在 printTabelAndList(head) 行上,应该创建并填充文件“lexemelist.txt”。在 symListHead =generateSymList() 中应该使用该文件。但是,如果我使用 symListHead =generateSymList() 注释掉运行代码,则文件将被创建并按应有的方式填充,但如果文件中保留该行,则前导为空,并且不会从空文件中读取任何内容。
lexemelist.txt 内容包含行 symListHead =generateSymList();留在
29 2 x 17 2 y 18 21 2 x 20 2 y 4 3 56 18 22 19
lexemelist.txt 内容包含行 symListHead =generateSymList();已删除
生成SymList()
symNode *generateSymList(){
FILE *fp;
char buff[255];
symNode *head, *temp;
head = temp = createSymNode();
fp = fopen("lexemelist.txt" , "r");
if(fp == NULL){
printf("\nfile not found.\n");
return NULL;
}
//this line is skicped since the file is empty for some reason
while(fscanf(fp, "%s", buff) != EOF)
{
if(isalpha(buff[0]))
continue;
printf("%d", atoi(buff));
temp->sym = atoi(buff);
temp->next = createSymNode();
temp = temp->next;
}//end while
fclose(fp);
return head;
}
printTableAndList()
void printTableAndList(node *head){
FILE *tfp, *lfp;
tfp = fopen("lexemetable.txt", "w");
lfp = fopen("lexemelist.txt", "w");
fprintf(tfp , "lexeme token type\n\n");
generateTableAndList(head, tfp, lfp);
}
最佳答案
没关系,我忘记关闭 printTableAndList() 函数中的文件。
关于c - 文件内容未及时填充以在 C 中读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38423466/