-
This is the shorter version. W means it's a word file, and D in the filename, means it's a definition.
The example you gave, with tabs (and I believe it will work fine with a space also, although I haven't tried it yet), was the test data. It needs to go into a file named "classify.txt", and be in the same directory as the program (for now).
I'm touching up the definition file part of this, and will post it up in a bit.
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;
//printf("\n Enter a word: "); //lines like this are for debugging only
//scanf("%s", s); (void) getchar();
fp = fopen("classify.txt", "rt");
if(fp==NULL) {
printf("ERROR opening 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(); //printf("\n%s", s);
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);
//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;
}
So aba gets a filename of aW3.txt, and Abe would go in there, as well.
Aardvark would go in aW8.txt, etc.
Test this part out a bit. I haven't done much of that.
-
thanks Sir Adak
ill just study it first.
thanks a lot.
-
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. :D
-
Seems one hell of a way to make a mountain out of a mole-hill to me...
I think the OP needs to make their own code, rather than you impressing everyone with your complicated scanf calls.