function getc problem - newbie

Discussion in 'C++' started by antonaras, Mar 8, 2006.

  1. antonaras

    antonaras Guest

    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!
     
    antonaras, Mar 8, 2006
    #1
    1. Advertising

  2. 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:> <http://www.dietmar-kuehl.de/>
    <http://www.eai-systems.com> - Efficient Artificial Intelligence
     
    Dietmar Kuehl, Mar 8, 2006
    #2
    1. Advertising

  3. antonaras

    Rolf Magnus Guest

    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.
     
    Rolf Magnus, Mar 8, 2006
    #3
  4. 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:> <http://www.dietmar-kuehl.de/>
    <http://www.eai-systems.com> - Efficient Artificial Intelligence
     
    Dietmar Kuehl, Mar 8, 2006
    #4
  5. antonaras

    antonaras Guest

    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
     
    antonaras, Mar 8, 2006
    #5
  6. antonaras

    Default User Guest

    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.
     
    Default User, Mar 8, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Irrwahn Grausewitz

    Re: getc hanging

    Irrwahn Grausewitz, Aug 31, 2003, in forum: C Programming
    Replies:
    1
    Views:
    359
    Irrwahn Grausewitz
    Aug 31, 2003
  2. spike
    Replies:
    9
    Views:
    324
    Martien Verbruggen
    Feb 27, 2004
  3. PL

    detect Ctrl+C with getc ?

    PL, May 25, 2004, in forum: C Programming
    Replies:
    2
    Views:
    885
  4. William L. Bahn

    getc() vs. fgetc()

    William L. Bahn, Jul 14, 2004, in forum: C Programming
    Replies:
    13
    Views:
    1,832
    Dave Thompson
    Jul 21, 2004
  5. Bill Cunningham

    getc and ungetc

    Bill Cunningham, Nov 18, 2004, in forum: C Programming
    Replies:
    8
    Views:
    417
    Malcolm
    Nov 22, 2004
Loading...

Share This Page