By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,911 Members | 1,239 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,911 IT Pros & Developers. It's quick & easy.

Simple code for reading word by word from a .txt or .doc file

P: 11
#include <stdio.h>
#include <conio.h>

void main() {
FILE *fp;
char a,data[100];//Just taking 100 bytes not more that that.
int i = 0,count = 0;

fp = fopen("C:\\mydoc.txt","r");
// Here my file is stored at C:\ as mydoc.txt
clrscr();
if(fp == NULL) {
printf("ERROR READING FILE\n");
getch();
exit(0);
}
else {
a = fgetc(fp);
while(!feof(fp)) {
if(a == ' ' || a == ' ' || a =='\n') {
data[i] = '\0';
if(i != 0) {
count++;
printf("THE %d NO. WORD IS : %s\n",count,data);
getch();
}
i = 0;
}
else {
data[i] = a;
i++;
}
a = getc(fp);
}
}
data[i] = '\0';
count++;
printf("THE %d NO. WORD IS : %s\n",count,data);
getch();
fclose(fp);
}
May 6 '07 #1
Share this Question
Share on Google+
1 Reply


Expert 10K+
P: 11,448
I moved your article to the 'Forums' section because the code is not correct and
can/will start some discussion which doesn't belong in the 'Articles' section.
Thanks.

kind regards,

Jos
May 6 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.