我已經在Visual C++中編寫了一個簡單的C代碼 我打算打開一個文本文件供閱讀, 「無法打開文件」。 然後我硬編碼程序本身的路徑。還是一樣的錯誤,誰能告訴我我在做什麼worng?或我將不得不復制粘貼該文件,以便我可以通過我的代碼在visual C++中打開它?而不是驅動器名稱後單反斜槓(如「C:\\」)即使在程序中提供了正確的路徑後,我也無法在Visual C++中打開該文件
#include<stdio.h>
#include<conio.h>
int main(){
FILE *p;
char file1[20];
char ch,i;
printf("\nEnter the source file name to be copied:");
gets(file1);
p=fopen(file1,"r"); // I have tried changing it with actual path to the file
if(p==NULL)
{
printf("cannot open %s",file1);getch();
exit(0);
}
while((i=getc(q))!=EOF)
printf("%c",i);
fclose(p);
return 0;
}
嘗試'if(p == NULL){perror(「filename」);出口(EXIT_FAILURE); }' – pmg
您是否嘗試在文件路徑中使用'\\\'或'/'而不是'\'。否則,它可能會將'\'視爲轉義序列? –
對不起,我糾正了錯字,用「file1」也得到了相同的錯誤 – Priyanka