Skip to content

@rawaid /gist:4ebb9ae1c09d443bd44c secret
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
#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.