Dietmar Kuehl wrote:
Hi i'm new to c++
and i'm trying to read from a file character by character and
output the result on the console.
This is my code:::
#include
#include
Brilliant! Step one: learn how to post code! The above include
statements are lacking their primary content.
This is probably goo
I guess they should
read
#include <iostream>
#include <stdio.h>
... and you somehow lost the 'using namespace std;' statement.
If you really want to learn C++, I recommend that you immediately
dump all uses of <stdio.h> and the corresponding functions in favor
of the C++ equivalents (yes, I hear the crowd in love with <stdio.h>
shouting but just disregard them: you can learn how to use the
<stdio.h> functions once you are more comfortable with C++).
int in_tags(FILE *pFile)
{
char t;
cout<<"in tags";
t=getc(pFile);
This does not work! 'getc()' returns an 'int' for a good reason:
the returned value indicates an error (if the value is 'EOF') or
the actual character. However, to represent all 'char' objects the
value 'EOF' cannot be of type 'char'.
However, there can be systems where int and char have the same size. On such
a system, checking the return value for EOF might not work either. Better
use feof(pFile).
Of course, you shall always check whether input was successful! Assuming
you are using IOStreams this could look like this:
int in_tags(std::istream& in)
{
std::cout << "in tags '";
for (char c; in.get(c) && c != '>'; )
std::cout << t;
There is no t.
std::cout << "'";
return 0;
}
I haven't looked closely at your 'main()' function but it suffers
from similar problems.
.