I don't like working files like this, but you can't have several score of files open at the same time, either.
First: this needs to be in it's OWN directory, with no ?Wn.txt or ?Dn.txt, where ? is any letter, and n is any number. If a previous run's files are there, delete them first, then run this.
I slowed it with a couple of very short pauses, to let the hardware handle the file work, with better reliability (imo).
This program should be run just ONCE (once you know it's accurate), on the data. Any other words that need to be added, should be done by either another function, or by hand, with cut and paste, on the correct word and define file.
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX 30
int main() {
int i, len;
char n, s1[MAX], def1[80]={""};
char file1[9] = {""}, appen[5]={".txt"}, tmp[9]={""};
FILE *fp, *fpW, *fpD;
//printf("\n Enter a word: ");
//scanf("%s", s); (void) getchar();
fp = fopen("classify.txt", "rt");
if(fp==NULL) {
printf("ERROR opening classify.txt file - terminating");
return 1;
}
i=0;
while(1) {
i = fscanf(fp, "%s%*c%[^\n]s", s1, def1);
if(i < 2)
break;
printf("\n%s %s", s1, def1);
//getch();
len=strlen(s1);
file1[0]=s1[0];
file1[1]='W';
itoa(len, tmp, 10);
strcat(file1+2, tmp);
strcat(file1, appen);
printf("\nFilename is: %s",file1);
fpW = fopen(file1, "at");
if(fpW == NULL) {
printf("Error opening %s - terminating", file1);
return 1;
}
fprintf(fpW, "%s\n", s1);
fclose(fpW);
delay(50);
file1[1]='D';
fpD = fopen(file1, "at");
if(fpD == NULL) {
printf("Error opening %s - terminating", file1);
return 1;
}
fprintf(fpD, "%s\n", def1);
fclose(fpD);
delay(50);
//getch();
//reset all the string arrays to 0
for(i=0;i<9;i++) {
file1[i]='\0';
tmp[i]='\0';
}
for(i=0;i<MAX;i++)
s1[i] = '\0';
for(i=0;i<80;i++)
def1[i]='\0';
}
fclose(fp);
printf("\n\n\t\t\t press enter when ready");
(void) getchar();
return 0;
}
It's late, and I'm nodding off. I'll check in with you, after I get some zzz's.
This is the program that will actually write the respective files, so check it out. You are my beta tester.