skip the first input of a text file in c -
anyone knows how in c language?
input: (input.txt)
2 word word word
output:
file contains: 2 word word are: word word
*i'am new in c programming, , practicing self. have little code, not fixing yet problem. here's code:
#include<stdio.h> int main(){ file* f = fopen("input.txt","r"); char c; int l = 1; if(f == null){ printf("file not found!"); return; } c = getc(f); while(c != eof){ if(c == ' '){ l++; } c = getc(f); } fclose(f); printf("file contain: %d word",l); return 0; }
thanks!
#include<stdio.h> int main(){ file *f = fopen("input.txt","r"); int wc, first_time = 1; char word[128]; fscanf(f, "%d", &wc); printf("file contain: %d word\n", wc); printf("word are: "); fscanf(f, "%*s");//skip while(1==fscanf(f, "%127s", word)){ if(first_time) first_time = !first_time; else printf(" "); printf("%s", word); } printf("\n"); fclose(f); return 0; }
update
int c, wc, first_time = 1; char word[128]; fscanf(f, "%d", &wc); printf("file contain: %d word\n", wc); printf("word are: "); c = 0; while(1==fscanf(f, "%127s", word) && c++ < wc){
Comments
Post a Comment