c - Print the 5th letter of each word read from a text file -
i wanted find 5th letter every word after reading text file. not know wrong.
after entering file path, getting pop window reading
read.c
has stopped working.
the code:
#include<stdio.h> #include<conio.h> int main(void){ char fname[30]; char ch[]={'\0'}; int i=0; file *fp; printf("enter file name path\n"); gets(fname); fp=fopen(fname,"r"); if(fp==0) printf("file doesnot exist\n"); else{ printf("file read successfully\n"); do{ ch[i]=getc(fp); if(feof(fp)){ printf("end of file"); break; } else if(ch[i]=='\n'){ putc(ch[4],stdout); } i++; }while(1); fclose(fp); } return 0; }
you can use following snippet. code need know maximum length of line. code prints 5th character on each line on each word if length 5 or more.. hope work you.
#include <stdio.h> #include <stdlib.h> #include <string.h> #define max_line 1000 int main() { char fname[30]; char mycurline[max_line + 1]; char mycurword[max_line + 1]; int index,loop; file *fp; printf("enter file name path\n"); gets(fname); fp=fopen(fname,"r"); if(fp==0) printf("file doesnot exist\n"); else { printf("file read successfully\n"); { if(fgets(mycurline,max_line,fp) != null) { index = 0; for(loop = 0;loop < max_line; loop++) { mycurword[index] = mycurline[loop]; index++; if((mycurline[loop] == ' ') || (mycurline[loop] == '\n')) { mycurword[index] = '\0'; index = 0; if(strlen(mycurword) > 4) { putchar(mycurword[4]); } index = 0; if(mycurline[loop] == '\n') break; } } } if(feof(fp)) { break; } }while(1); fclose(fp); } return 0; }
Comments
Post a Comment