由于文件路径上的奇怪字符,打开文件时出现问题
Problem while opening a file because of weird characters on the file path
我正在从作为我的应用程序参数提供的文件中读取某个文件的路径。
每条路径都写在一行上。我对最后一条路径有问题,它给我这个错误:
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] = '[=11=]';
k = k + 1;
}
fclose(fInput);
for(int j = 0; j<2; j++){
FILE * fp = fopen(fPath[j].path, "r");
if(fp == NULL){
fprintf(stderr, "Cannot open %s, exiting. . .\n", fPath[j].path);
exit(1);
}
}
我只上传了程序中感兴趣的部分,这不是我编写代码的方式。所以,有人知道我该如何解决这个问题并取消这些字符 "l¹v"。谢谢。
你将文件名逐个字符读入fPath[k].path[i]
,然后在循环后将path[i]
设置为nul。
但是 fPath[k].path
和 path
不是同一个变量,所以你没有空终止 fPath[k].path[i]
我正在从作为我的应用程序参数提供的文件中读取某个文件的路径。 每条路径都写在一行上。我对最后一条路径有问题,它给我这个错误:
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] = '[=11=]';
k = k + 1;
}
fclose(fInput);
for(int j = 0; j<2; j++){
FILE * fp = fopen(fPath[j].path, "r");
if(fp == NULL){
fprintf(stderr, "Cannot open %s, exiting. . .\n", fPath[j].path);
exit(1);
}
}
我只上传了程序中感兴趣的部分,这不是我编写代码的方式。所以,有人知道我该如何解决这个问题并取消这些字符 "l¹v"。谢谢。
你将文件名逐个字符读入fPath[k].path[i]
,然后在循环后将path[i]
设置为nul。
但是 fPath[k].path
和 path
不是同一个变量,所以你没有空终止 fPath[k].path[i]