Hi all,
I am a quite inexperienced C programmer and am trying to write a simple program to add HTML line breaks to a text file. I actually got the program to compile but it gives me weird output.
for example:
Original file:
Code:
Hello World of C Programming.
Altered File:
Code:
(×û¿žÌ
žô·aƒ ProHž@+
ž<br />
Here is my source code:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char buffer1[500];
char* readLine(FILE *fp)
{
int tc, cnt = 0;
char tmp;
while ((tc = fgetc(fp)) != -1)
{
tmp = (char) tc;
if (tmp == '\n')
{
return buffer1;
}
else
{
buffer1[cnt++] = tmp;
}
}
return NULL;
}
int main(int argc, char **argv)
{
FILE *fl;
char *tmp = malloc(sizeof(char) * 500);
char file[50], buffer[5000] = "";
printf("Enter the name of the file you wish to affect> ");
scanf("%s", file);
fl = fopen(file, "r");
while ((tmp = readLine(fl)) != NULL)
{
strcat(buffer, tmp);
strcat(buffer, "<br /> \n");
}
fclose(fl);
fl = fopen(file, "w");
fprintf(fl, buffer);
fclose(fl);
printf("Your file has been altered thanks for using webliner\n");
free(tmp);
return 0;
}
In my mind it is probably a simple little bug but I cant find it.
All help is greatly appreciated.