how to come out of file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • kumar

    how to come out of file

    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

  • Bartc

    #2
    Re: how to come out of file


    "kumar" <raman.emb@gmai l.comwrote in message
    news:2a5df39e-d53c-4d98-b09d-ff60985a724a@w3 4g2000prm.googl egroups.com...
    >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




    Comment

    • Jens Thoms Toerring

      #3
      Re: how to come out of file

      kumar <raman.emb@gmai l.comwrote:
      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 ___ jt@toerring.de
      \______________ ____________ http://toerring.de

      Comment

      • Jens Thoms Toerring

        #4
        Re: how to come out of file

        Bartc <bc@freeuk.comw rote:
        "kumar" <raman.emb@gmai l.comwrote in message
        news:2a5df39e-d53c-4d98-b09d-ff60985a724a@w3 4g2000prm.googl egroups.com...
        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 ___ jt@toerring.de
        \______________ ____________ http://toerring.de

        Comment

        • vippstar@gmail.com

          #5
          Re: how to come out of file

          On May 26, 5:42 pm, kumar <raman....@gmai l.comwrote:
          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) ;
          >
          }

          Comment

          • Jens Thoms Toerring

            #6
            Re: how to come out of file

            Jens Thoms Toerring <jt@toerring.de wrote:
            Bartc <bc@freeuk.comw rote:
            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 ___ jt@toerring.de
            \______________ ____________ http://toerring.de

            Comment

            • Bartc

              #7
              Re: how to come out of file


              "Jens Thoms Toerring" <jt@toerring.de wrote in message
              news:6a032oF345 ijuU2@mid.uni-berlin.de...
              Bartc <bc@freeuk.comw rote:
              >"kumar" <raman.emb@gmai l.comwrote in message
              >news:2a5df39 e-d53c-4d98-b09d-ff60985a724a@w3 4g2000prm.googl egroups.com...
              >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


              Comment

              • Keith Thompson

                #8
                Re: how to come out of file

                kumar <raman.emb@gmai l.comwrites:
                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_Keit h) kst-u@mib.org <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"

                Comment

                • gavenkoa

                  #9
                  Re: how to come out of file

                  On May 26, 5:58 pm, "Bartc" <b...@freeuk.co mwrote:
                  "kumar" <raman....@gmai l.comwrote 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.

                  Comment

                  • CBFalconer

                    #10
                    Re: how to come out of file

                    Bartc wrote:
                    "kumar" <raman.emb@gmai l.comwrote in message
                    >
                    >i am bit confused about this code, why does the below while loop
                    >is contineous how can i come out of the file.
                    >>
                    >char ch;//char varaible
                    >FILE *fp; // file pointer
                    > fp=fopen("file" ,"r");
                    >while(fp!=EO F)// 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.
                    And don't forget that ch has to be an int, not a char. The line
                    "printing to display" is going to trigger a syntax error. Don't
                    use // comments, especially for code to be posted in Usenet.

                    --
                    [mail]: Chuck F (cbfalconer at maineline dot net)
                    [page]: <http://cbfalconer.home .att.net>
                    Try the download section.

                    ** Posted from http://www.teranews.com **

                    Comment

                    Working...