In: Computer Science
C programming language only!
a file is given that has comma-separated integers. the files contents are -1,-9,1,45,3,2,1,-1...
Create a function that takes as an input a filename and returns an array containing the list of integers in the file
C code:
#include <stdio.h>
#include <stdlib.h> // For exit()
char * display(char fname[])
{
FILE *fileptr;
char c,arr[100];
int i=0;
fileptr = fopen(fname, "r");
if (fileptr == NULL)
{
printf("Cannot open file
\n");
exit(0);
}
c = fgetc(fileptr);
while (c != EOF)
{
arr[i++] = c;
printf("%c",arr[i-1]);
c = fgetc(fileptr);
}
fclose(fileptr);
return arr;
}
int main()
{
char fname[100];
printf("Enter the fname to open \n");
scanf("%s", fname);
char *a=display(fname);
return 0;
}
Execution screenshots: