File IO in C problem

Hi. I am trouble with File IO with c.

Here is my code.

#include<stdio.h>

int main(int argc, char* argv[])
{
char c;
File* file = fopen(argv[1], "r");
while (c = getc(file))
{
printf("%c", c);
}
return 0;
}


When I run this on the linux command line, I get some strange output.

I must be able to use fopen and getc in my solution.
You should check for EOF.
1
2
3
4
5
6
7
8
9
10
11
12
#include<stdio.h>

int main(int argc, char* argv[])
{
	char c;
	File* file = fopen(argv[1], "r");
	while ((c = getc(file)) != EOF)
	{
		printf("%c", c);
	}
	return 0;
}

Last edited on
Topic archived. No new replies allowed.