function getc problem - newbie

A

antonaras

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


int in_tags(FILE *pFile)
{
char t;
cout<<"in tags";
t=getc(pFile);
cout<<t;
while (t=getc(pFile)!='>')
{

cout<<t;
}
return 0;
}




int main ()
{
FILE * pFile;
char c;
bool tag;

pFile = fopen("example.txt","r");
if (pFile==NULL) perror ("Error opening file");
else
{
do {
c = getc (pFile);
cout<<c;
if(c=='<')
{
tag=in_tags(pFile);
}
} while (c != EOF);
fclose (pFile);

}
return 0;
}
The output that i get from the main program is ok
but when are coming from the function within the while loop
the caracters are lost and i get symbols.
I hope i'm clear enough but i don't think so but in case you know how
to help me pls reply as this is for a project that i'm doing for
college
thanks in advance!
 
D

Dietmar Kuehl

antonaras said:
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. 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
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'. 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;
std::cout << "'";
return 0;
}

I haven't looked closely at your 'main()' function but it suffers
from similar problems.
 
R

Rolf Magnus

Dietmar said:
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


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.
 
D

Dietmar Kuehl

Rolf said:
However, there can be systems where int and char have the same size. On

Yup. However, 'EOF' is still used to indicate read errors and you
might actually be unable to read 'char's which have the value 'EOF'.
Essentially, both C and C++ only define I/O for some character set
and this set does not include a 'char' which has the value 'EOF'.
such a system, checking the return value for EOF might not work either.
Better use feof(pFile).

Nope. You might use 'feof(pFile) || ferror(pFile)' instead of
testing the result against 'EOF'.
There is no t.

This is just a typo: I recycled the output but used a different
name in the input. Just replace the 't' with 'c' and it should be
fine. Thanks for pointing the problem out.
 
A

antonaras

thanks for the help everybody
though you should cut me a little slack i said i'm new i didn't know
that the source code will look like this in html
i didn't make my self clear and i'm sorry about that
Any way i found out what the problem was so thanks again appriciate the
help
 
D

Default User

antonaras said:
while (t=getc(pFile)!='>')

This has precedence problems. The evaluation operators, == and != have
higher precedence than assignment. What you actually have is equivalent
to:

while (t=(getch(pFile != '>'))

The value of the comparison is stored in t, which is not what you want.

It should be written:

while ((t=getc(pFile)) !- '>')


The output that i get from the main program is ok
but when are coming from the function within the while loop
the caracters are lost and i get symbols.

That's probably why.





Brian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

The Best Buffer Size for IO process 7
motion capture application, need improvements 1
problem opening a file 11
getc fgetc 7
Reading a file... 21
Write to file 22
Weird link list problem 2
I need help 1

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top