-2

I'm making a program that takes a standard text file and converts it into an HTML document. for the most part it works fine and I am just improving it's functionality now, but there is this strange thing that happens when I save the title to a string/char arrray to print later. 6 characters, randomly determined by some magical force every time I run the binary get added to the start of the string "title".

Here is the code I believe is the problem:

char title[128]; /*handles the title of the webpage*/

printf("Creating file %s", htmlname);
  /*begins creating the first open-tags, from the doctype to <title>*/
  puttag(html, "!DOCTYPE html");
  puttag(html, "html");
  puttag(html, "head");
  puttag(html, "title");
/*reads the start of txt until it finds a newline; prints all characters it finds along the way*/
ch = fgetc(txt);
  while (ch != '\n')
  {
    fprintf(html, "%c", ch);
    addchar(title, ch);
    ch = fgetc(txt);
  }
/*closes <title> and <head>; opens <body>*/
  closetag(html, "title");
  closetag(html, "head");
  puttag(html, "body");
  /*puts string "title" in <h1> tags*/
  puttag(html, "h1");
  fprintf(html, "%s", title);
  closetag(html, "h1");


/*FUNCTION DEFINITIONS*/
/*puts string "tag" in <> brackets and prints it into fp*/
void puttag(FILE *fp, char *tag)
{
  fprintf(fp, "\n<%s>\n", tag);
}

/*puts string "tag in </> brackets and prints it into fp"*/
void closetag(FILE *fp, char *tag)
{
  fprintf(fp, "\n</%s>\n", tag);
}

/*adds character "ch" to string "str"*/
void addchar(char *str, char ch)
{
  int i = strlen(str);
  str[i] = ch;
  str[i + 1] = '\0';
}




and the output created by this piece of the code looks like this:

<!DOCTYPE html>

<html>

<head>

<title>
This is the title
</title>

</head>

<body>

<h1>
p6+��This is the title
</h1>

I am new to C and have absolutely no idea why this is happening, so I do apologise if this is too much or too little sample to determine the problem.

One thing I have noticed is that this problem only appeared as I was adding later parts of the code to the program, even though I am absolutely sure that they have nothing to do with ths issue. This is the only place that this string is used, and strings are not used at all after this code(apart from when I check to see how long the string is, when It returns 23, 6 characters longer than the actual length of the title). Another thing is, sometimes, possibly the first time that it gets compiled after a restart(?), the program returns the correct string. I haven't investigated this much.

If anyone knows what is going on, help would be greatly appreciated.

Architect
  • 141
  • 2
  • 7
  • 3
    Where is the [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example)? – Blaze Mar 05 '20 at 09:00
  • the error is probably in addchar but without seeing how you define title i couldnt know for sure – dangee1705 Mar 05 '20 at 09:01
  • Ah. I will add the definition of title in. it's ```char title[128]; /*handles the title of the webpage*/```, for quick reference. I have used addchar before without problems before so I'm not sure how it would affect it with my current knowledge. – Architect Mar 05 '20 at 09:09
  • 1
    It looks that you are experienced Undefined Behaviour, probably because of one of the common mistakes: writing in unallocated memory (past end of an array), through an unitialized pointer, or using a dangling pointer. The hard part is that the symptoms are often unrelated to the real cause. How to fix: test separately the different parts of your code, because it is easier to detect a problem in 50 lines than in 500. That is the reason why SO rules ask for a [mcve]: if you can provide one, we will be able to reproduce and fix. Without it, we could only try to guess... – Serge Ballesta Mar 05 '20 at 09:11
  • Working on one now – Architect Mar 05 '20 at 09:15
  • 2
    try `char title[128] = ""`; – 0___________ Mar 05 '20 at 09:24
  • Your title is uninitialized so there's random junk in it before you add the title characters, and then you print the junk and the title. Hence the suggestion `char title[128] = "";` to fix that. And you should protect against overly long titles too. – Jonathan Leffler Mar 05 '20 at 09:32

1 Answers1

1

A simpler way to read in a line of text would be to use fgets. It will overwrite whatever is in title so it doesn't matter if it is uninitialised or not - although it is good practice to make sure all variables are initialised. And it won't overrun the end of your array as you can specify the maximum number of characters to read in.

fgets(title, 128, txt);

The one caveat is that it will also include the \n if the line read in will fit into the specified size. But it is easy enough to check if there is one in the string and remove it like so...

if(strchr(title, '\n'))
    {
    *(strchr(title, '\n'))='\0';
    }

Also rather than using the number 128 everywhere in your code, you could use use a define so that your array and call to fgets are consistent

#define TITLE_SIZE (128)

char title[TITLE_SIZE];
fgets(title, TITLE_SIZE, txt);
if(strchr(title, '\n'))
    {
    *(strchr(title, '\n'))='\0';
    }
Chris Turner
  • 8,082
  • 1
  • 14
  • 18
  • 2
    Remove newlines: [`title[strcspn(title, "\n")] = 0;`](https://stackoverflow.com/questions/2693776/removing-trailing-newline-character-from-fgets-input) – Andrew Henle Mar 05 '20 at 10:20