I am continuously getting a seg fault (core dump) each time that I run it, no matter what I change or the fact that is compiling fine! Not sure where I am making the mistake? Any suggestions?
int main (int argc, char** argv)
{
FILE *output;
if ((output = fopen("output", "w")) == NULL)
{
printf ("cannot open the file input\n");
exit (EXIT_FAILURE);
}
int data = 1;
while (scanf("%d", data) > 0)
{
print(output, data);
}
fclose(output);
return (0);
}
void print(FILE* output, int data)
{
fprintf(output, "%d\n", data);
return;
}
1 change while (scanf("%d", data) > 0)
to while (scanf("%d", &data) > 0)
.
2 move the function of print
to the begin.
This could work for you.
#include <stdio.h>
#include <string.h>
#define MAX 100
void print(FILE* output, int data)
{
fprintf(output, "%d\n", data);
return;
}
int main (int argc, char** argv)
{
FILE *output;
if ((output = fopen("output", "w")) == NULL)
{
printf ("cannot open the file input\n");
return -1;
}
int data = 1;
while (scanf("%d", &data) > 0)
{
print(output, data);
}
fclose(output);
return (0);
}
1 2 3 "Enter" "Ctrl+D"