Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist
View gist:4ebb9ae1c09d443bd44c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
#define SIZE 20
#define LINE 80
 
// you must change getFile to pointer syntax
FILE *getFile()
{
FILE *fptr;
char fname[32];
 
printf("Enter the name of the input file: \n");
scanf(" %s", fname);
fptr = fopen(fname,"r");
if (fptr == NULL)
{
fprintf(stderr, "Failed to open file %s \n\n", fname);
exit(1);
}
return fptr;
}
 
// You must change all of readline to pointer syntax
int readline(char s[], int max, FILE *fptr)
{ int c, i = 0;
max--;
while ( i < max && (c = fgetc(fptr)) != EOF && c != '\n')
s[i++] = c;
// must put this in to make the array a string
s[i] = '\0';
if (c == EOF)
return 0;
return 1;
}
 
int main()
{
// change the second dimension into dynamic memory
char str1[SIZE][LINE];
int n, i;
FILE *fptr;
 
fptr = getFile();
// it's ok to use the first dimension of str1 as an array
// but the second dimension must be made into dynamic memory
n = readline(&str1[0][0],LINE, fptr);
printf( "String[0]: %s\n", str1[0]);
i = 1;
while (n != 0){
// it's ok to use the first dimension of str1 as an array
// but the second dimension must be made into dynamic memory
n = readline(&str1[i++][0],LINE, fptr);
printf( "String[%d] = %s\n", i-1, str1[i-1]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.