Hi,
I'm completely new to C (I'm more of a PHP guy), so I hope someone can help me out with what I want to achieve.
I want to loop through a TXT file, such as:
apple
pear
banana
lemon
And do something with each string; output it with its length.
:-)
Thanks,
Jay
P.S. I'm using gcc to compile it on a Linux machine (via SSH) which has CentOS 4.5 as the OS.
#include <stdio.h>
#include <string.h>
int main()
{
FILE *fp;
char str[255];
// if file opening in binary mode fails
if ( (fp = fopen("myfile.txt", "r")) == NULL )
{
printf("Error opening file");
// 1: EXIT_FAILURE
return 1;
}
// opening file is successful
else
{
while ( !feof(fp) )
{
fscanf(fp, "%s", str);
printf("%s read and its length is %d\n", str, strlen(str));
}
fclose(fp);
return 0;
}
}
hongjun