-
GNU Compiling Question
I have this program that compiles and runs in microsoft C++ but when I try to compile with GNU either no output is produced or the output file is put somewhere and I don't know where it is! I am using the gcc command to compile. Should that put the output file in the same directory as my .c file? Or is my problem of a different nature?
-
First off, thank you very much for the help!
There is an fprintf in the .c file. So it should be printing to that file right? Also, if I open the a.out file there's a bunch of unreadable stuff in the file.
Code:
#include <stdio.h>
#define max 5000
int main (void)
{
char data [max];
int j = 0, i=0;
FILE* input;
FILE* output;
input = fopen ("input_40x10x300_nohead.cor", "r"); //open the input file
output = fopen ("input_new_40x10x300.cor", "w");
for(j=1; j<max && fscanf(input, "%c", &data[j]) != EOF; j++)
{
if (data[j] == ' ')
{
i++;
}
if(i < 4 || i >= 7)
{
fputc (data[j], output);
}
if (data[j] == '\n')
{
j=0;
i=0;
}
}
fclose (input);
fclose(output);
return 0;
}
-
Also, previously this:
Code:
fprintf (output, "%c", data[j]);
was in the place of:
Code:
fputc (data[j], output);
but that did not work either.
-
I added the code above. I am not familiar with perror but I assume that if that condition was true it would have reported that error during compiling. The program compiled fine and when I added fprintf (output, "Outside") and another one that should print "inside" in the loop just to see if I could write to the file at all. This did not print to the file either. This leads me to believe I have some error in opening the file or in my syntax in writing to the file. Thanks again for the help, it is very appreciated.
The code again, for reference:
Code:
#include <stdio.h>
#define max 5000
int main (void)
{
char data [max];
int j = 0, i=0;
FILE* input;
FILE* output;
input = fopen ("input_40x10x300_nohead.cor", "r"); //open the input file
output = fopen ("input_new_40x10x300.cor", "w");
if ( input == NULL ) {
perror( "Can't open input file" );
exit( 1 );
}
fprintf (output, "outside");
for(j=1; j<max && fscanf(input, "%c", &data[j]) != EOF; j++)
{
if (data[j] == ' ')
{
i++;
}
if(i < 4 || i >= 7)
{
fprintf (output, "%c", data[j]);
fprintf (output, "inside");
}
if (data[j] == '\n')
{
j=0;
i=0;
}
}
fclose (input);
fclose(output);
return 0;
*edit: code was only half there.