Please refer to the code below.
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#define MAX 100
int main()
{
char* n[5];
char* name;
FILE* fp = NULL;
int cnt = 0;
fp = fopen("Word.txt", "r");
if (fp == NULL)
{
fprintf(stderr, "File Open Error!\n");
exit(1);
}
for (int i = 0; i < 5; i++)
{
name = (char*)malloc(sizeof(char) * MAX);
fgets(name, MAX, fp);
n[i] = name;
printf("%s", name);
}
printf("\n\n");
for (int i = 0; i < 5; i++)
printf("%s", n[i]);
for (int i = 0; i < 5; i++)
free(n[i]);
fclose(fp);
return 0;
}
Run Results
© 2024 OneMinuteCode. All rights reserved.