I have now made some changes.
New code.
Code:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define MAX 10000
#define LITE 100
void str2html(FILE * fp);
char words[MAX];
int main()
{
FILE * fp;
FILE * st;
char readFile[LITE];
char writeFile[LITE];
printf("Enter file to read\n");
scanf("%s", readFile);
printf("Enter file to write to\n");
scanf("%s", writeFile);
if ((fp = fopen(writeFile, "w")) == NULL)
{
fprintf(stderr, "Can't write to file!", writeFile);
exit(1);
}
if ((st = fopen(readFile, "r")) == NULL)
{
fprintf(stderr, "Can't read file!", readFile);
exit(2);
}
fprintf(fp, "<html>\n<head>\n<body bgcolor=""white"">\n</head>\n");
while (fgets(words, MAX, st) != NULL && words[0] != '\0')
fprintf(fp, "%s <br>", words);
fprintf(fp, "\n</body>\n</html>");
str2html(fp);
fclose(st);
fclose(fp);
return 0;
}
void str2html(FILE * fp)
{
int val;
char str[LITE];
char *pdest;
printf("Do you want to make html links? 1 for yes, 2 for no\n");
scanf("%d", &val);
if (val < 1 || val > 2)
{
printf("Enter a value 1 or 2\n");
scanf("%d", &val);
}
while (val == 1)
{
printf("Enter word to make a link to\n");
scanf("%s", &str);
pdest = strstr(words, str);
if (pdest != NULL)
{
char ch[100];
printf("Enter shortcut to the word\n");
scanf("%s", &ch);
fprintf(fp, "<a href=%s%s.html>", ch, str);
fprintf(fp, "%s", str);
fprintf(fp, "</a> ");
}
else
fprintf(stderr, "Error, can't find word\n");
printf("Do you want to continue? 1 for yes, 2 for no\n");
scanf("%d", &val);
if (val < 1 || val > 2)
{
printf("Enter a value 1 or 2\n");
scanf("%d", &val);
}
}
printf("End of program\n");
}
Prelude:
/*
>while (fgets(words, MAX, st) != NULL && words[0] != '\0')
fgets reads the trailing newline as well, so if your file happens to have lines like so:
Line 1\n
Line 2\n
\n
Line 3\n
<eof>
Then if words is not NULL, words[0] will never be '\0'. A better test would check for '\n' instead.
*/
I have tried with '\n' but is doesn¨t behave completly the way as expected, for me is better with '\0'.
I have tested with GCC 3.2 under Windows 2000sp3 and Red Hat 7.3