我的程序不会打开文件的内容 [英] my program won't open the contents of a file

查看:88
本文介绍了我的程序不会打开文件的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我要求提供文本文件后,为什么我的程序不会继续?一旦我运行该程序,由于某种原因,我似乎没有找到该文件,也没有输出该文件的内容。

可以做些什么来解决这个问题。



我正在尝试输入带有一串路径的文本文件。然后从那里我解析这个字符串并将其放到链接列表中。之后再创建一个搜索功能。









输出:



输入文本文件

警告:此程序使用gets(),这是不安全的。

a1。 txt





文本文件:path.txt



a / a1。 txt

a / a2.txt

a / b / b3.txt

a / b / b4.txt

a / c / c4.txt

a / c / c5.txt

a / c / d / d6.txt

a / c / d / g

a / c / d / h

a / c / e / i / i7.txt

a / c / f / j / k / k8。 txt









代码:



Why won''t my program continue after I ask for the text file? Once I run the program, for some reason I doesn''t seem as if it looks for the file, nor does it output the contents of the file.
what can be done to fix this.

I''m trying to input a text file with a string of paths. And then from there I parse through this string and put it to a link list. and then create a search function afterwards.




output:

enter text file
warning: this program uses gets(), which is unsafe.
a1.txt


text file: path.txt

a/a1.txt
a/a2.txt
a/b/b3.txt
a/b/b4.txt
a/c/c4.txt
a/c/c5.txt
a/c/d/d6.txt
a/c/d/g
a/c/d/h
a/c/e/i/i7.txt
a/c/f/j/k/k8.txt




code:

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

typedef struct sMyPath{
        char *element;
        struct sMyPath *next;
} tMyPath;


int main(void)
{
        FILE *pFile;
        pFile = fopen("path.txt", "r");
        char inputstr[1024];
        tMyPath *curr, *first = NULL, *last = NULL;

//get the text file, and put it into a string inputstr

        if (pFile != NULL)
        {
                while(!feof(pFile))
                {
                        fgets(inputstr, sizeof(inputstr), pFile);
                }
        fclose(pFile);
        }
        else
        {
                printf("Could not open the file.\n");
        }

//using tokens to get each piece of the string
//seperate directories and text files, put it into a link list

        char *token = strtok(inputstr, "\\");
        while (token != NULL)
        {
        if(last == NULL){
                //creating node for directory
                first = last = malloc (sizeof (*first));
                first -> element = strdup (token);
                first -> next = NULL;
        } else {
                last -> next = malloc (sizeof (*last));
                last = last -> next;
                last -> element = strdup (token);
                last -> next = NULL;
        }
        token = strtok(NULL, "\\");
        }




//ask user for txt file
        char pathU[20];
        printf("enter text file\n");
        gets(pathU);


//check if text file exist, if yes output entires in text file, else say no
        while(first != NULL)
        {
                if(strcmp (first -> element,pathU)==0)
                {
                        FILE *nFile;
                        char texxt[300];
                        nFile = fopen(pathU, "r");
                        while (!feof(nFile))
                        {
                                fgets(texxt, 300, nFile);
                                printf("Theses are the contents\n");
                                printf("%s", texxt);
                        }

                }

                else if(first == NULL)
                {
                        printf("invalid file name\n");
                }

                else
                {
                first = first -> next;
                }




        }

return 0;
}

推荐答案

这段短代码存在大量问题(甚至没有提到有非常奇怪的命名标准。)



1)我不确定它是否应该编译。一个体面的编译器不会让你做 first = last = malloc(sizeof(* first)); 因为你试图从 void * 到 sMyPath *

替换为: first = last =(sMyPath *)malloc(sizeof(* first));

或者更好的是,这个: first = last = new sMyPath;

(虽然后来可能是C ++的东西?我对C的一些差异略显粗略。



2)注意到 curr 从未使用过。删除它。



3)你正在使用几个不安全的功能。如果这至少适用于Windows(VC ++),请确保使用安全版本替换它们: fopen_s gets_s strtok_s 而不是 fopen 分别获得 strtok 。理想情况下完全避免使用strtok_s。我甚至不确定你要用它做什么,因为你没有传递它甚至在你的文件中的分隔符。



4)你正在循环直到文件结束调用fgets,并且每次都将它放入同一个缓冲区

fgets从当前流位置读取字符到并包括第一个换行符,到流的末尾,或直到读取的字符数等于n - 1,以先到者为准

因此,当您到达文件末尾时, inputstr 仅包含文件的最后一行。 (在您的示例中a / c / f / j / k / k8.txt \ n - 包含换行符!)



5)你应该检查fgets返回的NULL,然后检查是否意味着有错误(使用ferror)或文件结束(使用feof)



6)这意味着您最终只得到一个 sMyPath 对象。 first = last,其中next为NULL,元素包含 a / c / f / j / k / k8.txt \ n 。 (再次包括换行符。)



7)因此,你永远不能输入链表中存在的路径,因为即使你输入a / c / f / j / k / k8.txt它也不会匹配你所拥有的唯一条目,因为你无法输入换行符。

因此你永远无法在链接列表中找到该文件,因此该程序只是存在因为它无关。



8)不知道为什么在检查之后,如果 first == NULL 进行测试(在while循环中),并使用指针访问元素成员!



9)你永远不会释放你分配的所有内存。



总结:

a)你的主要内容(逻辑)问题是你应该删除换行符并在读取每一行时创建链表项。

b) 你需要做一些认真的返工



问候,

Ian。
There is a surprisingly large number of issues with this short piece of code (without even mentioning having very strange naming standards).

1) I''m not sure it should even compile. A decent compiler won''t let you do "first = last = malloc (sizeof (*first));" because you''re trying to implicitly convert from void* to sMyPath*.
Replace it with this: "first = last = (sMyPath*)malloc (sizeof (*first));"
or, better still, this: "first = last = new sMyPath;"
(although perhaps the later is a C++ thing? I''m a bit sketchy on some of the differences with C)

2) Noted that "curr" is never used. Remove it.

3) You are using several unsafe functions. If this is for Windows at least (VC++), make sure to replace them with the secure versions: fopen_s, gets_s, and strtok_s instead of fopen, gets, and strtok respectively. Ideally avoid strtok_s completely. I''m not even sure what you''re trying to do with it since you''re not passing it a delimiter that is even in your file.

4) You are looping until end of file calling fgets, and putting it into the same buffer each time.
"fgets reads characters from the current stream position to and including the first newline character, to the end of the stream, or until the number of characters read is equal to n – 1, whichever comes first."
Thus when you reach the end of file, inputstr contains only the last line of the file. (In your example "a/c/f/j/k/k8.txt\n" - the newline is included!)

5) You should be checking for NULL returned by fgets anyway, and then check if it means there was an error (using ferror) or the end of file was reached (using feof)

6) This means that you end up with only one "sMyPath" object. first = last, where next is NULL and element contains "a/c/f/j/k/k8.txt\n". (again the newline is included).

7) As a result, you can never enter a path which exists in your linked list, because even if you enter "a/c/f/j/k/k8.txt" it won''t match the only entry you have because you cannot enter the newline character.
Hence you can never find the file in your linked list, and so the program simply exists as it has nothing to do.

8) Not sure why your testing if "first == NULL" after having checked it isn''t (in the while loop), and using the pointer to access the element member!

9) You are never freeing all the memory you have allocated.

In summary:
a) Your main (logical) issue is that you should be removing the newline character and creating the linked list entries as you read each line in.
b) You need to do some serious rework.

Regards,
Ian.


这篇关于我的程序不会打开文件的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆