-2
프로그램을 실행할 때마다 화면에 "파일 이름을 열 수 없습니다."라는 메시지가 인쇄됩니다. 나는 강의 노트에서 지침과 방법론을 따랐으며 실제로 열리지 않는 이유를 알 수 없었다. 모든 종류의 도움을 많이 주시면 감사하겠습니다.왜 파일을 열지 않고 어떻게 해결할 수 있습니까?
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#define READONLY "r"
int main (void)
{
FILE *ipfile;
char filename[FILENAME_MAX+1];
int *unsorted_details;
int elements;
printf("Enter the name of the input file:\n");
scanf("%s", filename);
if((ipfile=fopen(filename, READONLY)) == NULL){
fprintf(stderr, "Couldn't open %s. \n", filename);
exit(EXIT_FAILURE);
}
if (fscanf(ipfile, "%d", &elements) != 1){
fprintf(stderr, "Couldn't read object details from %s\n",
filename);
exit(EXIT_FAILURE);
}
if ((unsorted_details=(int *)malloc(elements * sizeof(int))) == NULL){
fprintf(stderr, "Failed to allocate memory.\n");
exit (EXIT_FAILURE);
}
/* Reading elements from file into unsorted array*/
int i;
for (i=0; i<elements; i++){
if(!fscanf(ipfile, "%d", &unsorted_details[i])){
fprintf(stderr, "Error reading element %d of the list\n", i+1);
exit (EXIT_FAILURE);
}
}
fclose(ipfile);
free(unsorted_details);
return (EXIT_SUCCESS);
}
파일이 현재 디렉토리에 존재하지 않습니다. –
죄송합니다. 파일 디렉토리가 무슨 뜻인지 잘 모릅니다. 어떻게 수정할 수 있습니까? –
exe 파일이 들어있는 동일한 디렉토리에 파일을 넣고 입력하는 동안 파일의 확장자도 지정하십시오. 한 번 시도해보십시오. – Kishor