从文件打印字符串时遇到困难
我的任务是从文件中读取单词并分别打印每个单词。 到目前为止,我得到了这个:
#define MAX 100000
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct node *ptr;
typedef struct node {
char *data;
ptr next;
} node;
ptr head = NULL;
void add(char *data) {
node *new_node = malloc(sizeof(node));
new_node->data = data;
new_node->next = head;
head = new_node;
printf("Adding new word:%s\n",data);
}
void print_list() {
node *current = head;
while (current != NULL) {
printf("%s\n", current->data);
current = current->next;
}
}
void free_list(ptr *hnode)
{
ptr p;
while(*hnode){
p = *hnode;
*hnode = (*hnode)->next;
free(p);
}
}
int main(int argc, char *argv[]) {
FILE *file;
char buffer[MAX];
char ch;
int i = 0;
if (argc != 2) {
printf("Usage: %s <file>\n", argv[0]);
exit(EXIT_FAILURE);
}
file = fopen(argv[1], "r");
if (file == NULL) {
printf("Error opening file\n");
exit(EXIT_FAILURE);
}
while(i < MAX-1 && !feof(file))
{
if((ch = fgetc(file)) == ' '|| ch == '\n')
{
buffer[i] = '\0';
add(buffer);
i = 0;
}
else{
if(ch == EOF)
break;
buffer[i++] = ch;
}
}
print_list();
free_list(&head);
exit(EXIT_SUCCESS);
}
当我尝试运行代码时,没有汇编或运行时错误,但是我只会得到最后一个单词和一个奇怪的符号,例如:
Adding new word:Hello
Adding new word:World!
Adding new word:Does
Adding new word:my
Adding new word:code
Adding new word:work?
work?
work?
work?
work?
work?
work?
添加新单词正确并正确读取文件时的部分,我的问题是打印部分。 编辑:现在我有了适当的EOF检查,仍然无法按预期工作。
I have a task to read words from a file and print each word separately.
So far I got this:
#define MAX 100000
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct node *ptr;
typedef struct node {
char *data;
ptr next;
} node;
ptr head = NULL;
void add(char *data) {
node *new_node = malloc(sizeof(node));
new_node->data = data;
new_node->next = head;
head = new_node;
printf("Adding new word:%s\n",data);
}
void print_list() {
node *current = head;
while (current != NULL) {
printf("%s\n", current->data);
current = current->next;
}
}
void free_list(ptr *hnode)
{
ptr p;
while(*hnode){
p = *hnode;
*hnode = (*hnode)->next;
free(p);
}
}
int main(int argc, char *argv[]) {
FILE *file;
char buffer[MAX];
char ch;
int i = 0;
if (argc != 2) {
printf("Usage: %s <file>\n", argv[0]);
exit(EXIT_FAILURE);
}
file = fopen(argv[1], "r");
if (file == NULL) {
printf("Error opening file\n");
exit(EXIT_FAILURE);
}
while(i < MAX-1 && !feof(file))
{
if((ch = fgetc(file)) == ' '|| ch == '\n')
{
buffer[i] = '\0';
add(buffer);
i = 0;
}
else{
if(ch == EOF)
break;
buffer[i++] = ch;
}
}
print_list();
free_list(&head);
exit(EXIT_SUCCESS);
}
When I try to run the code there are no compilations or runtime errors but I get only the last word and a weird symbol, for example:
Adding new word:Hello
Adding new word:World!
Adding new word:Does
Adding new word:my
Adding new word:code
Adding new word:work?
work?
work?
work?
work?
work?
work?
The part when it says adding new word is correct and reads the file properly, my problem is the printing part.
Edit: Now I have a proper EOF check, still doesn't work as intended.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您调用
添加(Buffer)
时,您每次都会传递完全相同的指针(从技术上讲,数组不是指针,但对于此讨论而言足够接近)。该指针被分配给节点的数据
字段。这意味着每个节点都包含对完全相同的字符串的引用。这就是为什么每个节点都打印出相同的值的原因。此外,随着您的代码变得更加复杂,如果buffer
的终生终止,则可能会冒着这些参考文献无效的风险。您要做的是使用
malloc
给出每个节点自己的字符串副本。When you call
add(buffer)
, you're passing the exact same pointer every time (technically, arrays aren't pointers but it's close enough for this discussion). That pointer gets assigned to thedata
field of your nodes. This means that every node contains a reference to the exact same string. That's why every node is printing out the same value. In addition, as your code becomes more complex, you run the risk of these references becoming invalid ifbuffer
's lifetime ever expires.What you want to do is use
malloc
to give each node its own copy of the string.在函数中,添加所有节点的数据成员数据指向MAIM中声明的同一数组缓冲区,
因此函数print_list将输出最后存储在缓冲区中的内容。
您需要动态地将传递的字符串的副本制作。
同时,循环
并没有很大的意义。首先,变量
CH
应声明为具有int
的类型。循环中的条件可能会导致将eof存储在缓冲区中。输出证明了这一点。
的问题中的此更新
(编辑:您回答后进行
。)另外,按一个角色读取字符串效率低下。您应该使用函数
fscanf
而不是fgetc
。注意定义全局变量
head
以及某些功能取决于全局变量是一个坏主意。
同样,在程序的某些部分中,您正在使用typedef名称
ptr
,而在程序的其他部分中,则使用typenode *
。这仅使代码的读者感到困惑。In the function add data members data of all nodes point to the same array buffer declared in main
So the function print_list will output what is last stored in the buffer.
You need to make dynamically a copy of the passed string.
Also this while loop
does not make a great sense. First of all the variable
ch
shall be declared as having the typeint
. The condition in the loopcan result in storing the value EOF in the buffer. And the output demonstrates this.
(Edit: this update in your question
you made after my answer.)
Also reading strings by one character is inefficient. You should use the function
fscanf
instead offgetc
.Pay attention to that it is a bad idea to define the global variable
head
and when some functions depend on the global variable.
Also in some parts of the program you are using the typedef name
ptr
while in other parts of the program you are using the typenode *
. This only confuses readers of the code.