我的印象是,要使用字符串打開二進制文件,您可以簡單地創建該字符串,然後將其實現爲將讀取字符串的文件的名稱。這是我的講義所說的。但是我很明顯錯過了一些東西。我已經使用&名稱,名稱,&名稱[SIZE]在fopen內,每次我得到inBinFile == NULL,除非我使用註釋行。我的字符串是正確的。怎麼了?非常感謝幫助。提前致謝。如何使用字符串打開二進制文件?使用C
#include <stdio.h>
#include <stdlib.h>
#define SIZE 25
int frstmenu(void);
int sndmenu(void);
int main()
{
int fmenu, smenu;
char name[SIZE];
FILE *inBinFile;
unsigned char numRead;
fmenu = frstmenu();
if (fmenu !=1 && fmenu !=2)
{
printf("\nIncorrect option\n");
fmenu = frstmenu();
}
if (fmenu == 1)
{
printf("\nEnter the file name: \n");
scanf("%s", &name[SIZE]);
/* printf("filename: %s", &name[SIZE]); */
smenu = sndmenu();
if (smenu !=1 && smenu !=2)
{
printf("\nIncorrect option\n");
smenu = sndmenu();
}
if (smenu == 1)
{
inBinFile = fopen(name, "rb");
/* inBinFile = fopen("stream.grc", "rb"); */
if (inBinFile == NULL)
{
fprintf(stderr, "Error opening %s", &name[SIZE]);
return(-1);
fclose(inBinFile);
}
}
return(0);
}
int frstmenu()
{
float selection;
printf("----Menu----\n");
printf("1 Open a file (supported format: .grc)\n");
printf("2 Exit the program\n");
printf(" Please select an option (1 or 2): ");
scanf("%f", &selection);
return(selection);
}
int sndmenu()
{
int selection;
printf("---Menu---\n");
printf("1 Decode the sequence\n");
printf("2 Exit the program\n");
printf(" Please select an option (1 or 2):\n");
scanf("%i", &selection);
return(selection);
}
**最小**代碼示例,請。 –
'scanf(「%s」,name);'是正確的。添加'printf(「文件名讀取爲%s \ n」,名稱);'就在下面確認。 – fvu