【发布时间】:2014-02-08 07:01:45
【问题描述】:
我的项目有问题。我编写了一个函数,它从文件中读取结构,按字母顺序对其进行排序并写回文件。从文件中读取并将其放回原处是可以的,因为我在其他函数中使用相同的代码并且它运行完美。 我的排序有问题,因为使用此功能后 txt 文件为空白。 它正在处理函数之外的结构:
typedef struct baseofwords
{
char *word;
char *category;
struct baseofwords* next;
} base;
这是我的功能:
void SORTING (base **head)
{
char word[30];
char category[20];
FILE *fp;
if ((fp = fopen("baza.txt", "r"))==NULL)
{printf("Error while opening the file!");
exit(EXIT_FAILURE);}
else
{
while(!feof(fp))
{
fscanf(fp,"%s %s \n", word, category);
base *wsk = *head;
base *new = malloc (sizeof(base));
new -> next = NULL;
new -> word = strdup(word);
new -> category = strdup(category);
if(wsk == NULL)
{
new -> next = *head;
*head = new;
}
else
{
while(wsk -> next != NULL)
wsk = wsk -> next;
wsk -> next = new;
}
}
}
fclose(fp);
//==========================================up until here it works, problem must be down there
base *newHead = NULL;
base *wsk1, *wsk2, *tmp;
wsk1 = tmp = *head;
wsk2 = NULL;
while(tmp->next)
{ if (tmp->next->word > wsk1->word)
{ wsk2 = tmp;
wsk1 = tmp->next;
}
tmp = tmp->next;
}
if (wsk2) wsk2->next = wsk1->next;
else *head = wsk1->next;
wsk1->next = newHead;
newHead = wsk1;
*head = newHead;
//======================this part is okay again
if ((fp = fopen("base.txt", "w"))==NULL)
{printf("Error while opening file!");
exit(EXIT_FAILURE);}
else
{base *wsk = *head;
while (wsk->next != NULL)
{fprintf(fp, "%s %s\n", wsk->word, wsk->category);
wsk=wsk->next;}
}fclose(fp);
}
非常感谢您的帮助!
【问题讨论】:
-
这一行如何:base *new = malloc (sizeof(baza)); ?我什至看不到它是如何编译的......
-
对不起!我用我的语言翻译了它。它是基础 *new=malloc (sizeof(base)); ,当然!
-
我认为如果我们查看您正在使用的实际代码会更好,或者甚至更好......如果您在发布之前编译并测试您在此处发布的代码说“它有效”...
-
最好的调试方法是将打印语句放入循环中,告诉你 wsk1、temp 和 wsk2 每次都是什么。我认为这样你会很快找到错误,而不是等待我们中的一些热心人士推断你想要做什么......
-
你错误地使用了feof:stackoverflow.com/questions/5431941/…