FILE pointer

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

    #1

    FILE pointer

    I have made a simple program to read the contents of a file.The FILE
    pointer returns NULL at the very beginning eventhough the file is
    present.Now if I modify my program and use command line arguments
    instead, I get the desired output.Now after I have run this program
    with argv; and I shift back to the one without command line arguments
    and run it ,it works.Why doesn't it work in the beginning itself?
    I am using Turbo C++ (DOS).I tried it in devc++ but there it doesn't
    work at all.

  • hbn

    #2
    Re: FILE pointer

    rajus wrote:
    I have made a simple program to read the contents of a file.The FILE
    pointer returns NULL at the very beginning eventhough the file is
    present.Now if I modify my program and use command line arguments
    instead, I get the desired output.Now after I have run this program
    with argv; and I shift back to the one without command line arguments
    and run it ,it works.Why doesn't it work in the beginning itself?
    I am using Turbo C++ (DOS).I tried it in devc++ but there it doesn't
    work at all.
    Your error is probably in line 42.

    Please provide some code, we have a very hard to guessing what your
    code looks like. Other than that it sounds like you are either trying
    to open a file you don't have permissions to open or you are giving
    fopen() an incorrect first and/or second argument.

    hbn

    Comment

    • Nick Keighley

      #3
      Re: FILE pointer

      rajus wrote:
      I have made a simple program to read the contents of a file.The FILE
      pointer returns NULL at the very beginning eventhough the file is
      present.Now if I modify my program and use command line arguments
      instead, I get the desired output.Now after I have run this program
      with argv; and I shift back to the one without command line arguments
      and run it ,it works.Why doesn't it work in the beginning itself?
      I am using Turbo C++ (DOS).I tried it in devc++ but there it doesn't
      work at all.
      if ((inf = fopen (filename, "r")) == NULL)
      {
      fprintf (stderr, "can't open file \"%s\" %s\n", filename,
      strerror (errno));
      exit (1);
      }

      note 1: printing the filename is handy because it may not be the one
      you think it is. You may have \n on the end for instance.

      note 2: strerror() relies on fprintf() setting errno. This is *not*
      guarenteed
      by the standard, but nevertheless works on many implementations . (eg.
      every unix I've come across and windows)


      --
      Nick Keighley

      Comment

      • Kenneth Brody

        #4
        Re: FILE pointer

        rajus wrote:
        >
        I have made a simple program to read the contents of a file.The FILE
        pointer returns NULL at the very beginning eventhough the file is
        present.Now if I modify my program and use command line arguments
        instead, I get the desired output.Now after I have run this program
        with argv; and I shift back to the one without command line arguments
        and run it ,it works.Why doesn't it work in the beginning itself?
        I am using Turbo C++ (DOS).I tried it in devc++ but there it doesn't
        work at all.
        Well, my psychic reader is a little flakey, but it's telling me that
        the problem is you forgot that, in order to include a backslash in a
        string literal, you need to use two backslashes.

        In other words, you cannot use:

        char filename[] = "c:\filename.tx t";

        but rather:

        char filename[] = "c:\\filename.t xt";

        Or, simply use the "better" form of:

        char filename[] = "c:/filename.txt";


        Either that, or there is an error on line 42. In that case, please
        include some actual source code.

        --
        +-------------------------+--------------------+-----------------------+
        | Kenneth J. Brody | www.hvcomputer.com | #include |
        | kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer .h|
        +-------------------------+--------------------+-----------------------+
        Don't e-mail me at: <mailto:ThisIsA SpamTrap@gmail. com>

        Comment

        • Keith Thompson

          #5
          Re: FILE pointer

          "Nick Keighley" <nick_keighley_ nospam@hotmail. comwrites:
          [...]
          if ((inf = fopen (filename, "r")) == NULL)
          {
          fprintf (stderr, "can't open file \"%s\" %s\n", filename,
          strerror (errno));
          exit (1);
          }
          [...]
          note 2: strerror() relies on fprintf() setting errno. This is *not*
          guarenteed by the standard, but nevertheless works on many
          implementations . (eg. every unix I've come across and windows)
          Correction: it depends on fopen() setting errno.

          --
          Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
          San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
          We must do something. This is something. Therefore, we must do this.

          Comment

          • Nick Keighley

            #6
            Re: FILE pointer

            Keith Thompson wrote:
            "Nick Keighley" <nick_keighley_ nospam@hotmail. comwrites:
            [...]
            if ((inf = fopen (filename, "r")) == NULL)
            {
            fprintf (stderr, "can't open file \"%s\" %s\n", filename,
            strerror (errno));
            exit (1);
            }
            [...]
            note 2: strerror() relies on fprintf() setting errno. This is *not*
            guarenteed by the standard, but nevertheless works on many
            implementations . (eg. every unix I've come across and windows)
            >
            Correction: it depends on fopen() setting errno.

            arg! how did I do that!?

            --
            Nick keighley

            Comment

            • CBFalconer

              #7
              Re: FILE pointer

              Nick Keighley wrote:
              Keith Thompson wrote:
              >"Nick Keighley" <nick_keighley_ nospam@hotmail. comwrites:
              >
              >[...]
              >> if ((inf = fopen (filename, "r")) == NULL)
              >> {
              >> fprintf (stderr, "can't open file \"%s\" %s\n",
              >> filename, strerror (errno));
              >> exit (1);
              >> }
              >[...]
              note 2: strerror() relies on fprintf() setting errno. This is *not*
              guarenteed by the standard, but nevertheless works on many
              implementations . (eg. every unix I've come across and windows)
              >>
              >Correction: it depends on fopen() setting errno.
              >
              arg! how did I do that!?
              And to detect that you need to precede the fopen statement with
              "errno = 0;" statement.

              --
              Chuck F (cbfalconer at maineline dot net)
              Available for consulting/temporary embedded and systems.
              <http://cbfalconer.home .att.net>


              Comment

              • rajus

                #8
                Re: FILE pointer

                My code is:

                #include<stdio. h>
                #include<conio. h>
                #include<stdlib .h>

                void main( )
                {
                FILE *fp,*ft;
                char ch;
                //clrscr();
                fp=fopen("txt1. txt","r");
                if(fp==NULL)
                {
                printf("Error opening file");
                fclose(fp);
                exit(1);
                }
                ft=fopen("txt2. txt","w");
                while((ch=fgetc (fp))!=EOF)
                {
                if((ch-32)>=65 || (ch-32)<=90 && ch!='\n')
                {
                ch=ch-32;
                fputc(ch,ft);
                }
                else
                {
                fputc(ch,ft);
                }

                }

                fclose(ft);
                fclose(fp);


                }


                This program just converts to uppercase.But this doesn't work in the
                beginning.In fact none of the programs using fopen work.All return NULL
                at the very beginning.

                But then if i run this code.
                #include<stdio. h>
                #include<conio. h>
                #include<stdlib .h>

                void main(int argc,char* argv[])
                {
                FILE *fp,*ft;
                char ch;
                //clrscr();
                fp=fopen(argv[1],"r");
                if(fp==NULL)
                {
                printf("Error opening file");
                fclose(fp);
                exit(1);
                }
                ft=fopen(argv[2],"w");
                while((ch=fgetc (fp))!=EOF)
                {
                if((ch-32)>=65 || (ch-32)<=90 && ch!='\n')
                {
                ch=ch-32;
                fputc(ch,ft);
                }
                else
                {
                fputc(ch,ft);
                }

                }

                fclose(ft);
                fclose(fp);


                }

                and run the previous one it works.Also all other fopen programs start
                working?
                What's the problem?

                hbn wrote:
                rajus wrote:
                I have made a simple program to read the contents of a file.The FILE
                pointer returns NULL at the very beginning eventhough the file is
                present.Now if I modify my program and use command line arguments
                instead, I get the desired output.Now after I have run this program
                with argv; and I shift back to the one without command line arguments
                and run it ,it works.Why doesn't it work in the beginning itself?
                I am using Turbo C++ (DOS).I tried it in devc++ but there it doesn't
                work at all.
                >
                Your error is probably in line 42.
                >
                Please provide some code, we have a very hard to guessing what your
                code looks like. Other than that it sounds like you are either trying
                to open a file you don't have permissions to open or you are giving
                fopen() an incorrect first and/or second argument.
                >
                hbn

                Comment

                • Kenny McCormack

                  #9
                  Re: FILE pointer

                  In article <1161367671.203 152.91170@i3g20 00cwc.googlegro ups.com>,
                  rajus <rajas3@gmail.c omwrote:
                  >My code is:
                  >
                  >#include<stdio .h>
                  >#include<conio .h>
                  >#include<stdli b.h>
                  >
                  >void main( )
                  You know the drill...

                  Comment

                  • Keith Thompson

                    #10
                    Re: FILE pointer

                    "rajus" <rajas3@gmail.c omwrites:
                    My code is:
                    >
                    #include<stdio. h>
                    #include<conio. h>
                    Delete this. It's non-standard, and you don't use it.
                    #include<stdlib .h>
                    >
                    void main( )
                    main returns int. The correct declaration is:

                    int main(void)
                    {
                    FILE *fp,*ft;
                    char ch;
                    You've declared ch as a variable of type char (see below).
                    //clrscr();
                    "//" comments can cause problems on Usenet. If a long line is
                    wrapped, which can easily happen, a "//" comment can create a syntax
                    error; "/*...*/" comments are less likely to cause this. And "//"
                    comments are new in C99; they're not supported in C90, and some
                    compilers, in some modes, do not support them.

                    Why would you want to clear the screen anyway? I might have valuable
                    information there.
                    fp=fopen("txt1. txt","r");
                    if(fp==NULL)
                    {
                    printf("Error opening file");
                    Usually error messages should be printed to stderr, and output
                    should end in a newline:

                    fprintf(stderr, "Error opening file\n");
                    fclose(fp);
                    exit(1);
                    exit(1) is non-portable. The only portable values for exit() are 0,
                    EXIT_SUCCESS, and EXIT_FAILURE. (On some systems, exit(1) indicates
                    that your program terminated successfully.)
                    }
                    ft=fopen("txt2. txt","w");
                    You didn't check whether this fopen() succeeded. You should *always*
                    check the value returned by fopen().
                    while((ch=fgetc (fp))!=EOF)
                    fgetc() returns a result of type int, but you assign it to a variable
                    of type char. The reason fgetc() returns int is so that the result
                    can represent *either* any possible character value (represented as an
                    unsigned char) *or* the unique value EOF. You need to change the
                    declaration of ch to "int ch;".
                    {
                    if((ch-32)>=65 || (ch-32)<=90 && ch!='\n')
                    {
                    ch=ch-32;
                    fputc(ch,ft);
                    }
                    else
                    {
                    fputc(ch,ft);
                    }
                    >
                    }
                    Magic numbers. The above code depends on the numeric values of
                    various character codes; these are not specified by the standard. It
                    also, I think, assumes that the codes for the letters are contiguous;
                    this also isn't guaranteed. Fortunately, there's no need to make
                    these assumptions. You say later that the program is supposed to
                    convert to upper case; see the toupper() function in <ctype.h>.
                    fclose(ft);
                    fclose(fp);
                    >
                    Add here:
                    return 0;
                    }
                    [snip]

                    The comp.lang.c FAQ is at <http://www.c-faq.com/>; I think you'll find
                    it very useful.

                    --
                    Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                    San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
                    We must do something. This is something. Therefore, we must do this.

                    Comment

                    • CBFalconer

                      #11
                      Re: FILE pointer

                      rajus wrote:
                      >
                      My code is:
                      >
                      #include<stdio. h>
                      #include<conio. h>
                      No such header as <conio.h>. Delete this.
                      #include<stdlib .h>
                      >
                      void main( )
                      main returns int. Use "int main(void)
                      {
                      FILE *fp,*ft;
                      char ch;
                      This should be an int. A char can't hold the value of EOF.
                      //clrscr();
                      fp=fopen("txt1. txt","r");
                      if(fp==NULL)
                      {
                      printf("Error opening file");
                      fclose(fp);
                      Don't close a NULL fp. Delete this.
                      exit(1);
                      Invalid argument to exit. Use EXIT_FAILURE and #include <stdlib.h>
                      }
                      ft=fopen("txt2. txt","w");
                      How do you know this fopen worked. You didn't test it.
                      while((ch=fgetc (fp))!=EOF)
                      {
                      if((ch-32)>=65 || (ch-32)<=90 && ch!='\n')
                      Horrors. What have 32, 65, 90, etc. got to do with it? See the
                      is* family of tests, prototyped in <ctype.h>. isupper, islower,
                      toupper, tolower, etc.
                      {
                      ch=ch-32;
                      Ugh.
                      fputc(ch,ft);
                      }
                      else
                      {
                      fputc(ch,ft);
                      }
                      >
                      }
                      >
                      fclose(ft);
                      fclose(fp);
                      You forgot to return 0, or EXIT_SUCCESS.
                      >
                      }
                      >
                      This program just converts to uppercase.But this doesn't work in
                      the beginning.In fact none of the programs using fopen work.All
                      return NULL at the very beginning.
                      Also end your sentences with ". ". And don't top-post, your
                      answer belongs below (or intermixed with) the _snipped_ material to
                      which you reply. The snipping removes anything that is not germane
                      to your reply.

                      You are also allowed to use blanks in your source. They are no
                      longer a scarce resource.

                      --
                      Chuck F (cbfalconer at maineline dot net)
                      Available for consulting/temporary embedded and systems.
                      <http://cbfalconer.home .att.net>

                      Comment

                      • Kenneth Brody

                        #12
                        Re: FILE pointer

                        CBFalconer wrote:
                        >
                        rajus wrote:
                        [...]
                        if((ch-32)>=65 || (ch-32)<=90 && ch!='\n')
                        >
                        Horrors. What have 32, 65, 90, etc. got to do with it? See the
                        is* family of tests, prototyped in <ctype.h>. isupper, islower,
                        toupper, tolower, etc.
                        [...]

                        Am I missing something, or have all the other posters missed what I'm
                        seeing?

                        Won't the above statement be true for every character except '\n',
                        regardless of whether it's lower-case or not, because of the "||"
                        rather than "&&"? Every number is ">=65" or "<=90"

                        (Not to mention the already-mentioned horrors of the use of such
                        magic numbers here and elsewhere in the program.)

                        --
                        +-------------------------+--------------------+-----------------------+
                        | Kenneth J. Brody | www.hvcomputer.com | #include |
                        | kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer .h|
                        +-------------------------+--------------------+-----------------------+
                        Don't e-mail me at: <mailto:ThisIsA SpamTrap@gmail. com>


                        Comment

                        • Richard Bos

                          #13
                          Re: FILE pointer

                          Kenneth Brody <kenbrody@spamc op.netwrote:
                          CBFalconer wrote:

                          rajus wrote:
                          [...]
                          if((ch-32)>=65 || (ch-32)<=90 && ch!='\n')
                          Horrors. What have 32, 65, 90, etc. got to do with it? See the
                          is* family of tests, prototyped in <ctype.h>. isupper, islower,
                          toupper, tolower, etc.
                          >
                          Am I missing something, or have all the other posters missed what I'm
                          seeing?
                          >
                          Won't the above statement be true for every character except '\n',
                          regardless of whether it's lower-case or not, because of the "||"
                          rather than "&&"? Every number is ">=65" or "<=90"
                          No, that's just another good reason to use the Standard <ctype.h>
                          functions, rather than getting it wrong your own way.

                          Richard

                          Comment

                          • Hallvard B Furuseth

                            #14
                            Which standards do the implementation support?

                            A recent thread mentioned that the C standard does not guarantee
                            that failed file operations set errno. To see if it did, set
                            errno=0 before the file operation and test errno afterwards.

                            I don't want to clutter my entire program with errno settings,
                            so is there a way for a general error reporting routine check
                            for _common_ implementations which set errno? Then use perror()
                            if yes, otherwise just output the user-supplied error phrase.

                            POSIX says errno should be set (or at least the POSIX version
                            I'm looking at) - is there an official 'if (running on POSIX)'
                            test to use? How about X/Open, XPG3/4, and whatever other
                            "standard" buzzwords there are to check for?

                            I need code for at least Windows and preferably Mac as well,
                            but I don't know them. I seem to remember there is some weird
                            Windows error code one should use instead of errno?

                            --
                            Hallvard

                            Comment

                            • Al Balmer

                              #15
                              Re: Which standards do the implementation support?

                              On Thu, 26 Oct 2006 13:52:48 +0200, Hallvard B Furuseth
                              <h.b.furuseth@u sit.uio.nowrote :
                              >POSIX says errno should be set (or at least the POSIX version
                              >I'm looking at) - is there an official 'if (running on POSIX)'
                              >test to use?
                              <OTYes, there are defined macros which tell what versions and what
                              particular features of POSIX are available if compiling on a POSIX
                              implementation, and functions that report the POSIX support at
                              runtime. Any good POSIX book should detail the steps. If you need
                              further help, comp.unix.progr ammer is probably a good place to ask.

                              --
                              Al Balmer
                              Sun City, AZ

                              Comment

                              Working...