I want to print the content of a txt file (first parameter), but the function to do so is in a different file. I have the following main file:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "fileoperation.h"
int main(int argc, char **argv)
{
read(argv[1]);
return 0;
}
and then in the fileoperation.c file I have:
#include "fileoperation.h"
void read(char* file)
{
FILE *fptr;
char c;
fptr = fopen(file, "r");
if (fptr == NULL)
{
printf("Cannot open file \n");
exit(0);
}
c = fgetc(fptr);
while (c != EOF)
{
printf ("%c", c);
c = fgetc(fptr);
}
fclose(fptr);
}
If I type the code from the function in the main function, it works. I don't understand why is not working
The header file of fileoperation.c is
#ifndef FILEOPERATION_H
#define FILEOPERATION_H
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void read(char* file);
#endif
Rename your function. read
exists in the backing libraries. To make matters worse, the compiler knows what it does and optimizes it out.
Could have been worse. You could have replaced the actual read
with your own and blown up the standard libraries.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With