해결된 질문
작성
·
242
0
안녕하세요.
c6385와 c6386warning이 뜨는 이유가 궁금합니다. 동적할당하고 배열 index도 잘 참조한거 같은데 warning이 안 지워지더라고요..ㅠ 오류 처리 때문인지 교수님 오류처리 코드도 다 따라 쳐 보았지만 소용이 없네요.
#include <stdio.h>
#include <stdlib.h>
#include <String.h>
#define LENGTH 100
struct movieinfo {
char subj[LENGTH];
double score;
};
void manual(void);
void print_all_items(struct movieinfo* msptr, int* num);
void print_an_item(struct movieinfo* msptr, int* num);
void edit_an_item(struct movieinfo* msptr, int* num);
void add_an_item(struct movieinfo** mmsptr, int* num);
void insert_an_item(struct movieinfo** mmsptr, int* num);
void delete_an_item(struct movieinfo** mmsptr, int* num);
void delete_all_items(struct movieinfo* msptr, int* num);
void save_file(struct movieinfo* msptr, int* num);
void search_by_name(struct movieinfo* msptr, int* num);
int main()
{
int input;
printf("Please input filename to read press Enter.\n>> ");
char fname[LENGTH] = { 0, };
scanf("%s", fname);
char a = getchar();
FILE* fmovie = fopen(fname, "r");
if (!fmovie)
{
perror("ERROR: cannot open file.");
exit(1);
}
puts("6 items have been read from the file.\n");
int num;
if (fscanf(fmovie, "%d%*c", &num) != 1) {
printf("ERROR: Wrong file format.");
exit(1);
}
struct movieinfo* msptr = (struct movieinfo*)malloc(sizeof(struct movieinfo) * num);
if (msptr == NULL)
{
printf("malloc failed. \n");
exit(1);
}
for (int i = 0; i < num; i++)
{
if(fscanf(fmovie, "%[^\n]%*c", msptr[i].subj) != 1 ||
fscanf(fmovie, "%lf%*c", &msptr[i].score) != 1)
{
printf("ERROR: Wrong file format.\n");
exit(1);
}
}
while (1)
{
manual();
scanf("%d%*c", &input);
switch (input)
{
case 1:
print_all_items(msptr, &num);
break;
case 2:
print_an_item(msptr, &num);
break;
case 3:
edit_an_item(msptr, &num);
break;
case 4:
add_an_item(&msptr, &num);
break;
case 5:
insert_an_item(&msptr, &num);
break;
case 6:
delete_an_item(&msptr, &num);
break;
case 7:
delete_all_items(msptr, &num);
break;
case 8:
save_file(msptr, &num);
break;
case 9:
search_by_name(msptr, &num);
break;
case 10:
puts("Good bye");
free(msptr);
fclose(fmovie);
return 0;
default:
printf("Wrong input.\n");
break;
}
}
free(msptr);
fclose(fmovie);
return 0;
}
void manual(void)
{
printf("Please select an option and press enter.\n");
printf("1. print all items\t2. print an item\n");
printf("3. Edit an item \t4. Add an item\n");
printf("5. Insert an item\t6. Delete an item\n");
printf("7. Delete all items\t8. Save file\n");
printf("9. Search by name\t10. Quit\n>> ");
}
void print_all_items(struct movieinfo* msptr,int *num)
{
for (int i = 0; i < *num; i++)
{
printf("%i : \"%s\". %.1f\n",i, msptr[i].subj, msptr[i].score);
}
}
void print_an_item(struct movieinfo* msptr, int *num)
{
printf("Input the index of item to print.\n>> ");
int input;
scanf("%d%*c", &input);
if (input<0 || input>*num)
printf("invalid item\n");
else
printf("%i : \"%s\". %.1f\n", input, msptr[input].subj, msptr[input].score);
}
void edit_an_item(struct movieinfo* msptr, int* num)
{
printf("Input the index of item to print.\n>> ");
int input;
scanf("%d%*c", &input);
if (input<0 || input>*num)
printf("invalid item\n");
else
printf("%i : \"%s\". %.1f\n", input, msptr[input].subj, msptr[input].score);
printf("Input new title and press enter.\n>> ");
scanf("%[^\n]%*c", msptr[input].subj);
printf("Input new rating and press enter.\n>> ");
scanf("%lf%*c", &msptr[input].score);
printf("%i : \"%s\". %.1f\n", input, msptr[input].subj, msptr[input].score);
}
void add_an_item(struct movieinfo** mmsptr, int* num)
{
int flag;
*num = *num + 1;
struct movieinfo* msptr = (struct movieinfo*)malloc(sizeof(struct movieinfo) * *num);
if (msptr == NULL)
{
printf("malloc failed. \n");
exit(1);
}
for (int i = 0; i < *num - 1; i++)
{
strcpy(msptr[i].subj, (*mmsptr)[i].subj);
msptr[i].score = (*mmsptr)[i].score;
}
printf("Input title and press enter.\n>> ");
flag = scanf("%[^\n]%*c", msptr[*num - 1].subj);
printf("Input rating and press enter.\n>> ");
flag = scanf("%lf%*c", &msptr[*num - 1].score);
printf("%i : \"%s\". %.1f\n", *num - 1, msptr[*num - 1].subj, msptr[*num - 1].score);
free(*mmsptr);
*mmsptr = msptr;
}
void insert_an_item(struct movieinfo** mmsptr, int* num)
{
printf("Input the index of item to insert.\n>> ");
int input;
scanf("%d%*c", &input);
*num = *num + 1;
struct movieinfo* msptr = (struct movieinfo*)malloc(sizeof(struct movieinfo) * *num);
if (msptr == NULL)
{
printf("malloc failed. \n");
exit(1);
}
for (int i = 0; i < *num - 1; i++)
{
strcpy(msptr[i].subj, (*mmsptr)[i].subj);
msptr[i].score = (*mmsptr)[i].score;
}
int imp = *num;
for (; imp - 1 > input; imp--)
{
strcpy(msptr[imp - 1].subj, msptr[imp - 2].subj);
msptr[imp - 1] = msptr[imp - 2];
}
printf("Input title and press enter.\n>> ");
scanf("%[^\n]%*c", msptr[input].subj);
printf("Input rating and press enter.\n>> ");
scanf("%lf%*c", &msptr[input].score);
printf("%i : \"%s\". %.1f\n", input, msptr[input].subj, msptr[input].score);
free(*mmsptr);
*mmsptr = msptr;
}
void delete_an_item(struct movieinfo** mmsptr, int* num)
{
printf("Input the index of item to delete.\n>> ");
int input;
scanf("%d%*c", &input);
*num = *num - 1;
struct movieinfo* msptr = (struct movieinfo*)malloc(sizeof(struct movieinfo) * *num);
if (msptr == NULL)
{
printf("malloc failed. \n");
exit(1);
}
int imp = input;
for (; input < *num; input++)
{
strcpy((*mmsptr)[input].subj, (*mmsptr)[input + 1].subj);
(*mmsptr)[input].score = (*mmsptr)[input + 1].score;
}
for (int i = 0; i < *num; i++)
{
strcpy(msptr[i].subj, (*mmsptr)[i].subj);
msptr[i].score = (*mmsptr)[i].score;
}
free(*mmsptr);
*mmsptr = msptr;
}
void delete_all_items(struct movieinfo* msptr, int* num)
{
*num = 0;
msptr = NULL;
}
void save_file(struct movieinfo* msptr, int* num)
{
printf("Please input filename to write and press Enter.\n>> ");
char fname[LENGTH] = { 0, };
scanf("%s", fname);
FILE* fmovie = fopen(fname, "w");
fprintf(fmovie, "%d\n", *num);
for (int i = 0; i < *num; i++)
{
fprintf(fmovie, "%s\n%.1lf\n", msptr[i].subj, msptr[i].score);
}
printf("%d items have been saved to the file.\n",*num);
fclose(fmovie);
}
void search_by_name(struct movieinfo* msptr, int* num)
{
printf("Input title to search and press enter.\n>> ");
char strcm[LENGTH] = { 0, };
scanf("%[^\n]%*c", strcm);
int i;
for (i = 0; i < *num; i++)
{
if (strcmp(strcm, msptr[i].subj) == 0)
break;
}
if (i == *num)
printf("No movie found : %s\n", strcm);
else
printf("%i : \"%s\". %.1f\n", i, msptr[i].subj, msptr[i].score);
}
답변 1
1
안녕하세요!
질문이 거의 올라오지 않는 강의라 확인이 늦었네요 ㅠㅠ 죄송합니다.
근데 저는 질문자님 코드 그대로 복사해서 제 VS 에 붙여넣기 한 후 실행해보았는데
아무 워닝도 뜨지 않습니다..!
그리고 에러가 아닌 워닝일 뿐이라서 지우려고 크게 신경 안 쓰셔도 될 것 같아요!
신경써라! 하는 경고 정도일 뿐이라 실행하는데 문제 없습니다.
동적 할당 받은 영역에 대한 사용은 컴파일 타임에서 영역 벗어나는 예외에 대해 검사할 수가 없기 때문에 이런 저런 경고가 좀 붙기도 하더라구요!