how to come out of file

Discussion in 'C Programming' started by kumar, May 26, 2008.

  1. kumar

    kumar Guest

    i am bit confused about this code, why does the below while loop is
    contineous
    how can i come out of the file.
    i am using linux machine


    char ch;//char varaible
    FILE *fp; // file pointer
    fp=fopen("file","r");
    while(fp!=EOF)// here i am just reading character by character and
    printing to display
    {
    fscanf(fp,&ch);
    printf("%c",ch);

    }

    problem is:: At end ,file pointer is pointing to last character and it
    is holding that because of this
    the while condition is true
    so how can i come out of loop
     
    kumar, May 26, 2008
    #1
    1. Advertising

  2. kumar

    Bartc Guest

    "kumar" <> wrote in message
    news:...
    >i am bit confused about this code, why does the below while loop is
    > contineous
    > how can i come out of the file.
    > i am using linux machine
    >
    >
    > char ch;//char varaible
    > FILE *fp; // file pointer
    > fp=fopen("file","r");
    > while(fp!=EOF)// here i am just reading character by character and
    > printing to display
    > {
    > fscanf(fp,&ch);
    > printf("%c",ch);
    >
    > }


    fp is unlikely to ever equal EOF; it's a file handle.

    You might try:

    while ( (ch=fgetc(fp))!=EOF)

    and take out the fscanf line (which looks suspect anyway).

    Also you should check that fp is valid after fopen(). And don't forget
    fclose(fp) at the end.


    --
    bartc
     
    Bartc, May 26, 2008
    #2
    1. Advertising

  3. kumar <> wrote:
    > i am bit confused about this code, why does the below while loop is
    > contineous
    > how can i come out of the file.
    > i am using linux machine


    > char ch;//char varaible


    You better don't get in the habit of commenting the obvious.

    > FILE *fp; // file pointer
    > fp=fopen("file","r");
    > while(fp!=EOF)// here i am just reading character by character and


    This completely off the mark and the compiler would tell so
    you if you would raise its warning level to a suitable value.
    You are comparing an int to a pointer and that makes no sense
    at all. 'fp' will never change (at least as long as you don't
    actively asign it a new value) and neither will EOF.

    Instead you should check 'fp' for being NULL before using it
    - it will be NULL in case opening the file failed.

    > printing to display
    > {
    > fscanf(fp,&ch);


    Testing the return value of fscanf() will tell you when you
    reached the end of the file. Since you read only a single
    item you simply can test it for EOF (but also other problem
    with reading the file will make it return EOF, if you want
    to know the details use feof() or ferror() to distinguish
    between those cases).

    But using fscanf() is a bit like shooting sparrows with a canon,
    have a look at (f)getc() for a probably much faster function for
    reading single characters.

    > printf("%c",ch);


    And here putchar() probably is more suitable.

    > problem is:: At end ,file pointer is pointing to last character and it


    The file pointer isn't pointing to a char, maybe some pointer
    in a struture 'fp' may be pointing to does (but that's imple-
    mentation specific and you shouldn't care about that)

    > is holding that because of this
    > the while condition is true


    The while condition is only true by accident. You're comparing
    apples to bananas and since such a comparison isn't reasonable
    you whatever the result is has no value for your program.

    > so how can i come out of loop


    By testing the return values of functions that tell you some-
    thing about the fact that the end of the file has been reached.
    Typically these are the functions that do the reading and thus
    notice that they couldn't read what you asked them to read.
    Carefully read their descriptions and you will find it's rather
    simple. Don't make unfounded assumptions like that the file
    pointer would do anything.
    Regards, Jens
    --
    \ Jens Thoms Toerring ___
    \__________________________ http://toerring.de
     
    Jens Thoms Toerring, May 26, 2008
    #3
  4. Bartc <> wrote:
    > "kumar" <> wrote in message
    > news:...
    > >i am bit confused about this code, why does the below while loop is
    > > contineous
    > > how can i come out of the file.
    > > i am using linux machine
    > >
    > >
    > > char ch;//char varaible
    > > FILE *fp; // file pointer
    > > fp=fopen("file","r");
    > > while(fp!=EOF)// here i am just reading character by character and
    > > printing to display
    > > {
    > > fscanf(fp,&ch);
    > > printf("%c",ch);
    > > }


    > fp is unlikely to ever equal EOF; it's a file handle.


    > You might try:


    > while ( (ch=fgetc(fp))!=EOF)


    I might be useful to add that in this case 'ch' must be defined
    as an int and not a char as the OP did, otherwise he might end
    up with the same problem as before;-)

    > and take out the fscanf line (which looks suspect anyway).


    In what respect? I guess it's not too clever to use fscanf()
    for single character input, but "suspect" seems to be a bit
    strong to me.
    Regards, Jens
    --
    \ Jens Thoms Toerring ___
    \__________________________ http://toerring.de
     
    Jens Thoms Toerring, May 26, 2008
    #4
  5. kumar

    Guest

    On May 26, 5:42 pm, kumar <> wrote:
    > i am bit confused about this code, why does the below while loop is
    > contineous
    > how can i come out of the file.
    > i am using linux machine
    >
    > char ch;//char varaible

    Don't comment the obvious, as suggested by others as well.
    ch should be int. See question 12.1 of the C faq for an explanation.
    <http://c-faq.com/>
    > FILE *fp; // file pointer
    > fp=fopen("file","r");

    Test for the return value of fopen(). If fopen() fails, NULL is
    returned.
    > while(fp!=EOF)// here i am just reading character by character and

    Here you compare a pointer to an integer. What you actually want is:
    while((ch = getc(fp)) != EOF)
    > printing to display

    Also use /* */ comments when you post to usenet. Line wrapping breaks
    code that uses // comments.
    > {
    > fscanf(fp,&ch);

    fscanf's second argument is expected to be a null terminated array.
    You are passing a pointer to a single char, also note the absence of
    any variables to write to whatsoever.
    > printf("%c",ch);
    >
    > }
     
    , May 26, 2008
    #5
  6. Jens Thoms Toerring <> wrote:
    > Bartc <> wrote:
    > > > fscanf(fp,&ch);

    > > and take out the fscanf line (which looks suspect anyway).


    > In what respect? I guess it's not too clever to use fscanf()
    > for single character input, but "suspect" seems to be a bit
    > strong to me.


    Sorry, I take that back! Of course, the way the OP used it is
    isn't just suspect but plain wrong since the format string is
    missing! I should have read it more carefully.

    Regards, Jens
    --
    \ Jens Thoms Toerring ___
    \__________________________ http://toerring.de
     
    Jens Thoms Toerring, May 26, 2008
    #6
  7. kumar

    Bartc Guest

    "Jens Thoms Toerring" <> wrote in message
    news:-berlin.de...
    > Bartc <> wrote:
    >> "kumar" <> wrote in message
    >> news:...
    >> >i am bit confused about this code, why does the below while loop is
    >> > contineous
    >> > how can i come out of the file.
    >> > i am using linux machine
    >> >
    >> >
    >> > char ch;//char varaible
    >> > FILE *fp; // file pointer
    >> > fp=fopen("file","r");
    >> > while(fp!=EOF)// here i am just reading character by character and
    >> > printing to display
    >> > {
    >> > fscanf(fp,&ch);
    >> > printf("%c",ch);
    >> > }

    >
    >> fp is unlikely to ever equal EOF; it's a file handle.

    >
    >> You might try:

    >
    >> while ( (ch=fgetc(fp))!=EOF)

    >
    > I might be useful to add that in this case 'ch' must be defined
    > as an int and not a char as the OP did, otherwise he might end
    > up with the same problem as before;-)


    Oops, yes. It looked just like some code of mine the other day, which come
    to think of it was wrong too..

    --
    Bartc
     
    Bartc, May 26, 2008
    #7
  8. kumar <> writes:
    > i am bit confused about this code, why does the below while loop is
    > contineous
    > how can i come out of the file.
    > i am using linux machine
    >
    >
    > char ch;//char varaible
    > FILE *fp; // file pointer
    > fp=fopen("file","r");
    > while(fp!=EOF)// here i am just reading character by character and
    > printing to display
    > {
    > fscanf(fp,&ch);
    > printf("%c",ch);
    >
    > }
    >
    > problem is:: At end ,file pointer is pointing to last character and it
    > is holding that because of this
    > the while condition is true
    > so how can i come out of loop


    Your best bet is to read section 12 of the comp.lang.c FAQ,
    <http://www.c-faq.com/>. throw away the code you've posted, and start
    again.

    It looks very similar to something that was posted here recently; I
    assume that's where you got it. You should at least go back and look
    at the responses to that article.

    Here's what's wrong with the code you posted.

    You posted a code fragment, not a complete program. There's no way
    for us to tell whether the code you posted is even your real code.
    Write a small, complete, self-contained program that illustrates your
    problem, and post that *exactly* (use copy-and-paste or equivalent).

    Avoid "//" comments in C code posted to Usenet. Line-wrapping can
    turn them into syntax errors.

    You don't check whether fopen() succeeded or not.

    The comparison ``fp!=EOF'' is nonsensical. Your compiler should have
    rejected it, or at least warned you about it. Did you get a warning
    from your compiler? If so, *don't just ignore it*, and at least tell
    us about it. If not, find out how to get your compiler to produce
    more warnings.

    Your fscanf call is nonsensical. The second parameter is (a pointer
    to) a format string. Don't even think about using a function without
    first reading the documentation.

    Your code shows signs that you're guessing how things work. Don't do
    that. If you need to know how a function works, consult your
    textbook, or your online documentation, or even the standard.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, May 26, 2008
    #8
  9. kumar

    gavenkoa Guest

    On May 26, 5:58 pm, "Bartc" <> wrote:
    > "kumar" <> wrote in message
    > You might try:
    >
    > while ( (ch=fgetc(fp))!=EOF)
    >


    > --
    > bartc


    Some warrning about `ch=fgetc(fp)'. ch has char type so you lost
    precision when compare with EOF.
     
    gavenkoa, May 26, 2008
    #9
    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. Replies:
    3
    Views:
    488
    Ken Cox [Microsoft MVP]
    Oct 7, 2005
  2. Emu

    When did ASP.net 2.0 come out?

    Emu, Nov 29, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    2,815
    Juan T. Llibre
    Nov 29, 2005
  3. Alan  Green
    Replies:
    1
    Views:
    387
    Alan Green
    Jul 19, 2005
  4. Mark Moeykens
    Replies:
    2
    Views:
    532
    Mark Moeykens
    Sep 7, 2010
  5. PerlFAQ Server
    Replies:
    0
    Views:
    141
    PerlFAQ Server
    Apr 26, 2011
Loading...

Share This Page