G
g.managoli
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
FILE *fptr;
char* buffer = malloc(20);
buffer[0] = '1';
if(argc < 2)
{
fptr = fopen("file.txt", "r");
}
else
{
fptr = fopen(argv[1], "r");
}
if(!fptr)
{
printf("File did not open\n");
return 0;
}
while(fptr) /* Does not seem to work */
{
fgets(buffer, 20, fptr);
printf("Read string: %s\n", buffer);
getchar();
}
return 1;
}
Hi,
I have a problem with the while loop where it checks whether the file
pointer has reached the end of file. This does not seem to work and the
loop executes infinitely. What should be check-clause in the loop?
Also, I have read comments on this group about replacing 'scanf' with
'fgets' and 'sscanf'. Could someone tell me how to do this?
If I am repeating a question that is already in FAQ, kindly point to
it.
Any help is appreciated,
Anjali.
#include <stdlib.h>
int main(int argc, char *argv[])
{
FILE *fptr;
char* buffer = malloc(20);
buffer[0] = '1';
if(argc < 2)
{
fptr = fopen("file.txt", "r");
}
else
{
fptr = fopen(argv[1], "r");
}
if(!fptr)
{
printf("File did not open\n");
return 0;
}
while(fptr) /* Does not seem to work */
{
fgets(buffer, 20, fptr);
printf("Read string: %s\n", buffer);
getchar();
}
return 1;
}
Hi,
I have a problem with the while loop where it checks whether the file
pointer has reached the end of file. This does not seem to work and the
loop executes infinitely. What should be check-clause in the loop?
Also, I have read comments on this group about replacing 'scanf' with
'fgets' and 'sscanf'. Could someone tell me how to do this?
If I am repeating a question that is already in FAQ, kindly point to
it.
Any help is appreciated,
Anjali.