我试图实现的代码是一种读取 .txt 文件并将字符串转换为节点的方法。本质上,当我阅读 .txt 文件时,我首先检查非字母(单词不能以数字开头,单词的任何索引中也不能有非字母数字)。一旦找到第一个字母,程序就会退出循环并进入另一个循环,直到它看到一个空格。当我成功地造一个词时(找到一个空格时一个词“结束”),我把这个词输入到一个链表中。
当我运行它时,我得到一个总线错误:10。我认为这是由于 word[b] 数组引起的,但是当我 malloc 它时,我仍然得到同样的错误。
提前致谢!
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define TRUE 1
#define FALSE 0
struct Node{
char value[100];
int numOccur;
int numVariance;
struct Node *next;
};
void testprint(struct Node * head){
int data;
data = head->value;
strcpy(data,head->value);
while(head != NULL){
printf("%s\n", data);
head = head->next;
}
}
int main()
{
struct Node *curr;
struct Node *n;
struct Node *head =0;
struct Node *tail =0;
struct Node *next;
char word[100];
int a;
int x;
FILE *file1;
file1 = fopen("test1.txt", "r"); //opens text file
if(file1 == NULL){
fprintf(stderr,"Error: Could not open file"); //if file1 has error, returns error message
exit(1);
}
a = fgetc(file1);
int b = 0;
while(a != EOF){
while(!isalpha(a)){
a = fgetc(file1);
continue;
}
n = (struct Node *) malloc( sizeof( struct Node));
while(isalnum(a)){
while( a != ' ' && a != EOF){
word[b] = a;
a = fgetc(file1);
b++;
}
word[b] = '\0';
}
n->next = 0;
if(head == 0){
head = n;
tail = n;
}
else{
tail->next = n;
tail = n;
}
}
testprint(head);
fclose(file1);
}
最佳答案
您应该注意编译器的警告。 (最好始终使用 -Wall -Wextra
进行编译以获得更多警告。)
正如 Barney Hsiao 所指出的,testprint()
是使用 FILE 指针而不是 Node 指针调用的。
不相关,但如果您需要 true 和 false 常量,有一个标准头文件将为您提供这些(小写,即。true
, false
).
#include <stdbool.h>
我在编译这段代码时收到以下编译器警告:
$ CFLAGS="-Wall -Wextra" make bus10 cc -Wall -Wextra bus10.c -o bus10 bus10.c: In function ‘testprint’: bus10.c:20:14: warning: assignment from incompatible pointer type bus10.c: In function ‘main’: bus10.c:48:8: warning: array subscript has type ‘char’ bus10.c:54:8: warning: array subscript has type ‘char’ bus10.c:68:23: warning: assignment from incompatible pointer type bus10.c:34:8: warning: unused variable ‘x’ bus10.c:31:17: warning: unused variable ‘next’ bus10.c:27:17: warning: unused variable ‘curr’ bus10.c:74:1: warning: control reaches end of non-void function
Now some of these are more serious than others. "array subscript has type 'char'" doesn't sound like a problem to me (to be honest, I don't know what it even means). "unused variable" means just that: you've created (declared) a variable and then you never use it. "control reaches end of non-void function" means you have a function that is declared to return a value but the code falls off the bottom without a return statement.
But "assignment from incompatible pointer type" is bad. The word incompatible is bad. Line 20 reads:
head = head->next;
第 68 行显示:
tail->next = n;
因为
struct Node{
...
struct node *next;
};
看到了吗? *next
不是结构节点
,而是结构节点
。大写/小写计数。
好的。这是一个大的。编译器什么也没说。好吧,公平地说,确实如此。但我们不知道这意味着什么。第 33 行:
char a;
你说的有什么问题吗?这是一种拥有角色的类型,对吧?问题出在这里:
a = fgetc(file1);
你看到了吗?这里怎么样?
while(a != EOF){
EOF 是一个标记值,它太大而不适合 char
。应该是这样的。为了让 fgetc
函数告诉您发生了文件结束条件,而不仅仅是文件中的另一个字节,它必须返回一个字符范围之外的值(0..255 unsigned , -128..127 签名)。 while
条件永远不会失败,因为没有 char
值可以比较等于 EOF。
因此将a
声明为int
,而不是char
。
关于c - 总线错误 : 10 in C while inputing text from . txt 文件进入节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14888052/