'C file complied wrongly
I'm currently reading a book 'Introducing Speech and Language Processing'.
In the book lets me know a code that generates 200Hz cosine wave.
Below is the code. (file name is coswave.c)
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
int main() {
int length, status, i;
short int *x;
float freq;
double arg, twopi;
FILE *file_id;
length = 8000;
freq = 0.025;
twopi = 8.0 * atan(1.0);
arg = twopi * freq;
x = (short int *) calloc(length, sizeof(short int));
if (!x) {
printf("Unable to allocate space for samples\n");
exit(1);
}
for (i = 0; i < length; i++)
x[i] = (short int) 32000 * cos(i * arg);
file_id = fopen("cosine.dat", "wb");
if (file_id == NULL) {
printf("Unable to open file\n");
exit(1);
}
status = fwrite(x, sizeof(short int), length, file_id);
if (status < length) {
printf("Unable to write all samples\n");
exit(1);
}
fclose(file_id);
return 0;
}
After writing that code, in the cmd, I typed the below.
gcc -c coswave.c // this command gives me coswave.o file
gcc -o bleep.exe coswave.o // this command gives md bleep.exe file
After that, I executed bleep.exe file and cosine.dat is created.
I opened it with 'hexdump for VSC' extension, but except a first line all contents are filled with 00.
00000000: 00 7D 00 00 00 00 00 00 00 00 00 00 00 00 00 00 .}..............
What is wrong here?
for (i = 0; i < length; i++) {
x[i] = (short int) 32000 * cos(i * arg);
printf("%d %d \n", i, x[i]);
}
According to printf(), There is no problem with cosine values.
My OS is Windows and I used mingw gcc. Thank you very much.
Solution 1:[1]
!!!!!!!!!!!!! I just fixed the problem!!!!@!!@!@!@!@!@!@
I think THE ANSWER is >>>>> reboot <<<<<
in order to try, I installed Visual Studio and it seemed I needed to reboot. At that point I just thought "oh come to think of it I did not try two basic solutions: 1) reboot computer 2) hit computer". After rebooting, I just complied the file and it was so successful!!! But I'm not sure whether rebooting is the key or installing visual studio is the key. Anyways I think rebooting did some job...
Now everything is clear... C doesn't hate me... it just turns out that something was alienating me from C... Now I'm crying with delight...
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Cor |
