由于文件路径上的字符异常,打开文件时出现问题

我正在从文件中读取某个文件的路径,该文件作为应用程序的参数。 每个路径都写在一行上。我在给我这个错误的最后一条路径上遇到了问题:

Cannot open C:\Users\Utente\Desktop\find\try1.txtl¹v

这是我的代码:

    struct filePath{
        char path[255];
        int fileOccurences;
    };

    struct filePath fPath[2];
    char currentLine[255];
    char path[255];
    char word[30];
    int i,ch;
    int k = 0;
    FILE * fInput = fopen(argv[1],"r"); 
    if(fInput == NULL){ //check sull'apertura del file
        fprintf(stderr,"Cannot open %s,exiting. . .\n",argv[1]);
       exit(1);
    }

    while(!feof(fInput)){
        for (i = 0; (i < (sizeof(path)-1) && ((ch = fgetc(fInput)) != EOF) && (ch != '\n')); i++){
            fPath[k].path[i] = ch;
        }
        FPath[k].path[i] = '\0';
        k = k + 1;
    }
    fclose(fInput);
    for(int j = 0; j<2; j++){
        FILE * fp = fopen(fPath[j].path,"r"); 
        if(fp == NULL){ 
            fprintf(stderr,fPath[j].path);
            exit(1);
        }
    }

我只上传了程序中感兴趣的部分,这不是我确切编写代码的方式。因此,有人知道我该如何解决此问题并取消这些字符“ lv”。谢谢。

sdqzangel 回答:由于文件路径上的字符异常,打开文件时出现问题

您逐个字符地将文件名读入fPath[k].path[i],然后在循环后将path[i]设置为nul。 但是fPath[k].pathpath是不同的变量,因此您没有以空值结尾的fPath[k].path[i]

本文链接:https://www.f2er.com/3155928.html

大家都在问