Hi,
I've done many simple procedures, but I'm only trying to read the first word into a char word[30] , from each line of a text file.
I've tried, but without success. Oh, I have to reuse that char each time I read it. (To put in an ordered list each time I read it).
Can anyone help me and show me a way to read this way from a file, in a simple and "cleany" way?
Thank you in advance
FILE *fp;
char word[30];
fp = fopen("/myhome/Desktop/tp0_test.txt", "r");
if (fp == NULL) {
printf("Erro ao abrir ficheiro!\n");
} else {
while (!feof(fp)) {
fscanf(fp,"%*[^\n]%s",word);//not working very well...
printf("word read is: %s\n", word);
strcpy(word,""); //is this correct?
}
}
fclose(fp);
For example for a file that contains:
word1 word5
word2 kkk
word3 1322
word4 synsfsdfs
it prints only this:
word read is: word2
word read is: word3
word read is: word4
word read is: