This question is an exact duplicate of:
- Read text from file skipping any spaces or empty lines 1 answer
Hi. I need to read several files without empty lines between words.They can have different layouts, such as 1.txt or 2.txt:
1.txt:
treg
hreger
ig
srg
fre
ig
lre
eg
2.txt:
lregreg
igregr
kreggerg
ereherh
tershs
hsehsteh
asreh
treshse
How do i do that ? How can I count the number of words in the fastest way? I just have the following code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
FILE *fp;
char palavra[50]="/0";
char *s;
fp = fopen("1.txt","r");
if(fp == NULL){
puts("Open file failed\n");
exit(-1);
}
while(fscanf(fp,"%s",palavra)!=EOF){
s=palavra;
/*do things with s var*/
}
fclose(fp);
exit(0);
}
how i implement something like that:
while ((c = fgetc(fp)) != EOF) {
if (isspace(c)){
continue;
}