My code export a TXT file which is supposed to be read by a Macro in Excel, but it writes the file name in the wrong way.
The name I assign to the file is something like Lissajous Figure 08-05-21 15-19-04.txt
(where the numbers refers to the current hour, day, month and year) but when running the code the file name is Lissajous Figure 08-05-21 15-19-04
with that unknown caracter before the first letter.
I'll let my code below,
#include <stdio.h>
#include <math.h>
#include <time.h>
#include <string.h>
//SOURCES
//https://austinrepp.com/how-to-write-to-a-csv-file-in-c/
//https://stackoverflow.com/questions/1442116/how-to-get-the-date-and-time-values-in-a-c-program
//https://austinrepp.com/how-to-write-to-a-csv-file-in-c/
float * genUnitCircle();
void genLissajousFigures(float A, float a, float B, float b, float delta);
void genLissajousFigures(float A, float a, float B, float b, float delta){
FILE * fpointer;
char fileName[100];
int i;
float * circle;
time_t timer;
char buffer[26], line[50];
struct tm* tm_info;
time(&timer);
tm_info = localtime(&timer);
strcat(fileName, "Lissajous Figure ");
strftime(buffer, 26, "%d-%m-%y %H-%M-%S", tm_info);
strcat(fileName, buffer);
strcat(fileName, ".txt");
fpointer = fopen(fileName, "w+");
circle = genUnitCircle();
for (i = 0; i <= 100; i++) {
fprintf(fpointer, "%3.4f \n", A * sin(a * circle[i]));
}
for (i = 0; i <= 100; i++) {
fprintf(fpointer, "%3.4f \n ", B * sin(b * circle[i] + delta));
}
}
Your fileName
variable is getting passed to strcat()
, which expects a valid 0-terminated C string as its first argument, before being initialized as one, causing undefined behavior (manifesting in this case as garbage bytes at the beginning of the string). Use
strcpy(fileName, "Lissajous Figure ");
instead.