Your file has had to be severely repaired to be able to run it, so I'm afraid you have not tested your code before sending it. Anyway, I have made a series of changes and I'll comment them in a diff file output, so you know what has been changed and how. Replacements are signalled with a -
before the line changed, and with +
--- pru4358.c-orig 2021-04-20 10:05:49.236297000 +0300
+++ pru4358.c 2021-04-20 10:08:11.266937000 +0300
@@ -1,7 +1,11 @@
#include <stdio.h>
#include <stdlib.h>
+char path[] = "file.txt";
+
There's no definition of the path
variable in main
. Every variable or data object must be declared to the compiler to be able to use it, before its use.
+void read(FILE *f);
+
you need to add prototypes of the functions you implement after their use in the file, or the compiler will not know what kinds of parameter your code uses and what types they are.
-main() {
+int main() {
Since a long ago, main()
needs a return type int
, and you need to write it. The compiler allows this to cope with very old legacy code, but it shouldn't be used in new projects. If you don't do, the compiler will assume your function returns int
and when you later define it as void
the compiler will shout at you telling that you don't agree yourself with the definition of it.
FILE *file;
file = fopen(path, "rt"); //Instead of "path" there is the file's path
@@ -10,7 +14,7 @@
return 0;
}
-void read(f) {
+void read(FILE *f) {
As above, you need to inform the compiler about the number or parameters, a name for each to be able to use them inside the function, and very important: the type of the parameters (in this case, FILE *
)
int c;
if (f)
Now, I will post the file as it is now, but commenting the places in which your don't do necessary things, that will lead you in trouble if you don't do them.
pru4358.c (corrected)
#include <stdio.h>
#include <stdlib.h>
char path[] = "file.txt";
void read(FILE *f);
int main() {
FILE *file;
file = fopen(path, "rt"); //Instead of "path" there is the file's path
You don't check the value returned from fopen()
, and this can cause problems in the future, if you get NULL
as return value from fopen()
(It happened to me the first time I executed it, as I hadn't a file.txt.
file)
read(file);
You had better not to select a name like read
for your file reading function. This example works (because you don't have to use the read(2)
system call) but you'll run in trouble if you use any system call name (or even a library function name) for your own functions.
fclose(file);
return 0;
}
void read(FILE *f) {
int c;
if (f)
You have better to do the NULL
check before calling read()
, just after fopen()
ing it. Don't you think so? (As someone reading your code --like me--, would think you are not checking the return value of the function) You don't actually check it in main()
and you later fclose()
it being it NULL
or not.
while ((c = getc(f)) != EOF)
putchar(c);
}
and last is how I believe your code should have been written (you were close to it :) )
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* static, to avoid name pollution */
static char path[] = "file.txt";
void copy_file_to_stdout(FILE *f);
int main() {
FILE *file;
file = fopen(path, "rt");
if (!file) {
fprintf(stderr, "ERROR: %s: %s\n",
path, strerror(errno));
/* don't continue if the file cannot be opened */
exit(EXIT_FAILURE);
}
/* successfuly opened, so all that follows will have sense */
copy_file_to_stdout(file);
fclose(file);
return EXIT_SUCCESS;
}
/* this is a better name for the file, as you don't just read the
* file contents, you also write them to stdout. */
void copy_file_to_stdout(FILE *f) {
int c;
while ((c = getc(f)) != EOF)
putchar(c);
}