无法读取 C 中的文本文件

Hoa*_*Nam 0 c file linked-list file-handling

我有一个任务来创建一个菜单程序,该程序使用链接列表来存储电话目录列表。这是我的源代码:

int isempty(FILE *in)
{
    return in == NULL;
}
node *makenewnode(item newitem)
{
    node *newnode = (node *) malloc(sizeof(node));
    newnode->info = newitem;
    return newnode;
}
int countlines(FILE *datain)
{
    int lines = 0;
    char string[MAX];
    while (fgets(string, MAX, datain))
        lines++;
    return lines;
}
node *push(node *listin, item newitem)
{
    node *newnode = makenewnode(newitem);
    if(listin == NULL)
    {
        newnode->next = NULL;
        listin = newnode;
    }
    else
    {
        newnode->next = listin;
        listin = newnode;
        head = listin;
    }
    cur = head;
    return head;
}
node *builldbytext(FILE *txt, int maxlines)
{
    int i = 0;
    node *result = (node *) malloc(maxlines*sizeof(node));
    item *temp = (item *) malloc(maxlines*sizeof(item));
    while(i < maxlines)
    {
        fscanf(txt, "%s %d %f %s\n", temp[i].model, &temp[i].memory, &temp[i].screen_size, temp[i].price);
        i++;
    }
    for(i = 0; i< maxlines; i++)
        result = push(result, temp[i]);
    return result;
}
void tranverse(node *listprint, int maxlines)
{
    int i = 0;
    printf("%-20s%-15s%-20s%-30s\n", "Model", "Memory (GB)", "Screen size (Inch)", "Price");
    for(i = 0; i < maxlines; i++)
    {
        item temp = listprint[i].info;
        printf("%-20s%-15d%-20.2f%-30s\n", temp.model, temp.memory, temp.screen_size, temp.price);
    }
}
int main()
{
    FILE *fp = fopen("phoneDB.txt", "r");
    int maxlines = countlines(fp);
    fclose(fp);
    node *phonelist = (node *) malloc(maxlines*5*sizeof(node));
    int choice = 0;
    do
    {
        FILE *fptxt = fopen("phoneDB.txt", "r");
        printf("1. Import from Text\n3. Display List\n8. Quit\n");
        printf("Your choice: ");
        scanf("%d", &choice);
        getchar();
        switch(choice)
        {
            case 1:
                if(isempty(fptxt))
                {
                    printf("This file is empty\n");
                    goto x;
                }
                phonelist = builldbytext(fptxt, maxlines);
                x: break;
            case 3:
                tranverse(head, maxlines);
                break;
        }
    fclose(fptxt);
    }while(choice != 8);
    free(phonelist);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

这是我的文本文件(phoneDB.txt):

Iphone6 12 9.6 2000000
IphoneX 32 12.3 40000000
SamsungA6 16 11.3 1000000
SamsungNote6 16 12.3 12000000
Iphone5 32 9.5 6000000
Iphone5s 32 9.5 7000000
Iphone6 32 9.3 8000000
Iphone6s 32 11.3 8500000
OppoF5 32 9.3 10000000
OppoE6 32 11.3 20000000
OppoReno 16 12.6 20000000
IphoneSXmax 128 11.3 45000000
Huawei4 64 11.3 20000000
NokiaE5 16 8.6 3000000
SamsungGalaxy 32 12.3 6000000
SamsungNote7 32 12.3 8000000
Iphone7s 32 12.3 10000000
Huawei6 16 9.5 15000000
SamsungNote5 16 8.5 12500000
IphoneX 16 12.3 25000000
Iphone7 24 11.5 25100000
Run Code Online (Sandbox Code Playgroud)

我的问题:当我调试我的程序时,我发现我的程序无法读取文本文件。它显示文件为空(函数isempty()返回 true)。但是,当我运行我的程序时,它没有显示任何消息(我在 Windows 10 上使用 MinGW 的 gcc 作为编译器)。而且,由于其不可读性,输出没有显示预期的结果。

当我使用ftell()函数检查指针的位置时,它返回 0。

注意:我已经检查了我的输入文件名,所以错误不存在。我认为它在其他地方。输出:

Model               Memory (GB)    Screen size (Inch)  Price
Iphone7             24             11.50               25100000
                    0              0.00
                    -2147479552    0.00
                    0              0.00
                    0              -0.42               ¨‰
                    917518         0.00                
                    0              0.00
\\Very long after that but I just show a sample of it
Run Code Online (Sandbox Code Playgroud)

Dav*_*ica 5

大部分的困惑来自于你建立清单的笨拙方式。现在把菜单放在一边——这留给你,让我们专注于构建你的链表。对于初学者来说,由于您没有检查输入函数的返回值,因此无法判断您的失败发生在哪里。除非您检查 return ,否则您无法正确使用任何输入函数(或任何对代码的持续操作至关重要的函数)。您也不希望硬编码 Filenames,这就是参数的int main (int argc, char **argv)用途 - 或者您可以将文件名作为用户输入读取。

接下来,由于您的数据在文件的输入行中排列,因此您应该使用面向行的输入函数从文件中读取(例如fgets()或 POSIX getline())这样可以确保每次都使用整行输入未读的内容与使用的格式说明符无关。相反,声明足够大小的字符数组从您的文件保存每个行,然后读成数组fgets(),然后分离成namememsize,和price使用sscanf()(不要忘记检查返回)

虽然您可以将打开FILE*指针发送builldbytext()到读取和构建列表,但下面让我们采用一种简单的方法,在循环中从文件中读取,main()并用每一行的值填充一个临时结构。然后我们可以将指向您的列表的指针的地址以及指向包含数据的临时结构的指针传递给add()函数(您的push())以构建列表。

您没有提供节点定义,因此出于示例目的,我们将使用:

#include <stdio.h>
#include <stdlib.h>

#define NAMSZ  32       /* if you need a constant, #define one (or more) */
#define MAXC 1024

typedef struct node_t {     /* list node */
    char name[NAMSZ];
    double size;
    int mem, price;
    struct node_t *next;
} node_t;
Run Code Online (Sandbox Code Playgroud)

虽然您可以使用正向链接来添加节点,但最终会以与文件中的顺序相反的顺序获得节点。您可以使用指针和指针在相同的 O(1) 时间内使用指针按顺序添加,也可以迭代到末尾并在 O(n) 时间内添加。

在每次迭代的末尾添加节点的简单示例是:

 /** add node at end of list */
node_t *add (node_t **head, node_t *tmp)
{
    node_t **ppn = head,                    /* pointer to pointer to head */
            *pn = *head,                    /* pointer to head */
            *node = malloc (sizeof *node);  /* allocate new node */

    if (!node) {                            /* validate allocation */
        perror ("malloc-node");
        return NULL;
    }
    *node = *tmp;                           /* assign tmp struct values */
    node->next = NULL;                      /* set next pointer NULL */

    while (pn) {                            /* iterate to end of list */ 
        ppn = &pn->next;
        pn = pn->next;
    }

    return *ppn = node;                     /* assign & return new node */
}
Run Code Online (Sandbox Code Playgroud)

注意:通过使用指针到指针,添加第一个或后续节点不需要特殊处理)

一个简单的遍历prn()和一个完成后删除列表中所有节点的函数del_list()可以是:

/** print all nodes in list */
void prn (node_t *l)
{
    if (!l) {
        puts ("list-empty");
        return;
    }
    for (node_t *n = l; n; n = n->next)
        printf ("%-16s %3d %5g %d\n", n->name, n->mem, n->size, n->price);
}

/** delete all nodes in list */
void del_list (node_t *l)
{
    node_t *n = l;
    while (n) {
        node_t *victim = n;
        n = n->next;
        free (victim);
    }
}
Run Code Online (Sandbox Code Playgroud)

最后,将您的文件名作为程序的第一个参数读取,或者stdin在没有提供参数的情况下读取,以填充列表然后遍历并释放所有分配的内存所需的一切可能是:

int main (int argc, char **argv) {

    char buf[MAXC];         /* buffer to hold each line */
    node_t *list = NULL;    /* pointer to list (must initialize NULL) */
    /* use filename provided as 1st argument (stdin by default) */
    FILE *fp = argc > 1 ? fopen (argv[1], "r") : stdin;

    if (!fp) {  /* validate file open for reading */
        perror ("file open failed");
        return 1;
    }

    while (fgets (buf, MAXC, fp)) {     /* read each line from file */
        node_t tmp;                     /* temporary struct to hold values */
        /* if name, mem, size, price successfully parsed from line */
        if (sscanf (buf, "%s %d %lf %d",
                    tmp.name, &tmp.mem, &tmp.size, &tmp.price) == 4)
            if (!(add (&list, &tmp)))   /* add node to list/validate */
                break;
    }
    if (fp != stdin)   /* close file if not stdin */
        fclose (fp);

    prn (list);
    del_list (list);
}
Run Code Online (Sandbox Code Playgroud)

注:is_empty()功能是在简单地通过验证的是,在列表中的第一个节点是不接收该列表中的每个函数处理NULL

示例使用/输出

使用文件中的数据dat/phones.txt,您将收到:

$ ./bin/lls_phones dat/phones.txt
Iphone6           12   9.6 2000000
IphoneX           32  12.3 40000000
SamsungA6         16  11.3 1000000
SamsungNote6      16  12.3 12000000
Iphone5           32   9.5 6000000
Iphone5s          32   9.5 7000000
Iphone6           32   9.3 8000000
Iphone6s          32  11.3 8500000
OppoF5            32   9.3 10000000
OppoE6            32  11.3 20000000
OppoReno          16  12.6 20000000
IphoneSXmax      128  11.3 45000000
Huawei4           64  11.3 20000000
NokiaE5           16   8.6 3000000
SamsungGalaxy     32  12.3 6000000
SamsungNote7      32  12.3 8000000
Iphone7s          32  12.3 10000000
Huawei6           16   9.5 15000000
SamsungNote5      16   8.5 12500000
IphoneX           16  12.3 25000000
Iphone7           24  11.5 25100000
Run Code Online (Sandbox Code Playgroud)

如果您使用正向链接来添加节点,那么它们将以相反的顺序打印。

内存使用/错误检查

在你写的,可动态分配内存的任何代码,你有2个职责关于分配的内存中的任何模块:(1)始终保持一个指针起始地址的内存,因此块,(2),可以释放时,它是没有不再需要。

您必须使用内存错误检查程序来确保您不会尝试访问内存或写入超出/超出分配块的边界,尝试读取或基于未初始化值的条件跳转,最后,确认你释放了你分配的所有内存。

对于Linuxvalgrind是正常的选择。每个平台都有类似的内存检查器。它们都易于使用,只需通过它运行您的程序即可。

$ valgrind ./bin/lls_phones dat/phones.txt
==17133== Memcheck, a memory error detector
==17133== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==17133== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==17133== Command: ./bin/lls_phones dat/phones.txt
==17133==
Iphone6           12   9.6 2000000
IphoneX           32  12.3 40000000
SamsungA6         16  11.3 1000000
SamsungNote6      16  12.3 12000000
Iphone5           32   9.5 6000000
Iphone5s          32   9.5 7000000
Iphone6           32   9.3 8000000
Iphone6s          32  11.3 8500000
OppoF5            32   9.3 10000000
OppoE6            32  11.3 20000000
OppoReno          16  12.6 20000000
IphoneSXmax      128  11.3 45000000
Huawei4           64  11.3 20000000
NokiaE5           16   8.6 3000000
SamsungGalaxy     32  12.3 6000000
SamsungNote7      32  12.3 8000000
Iphone7s          32  12.3 10000000
Huawei6           16   9.5 15000000
SamsungNote5      16   8.5 12500000
IphoneX           16  12.3 25000000
Iphone7           24  11.5 25100000
==17133==
==17133== HEAP SUMMARY:
==17133==     in use at exit: 0 bytes in 0 blocks
==17133==   total heap usage: 24 allocs, 24 frees, 6,848 bytes allocated
==17133==
==17133== All heap blocks were freed -- no leaks are possible
==17133==
==17133== For counts of detected and suppressed errors, rerun with: -v
==17133== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
Run Code Online (Sandbox Code Playgroud)

始终确认您已释放所有分配的内存并且没有内存错误。

仔细检查一下,如果您有任何问题,请告诉我。如果您想更改为前向链接或使用tail指针,请告诉我,我很乐意为您提供进一步的帮助。