Reputation: 19
I've been stuck for a while now. The program i'm writing basically changes the false words with the correct ones from the dictionary. However, when i run the program, it gives me no warnings or errors, but it doesn't display anything. Can you please help me?
#include<stdio.h>
#include<ctype.h>
#include<string.h>
int main(void){
char fname[20],word[2500], dictn[50];
int i,j;
float len1, len2;
FILE *inp, *dict, *outp, *fopen();
fpos_t pos1, pos2;
dict= fopen("dictionary.txt", "r");
printf("Enter the path of the file you want to check:\n");
scanf("%s", fname);
inp= fopen(fname, "r");
for(i=0;(fscanf(inp, "%s", word) != EOF); i++){
for(j=0;fscanf(dict, "%s", dictn) != EOF; j++){
fgetpos(inp, &pos1);
fgetpos(dictn, &pos2);
len1=(float)strlen(word);
len2=(float) strlen(dictn);
if(len1<=(0.6*len2)){
fsetpos(dictn, &pos1);
}
if(strncmp(word, dictn, 1)==0){
fsetpos(dictn, &pos1);
}
if(strcmp(word, dictn)==0){
fsetpos(dictn, &pos1);
}
}
printf("%s ", word);
}
fclose(inp);
fclose(dict);
return(0);
}
Upvotes: 0
Views: 164
Reputation: 929
First of all, I'm assuming you have created arrays word
and dictn
with enough size to hold the maximum length string any of your files.
First fault:
In loops you've created, i
represents number of strings in input file and j
represents number of strings in dictionary. word
is your input string variable and dictn
is your dictionary string variable. But you want to retrieve and alter word's ith or dictn's jth character. This may cause an error because there can be a case like this:
Suppose there are 10 words at inp
file and 100 words at dictn
. And in your loops, i
have value of 8 and j
have value of 88. Corresponding these i
and j
values, word has string value of, say, apple
and dictn
has string value of apple
also. So this means apple
is the 8th word at input file and 88th word at dictionary file. And if one of those if conditions was satisfied, compiler tries to apply a statement like word[i]=dictn[j];
. This means word[8] = dictn[88];
for this example. But both of those string have apple
as values which consists only 5 characters! And this will cause an error since you've tried to retrieve 88th character of a 5-length string and assign it to the 8th character of a 5-length string. So your code is wrong, it will only work for some cases which will be a rare situation.
Second fault:
I assume you want to read whole dictionary file for every word in input file but you will be able to read it for only first word of input file since you don't reopen it or set position indicator at the beginning of dictionary file after you read whole dictionary.
Third fault:
Your first if statement will never be reached assuming you have created len1
and len2
variables as integers. Because in your if statement, there is a multiplication of a decimal number and an integer which will return 0 as a result and since fscanf()
ignores whitespaces, len1
and len2
will be at least 1.
Fourth fault:
Also your else if
statement will never be reached because if a string has same value with another, their first character will also be equal to each other and your if statement where you compare their first characters will be also accepted.
Actually, I would write a code as solution but first of all you need to correct things up which are logically wrong because I do not know what you are really try to achieve by your code -just because I commented with full of assumptions-. But I can provide you some guidelines:
len1
and len2
variables from int
to float
and cast values which return from strlen()
functions to float
.dict
file for every iteration of outside loop. (And do not forget not to close it).inp
file, you can use a fpos_t
type of variable to track your position indicator of your inp
file (fgetpos()
to get current position and fsetpos()
to change position with value of fpos_t
variable. You can search them.) and type the word with fprintf()
or fputs()
to that location to change that string.Upvotes: 0
Reputation: 260
You can use
sprintf(word, "%s ", dictn);
If your code is working with printf it should work with sprintf, provided you don't overflow "word", including the NULL termination, so you might have to resize "word" if it is smaller than dictn.
Upvotes: 1