从文件中读取单词到简单的链接列表 [英] Read word from file into simple linked list
问题描述
'\ n'
)或空格('
)被检测到,表示一个字。 对不起,我是一个文件指针的新手,这是我到目前为止:
struct list {//全局
char字符串[30];
struct list * next;
};
int main(void){
FILE * filePtr;
char file [] =text.txt;
char tempStr [30];
list * curr,* header;
char c;
int i = 0;
curr = NULL;
header = NULL; ((filePtr = fopen(file,r))== NULL){
printf(\\\
Error opening file!);
if
getchar();
出口(101);
}
printf(\\\
File打开阅读。); $(!EOF){
while((c = fgetc(filePtr)!='')&&(c = fgetc(filePtr)!='\\\
')
)){
curr =(list *)malloc(sizeof(list));
// c = fgetc(filePtr);
tempStr [i] = fgetc(filePtr);
i ++;
}
tempStr [i] ='\0';
strcpy(curr-> string,tempStr);
curr-> next = header;
header = curr;
i = 0;
while(curr!= NULL){
printf(%s - ,curr-> string); //这将不会打印。
curr = curr->下;
if(fclose(filePtr)== EOF){
printf(\\\
Error closing file!);
getchar();
出口(102);
}
printf(\\\
File is closed.\\\
);
getchar();
getchar();
$ b 如果文本文件:
有美好的一天
希望的输出:
have - a - nice - day
但是,除了文件打开和关闭之外,我无法打印任何内容。
谢谢。
b $ b 解决方案 c $ c>,它是在 stdio.h
中定义的系统宏。文件读取API( fgetc
, fread
, fscanf
)一旦到达文件结尾,返回-1。所以在你的程序中你有 while(!EOF)
这将始终是false,因为不是 -1
始终为0. -1
将以二进制补码表示,因此该变量的所有位将为1。 int
是2, -1
将被存储为 0xFFFF
在 int
变量中)。
使用下面的示例代码。
while(EOF!= (c = fgetc(filePtr)))
{
if((c =='')||(c =='\\\
'))
{
if(i == 0)
{
continue;
}
tempStr [i] ='\0';
i = 0;
//在这里做你的linklist节点的创建和插入操作
continue;
}
tempStr [i] = c;
i ++;
}
I need to write a program to read from a file, then save the words into a linked list for further use. I decided to read the text character by character using fgetc, then save all into the list each time a newline ('\n'
) or space (' '
) is detected, indicating one word.
Sorry I'm a newbie in file pointers, this is what I've gotten so far:
struct list { //global
char string[30];
struct list *next;
};
int main(void) {
FILE *filePtr;
char file[] = "text.txt";
char tempStr[30];
list *curr, *header;
char c;
int i = 0;
curr = NULL;
header = NULL;
if((filePtr = fopen(file, "r")) == NULL) {
printf("\nError opening file!");
getchar();
exit(101);
}
printf("\nFile is opened for reading.\n");
while(!EOF) {
while((c = fgetc(filePtr) != ' ') && (c = fgetc(filePtr) != '\n')) {
curr = (list*)malloc(sizeof(list));
//c = fgetc(filePtr);
tempStr[i] = fgetc(filePtr);
i++;
}
tempStr[i] = '\0';
strcpy(curr->string, tempStr);
curr->next = header;
header = curr;
i = 0;
}
while(curr!=NULL) {
printf("%s - ", curr->string); //This will not print.
curr = curr->next;
}
if(fclose(filePtr) == EOF) {
printf("\nError closing file!");
getchar();
exit(102);
}
printf("\nFile is closed.\n");
getchar();
getchar();
}
If the text file:
have a nice day
Desired output:
have - a - nice - day
But, I could not print out anything except the file opened and closed.
Thanks.
解决方案 Value of the macro EOF
is -1
, which is a system macro defined in stdio.h
. File read APIs(fgetc
, fread
, fscanf
) will return -1 once it reaches end of file. So in your program you have while(!EOF)
this will be always false, because NOT
of -1
is always 0. -1
will be represented in 2's complement so all bits of that variable will be 1. (If size of int
is 2, -1
will be stored as 0xFFFF
in int
variable).
Use the below sample code.
while(EOF != (c = fgetc(filePtr)))
{
if ((c == ' ') || (c == '\n'))
{
if (i == 0)
{
continue;
}
tempStr[i] = '\0';
i = 0;
//here do your linklist node creation and insertion operation
continue;
}
tempStr[i] = c;
i++;
}
这篇关于从文件中读取单词到简单的链接列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文