function getc problem - newbie



 DEVELOP > c-Plus-Plus > function getc problem - newbie

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "antonaras"
Date: 08 Mar 2006 04:45:39 AM
Object: function getc problem - newbie
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!
.

User: "Dietmar Kuehl"

Title: Re: function getc problem - newbie 08 Mar 2006 05:05:07 AM
antonaras 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. 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'. 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.
--
<mailto:dietmar_kuehl@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence
.
User: "Rolf Magnus"

Title: Re: function getc problem - newbie 08 Mar 2006 08:22:11 AM
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.

.
User: "Dietmar Kuehl"

Title: Re: function getc problem - newbie 08 Mar 2006 08:42:35 AM
Rolf Magnus wrote:

Dietmar Kuehl wrote:

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

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

for (char c; in.get(c) && c != '>'; )
std::cout << t;


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.
--
<mailto:dietmar_kuehl@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence
.
User: "antonaras"

Title: Re: function getc problem - newbie 08 Mar 2006 11:48:13 AM
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
.




User: "Default User"

Title: Re: function getc problem - newbie 08 Mar 2006 01:21:30 PM
antonaras wrote:

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
--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER