I’ve never had so much trouble writing data to files! I’m running GCC from MinGW, because I’m used to using GCC in Linux. I usually use the Linux system calls open(), write(), and read(), but I’m writing a Windows program now and I had trouble using read()/write() in Windows, so I’m just using the standard libraries. Anyway, the problem I’m having is I have no idea how to write to a file! I’ve defined “FILE *” variables, used fopen(), with “r+b”, “wb”, and “w+b”, but I still cannot write to my output file with fwrite() or fprintf(). I don’t know what I’m even doing wrong! Here’s my source:
#include <limits.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#define DEBUG 1
/*** Global functions ***/
double highfreq(double deg);
/*** Global variables ***/
double sin_now;
unsigned int *ptr;
unsigned char *key, *infilename, *outfilename;
FILE *infile, *outfile, *keyfile;
const char *pipe_name="[pipe]";
int main(int argc, char *argv[]) {
unsigned int x, y, z;
if(argc!=3) {
fprintf(stderr, "Syntax error: %s <infile.txt> <outfile.wav>", argv[0]);
return 1;
}
if(argv[1][0]=='-') {
infile=stdin;
infilename=(unsigned char *)pipe_name;
}
else {
infilename=argv[1];
if((infile=fopen(infilename, "rb"))==NULL) {
fprintf(stderr, "Could not open input file for modulation.\n", infile);
return 2;
}
}
if(argv[2][0]=='-') {
outfile=stdout;
outfilename=(unsigned char *)pipe_name;
}
else {
outfilename=argv[2];
if((infile=fopen(outfilename, "wb"))==NULL) {
fprintf(stderr, "Could not open/create output file for modulation.\n", outfile);
return 3;
}
}
if(DEBUG) printf("Input file:\t%s\nOutput file:\t%s\n", infilename, outfilename);
fprintf(outfile, "Why won't this work!?\n");
fclose(infile);
fclose(outfile);
return 0;
}
double highfreq(double deg) {
double conv, rad;
conv=M_PI/180;
rad=deg*conv;
return sin(rad);
}
I’m eventually going to make a WAV file as output, hence the “highfreq()” function, but for now I can’t even get it to write to a file! fprintf() returns with an error value of -1, if that helps anyone. I don’t really understand, though because from what I read, this simply indicates there was an error, but nothing more.
That’s the second time in your code you assign the result of
fopentoinfile. You probably wantedoutfilethere.