-
Ah, Thanks Elysia. What did I do wrong now? Won't go through second while loop...
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main (void)
{
char a[21];
char b[50];
char c[99];
int d;
int e;
int f;
FILE *syns;
FILE *thes;
FILE *alp;
syns = fopen ( "syns.txt", "r");
thes = fopen ( "thes", "r");
alp = fopen ( "alp.txt", "w");
printf("How many synonyms?\n");
scanf("%d",&e);
fseek( syns, 0L, SEEK_SET );
while(1==fscanf(syns,"%20s",a)) /*While there are words, take a word from the list*/
{
fseek( thes, 0L, SEEK_SET);/*go to beginning of thesaurus*/
while((1==fscanf(thes,"%49s",b))&&(f==0)) /*While there are words in thesaurus, take one and...*/
{
if (strcmp(a,b)==0) /*...Compare it to the word from the list, if they are the same*/
{
fprintf(alp,"%s- ",a);/*Print the word*/
for(d=0;d<=e;d++)/*Pick amount of synonyms*/
{
fscanf(thes,"%98s",c); /*take current word*/
fprintf(alp,"%s, ",c);/*print it*/
}
fprintf(alp,"\n");/*Goto a newline when finished*/
f=1;
}
}
}
return (0);
}
-
Indent properly, please. And don't do { something, instead do { (new line) something.
Your loops are a mess to read. And rename your variables to better names and stop using fscanf(..., "%s", ...)! Read my signature for information. It's unsafe.
-
I use Dev C++, It doesn't like indenting properly. Suggestions on a better IDE? Also, where do I not have a curly-bracket newline?
-
For the last question: you fixed it.
Now the code is much easier to read.
And Dev-C++ should be able to handle it properly. Many use that and if it didn't indent properly it would be a very poor IDE indeed.
Let's see if I can find errors in that code.
-
Ok, I forgot to do that. Changed "%s" to match the sizes.
-
a, b, c, d, e, f are very very poor variables names. I had to rename them to make sense of your code.
Anyway, I found this:
Code:
for(d=0;d<=e;d++)/*Pick amount of synonyms*/
You probably to start at 1 since otherwise it will pick the number of synonyms entered + 1 (since you go from 0 to the number entered, not 1).
Your loop is also confusing:
Code:
while((1==fscanf(thes,"%49s",b))&&(f==0)) /*While there are words in thesaurus, take one and...*/
If fscanf fails, you probably tried to read beyond the end of file, so why f==0?
Besides that, f is set to 1 if a match is found, so if a match is found and it reaches the end of the file... it won't stop looping because you never set f to 0 again. So get rid of it unless you have a good reason for it to be there.
-
Better? EDIT: Runs fine now... Thanks for all your help!
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main (void)
{
char word[21];
char wordtocheck[50];
char synonym[99];
int picksyns;
int howmanysyns;
int resultscheck;
FILE *syns;
FILE *thes;
FILE *alp;
syns = fopen ( "syns.txt", "r");
thes = fopen ( "thes", "r");
alp = fopen ( "alp.txt", "w");
printf("How many synonyms?\n");
scanf("%d",&howmanysyns);
fseek( syns, 0L, SEEK_SET );
while(1==fscanf(syns,"%20s",word)) /*While there are words, take a word from the list*/
{
resultscheck=0;
fseek( thes, 0L, SEEK_SET);/*go to beginning of thesaurus*/
while((1==fscanf(thes,"%49s",wordtocheck))&&(resultscheck==0)) /*While there are words in thesaurus, take one and...*/
{
if (strcmp(word,wordtocheck)==0) /*...Compare it to the word from the list, if they are the same*/
{
fprintf(alp,"%s- ",word);/*Print the word*/
for(picksyns=1;picksyns<=howmanysyns;picksyns++)/*Pick amount of synonyms*/
{
fscanf(thes,"%98s",synonym); /*take current word*/
fprintf(alp,"%s, ",synonym);/*print it*/
}
fprintf(alp,"\n");/*Goto a newline when finished*/
resultscheck=1;
}
}
}
return (0);
}
-
When you open a file your need to check that everything went well and that the program can continue.
Code:
syns = fopen ( "syns.txt", "r");
if ( syns == NULL )
{
<some kind of error here.>
<some form of dealing with the problem here>
}
-
Thanks! But how should I handle it?
-
Since it's a fixed filename, you can't do anything except exit the program.
If the filename were a command line parameter, you could exit with a message reminding the user of the usage of the program.
If the filename was read in at some prompt within your program, you could ask again.