Reputation: 27
warning: passing argument 1 of 'fprintf' from incompatible pointer type
warning: passing argument 2 of 'fprintf' makes pointer from integer without a cast
How can I fix these two warnings? I cannot produce a file with expected output.
int main(int argc, char* argv[])
{
int num_values = strtoul(argv[1], NULL, 10);
value_t* pValues = generate_sequence(num_values);
randomize_sequence(pValues, num_values);
// Record results
//FILE *fd = fopen("/results.txt", "w+");
for (int i = 0; i < num_values; i++) { //change made: i++ to allow looping
fprintf("results.txt", i, pValues[i]); //changes made: "fprintf". i and fd were added to the argument
}
//fclose(fd); //change made: "fclose"
return EXIT_SUCCESS;
}
Upvotes: 0
Views: 5273
Reputation: 1153
The general syntax for fprintf()
is
int fprintf(FILE *stream, const char *format, ...)
Where first argument FILE *stream
needs a file pointer, which should be opened for operation using
FILE *fptr = fopen("results.txt", "w");
function.
So, please uncomment line no. 54 and 58 then inside loop change the fprintf("results.txt", i, pValues[i])
to fprintf(fd, "%d", pValues[i])
.
Also learn about Basics of File Handling in C.
Upvotes: 0
Reputation: 2177
The first argument to fprintf
should be the FILE*
that is returned from a call to fopen
.
The second argument is a format string. Any further arguments are inserted into the placeholders in the format string.
Consider reading the manual page to better understand standard library functions.
Upvotes: 2