Why does fprintf not work in my whole C program, but does work by itself in seperate program? What sort of mistake causes this?

Viewed 62

I'm having trouble writing to csv files. My full code contains structures, functions etc. which have nothing to do with writing to my csv. The writing part doesn't actually use any variables, functions, structures that come before it. However it only works when isolated in another program by itself, as shown in the code below.

When this code is copy-pasted into my full program the first csv file is created but does not write anything and the program terminates at the first fprintf(). I am very confused how the rest of my program is affecting it. What sort of mistake would cause this behaviour?

My full program is too long to post here, but this is the part that works only when isolated:

#include <stdio.h>

int main(){
//locations of the csv files
char* stream_u = "./data/u data.csv";
char* stream_v = "./data/v data.csv";
char* stream_v_half = "./data/v half data.csv";

//delete old files
remove(stream_u);
remove(stream_v);
remove(stream_v_half);

FILE *fp_u;
FILE *fp_v;
FILE *fp_v_half;

fp_u = fopen(stream_u, "w");
fp_v = fopen(stream_v, "w");
fp_v_half = fopen(stream_v_half, "w");

for (int j = 0; j < 11; j++){fprintf(fp_u,"%d,",j);} //code terminates here in the full program
fprintf(fp_u,"\n");

for (int j = 0; j < 11; j++){fprintf(fp_v,"%d,",j);} 
fprintf(fp_v,"\n");

for (int j = 0; j < 11; j++){fprintf(fp_v_half,"%d,",j);} 
fprintf(fp_v_half,"\n");

fclose(fp_u);
fclose(fp_v);
fclose(fp_v_half);

printf("finished");

}

I am new to C and would really appreciate any suggestions!

0 Answers
Related