Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C无法打开文本文件_C_File_Text Files_Token_Fopen - Fatal编程技术网

C无法打开文本文件

C无法打开文本文件,c,file,text-files,token,fopen,C,File,Text Files,Token,Fopen,我试图制作一个程序,告诉你一个文本文件中有多少单词、行和字符,但函数fopen无法打开文件。我尝试了文本文件的绝对路径和相对路径,但得到了相同的输出。你能告诉我怎么了吗 我的编译器是gcc版本4.6.3 Linux #include <stdio.h> #include <string.h> #include <stdlib.h> #define N 256 void tokenize(const char *filename) { FILE *f=

我试图制作一个程序,告诉你一个文本文件中有多少单词、行和字符,但函数fopen无法打开文件。我尝试了文本文件的绝对路径和相对路径,但得到了相同的输出。你能告诉我怎么了吗

我的编译器是gcc版本4.6.3 Linux

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define N 256

void tokenize(const char *filename)
{
    FILE *f=NULL;
    char line[N],*p;
    unsigned long int ch=0,wd=0,ln=0;
    int t;
    f=fopen(filename,"rt");
    if(f==NULL)
    {
        perror("The following error occurred");
        exit(1);
    }
    fgets(line,N,f);
    while(!feof(f))
    {
        ln++;
        p=strtok(line," ");
        while(p!=NULL)
        {
            wd++;
            t=strlen(p);
            ch+=t;
            printf("Word number %lu with length %d: %s\n",wd,t,p);
            p=strtok(NULL," ");
        }
        fgets(line,N,f);
    }
    printf("%lu lines, %lu words, %lu characters\n",ln,wd,ch);
    fclose(f);
}

int main(void)
{
    char filename[80];
    size_t slen;
    printf("Enter filename path:\n");
    fgets(filename,80,stdin);
    slen = strlen (filename);
    if ((slen > 0) && (filename[slen-1] == '\n'))
         filename[slen-1] = '\0';
    printf("You have entered the following path: %s\n",filename);
    tokenize(filename);
    return 0;
}

您保留了文件名中输入的换行符。当你在输出中回响文件名时,你可以看到这一点:注意空白行。 在将换行符传递给函数之前,您需要去掉它。有几种方法可以做到这一点,这里有一种:

size_t idx = strlen(filename);
if ((idx > 0) && filename[idx - 1] == '\n')
    filename[idx - 1] = '\0';

您需要从字符串中删除尾随的换行符,如下所示:

size_t slen = strlen (filename);
if ((slen > 0) && (filename[slen-1] == '\n'))
    filename[slen-1] = '\0';

而且,虽然我对您使用fgets进行用户输入表示赞赏,因为它可以防止缓冲区溢出,但仍有一些边缘情况您没有考虑,例如行太长,或者用户标记输入结束。有关更健壮的解决方案,请参阅。

您可以声明如下函数:

void rmnewline(char *s)
{
int l=strlen(s);
if(l>0 && s[l-1]=='\n')
   s[l-1]='\0';
}
并在使用char数组之前调用它

void rmnewline(char *s)
{
int l=strlen(s);
if(l>0 && s[l-1]=='\n')
   s[l-1]='\0';
}