Thread: Please help connect file pointer

  1. #1
    Registered User ralphwiggam's Avatar
    Join Date
    Sep 2006
    Posts
    32

    Please help connect file pointer

    I have a list of positive integers in a file called "scores.txt" that is located on my C drive at the address I listed in the program.
    I have been given this program by my instructor to open the file up so I can read it. It also has a while loop with an end-of file control within it that I don't really understand but I am working on that.
    I have defined a file pointer & I have connected that file with fopen() but it still cannot find it and displays "Error . . .etc".
    What am I doing wrong?
    Code:
    #include <stdio.h>
    
    int main(void)
    {
      FILE *scores;
      int score;
    
      scores = fopen("C:\\scores.txt","r");
      if (scores == NULL)
        printf("Error opening input file\n");
      else
        {
          while (feof(scores) <= 0)
    	{
    	  fscanf(scores,"%d",&score);
    	  printf("%d\n",score);
    	}
        }
      fclose(scores);
      system("pause");
      return(0);
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Try it without the path. Put the file in the same place as your executable. Read the FAQ on why you shouldn't use feof for loop control. Don't try to fclose NULL. You've already been told about not using system, but apparently don't care.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User ralphwiggam's Avatar
    Join Date
    Sep 2006
    Posts
    32
    I do care. I have read over my 3 previous threads and do not understand what using system is and I googled it "system" +problems +"C language" and "system +"C language" & I found nothing informative.
    I will do whatever u recommend.

    Looking up using system within this website.
    Last edited by ralphwiggam; 10-02-2006 at 08:11 AM.

  4. #4
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    system() is running an underlying operating system command. In your case "pause". This is inherently non-portable as not all operating systems will have a "pause" command, for example Unix operatings systems wouldn't understand it. Also, even on a PC, it would only work if the command is either (a) built-in, or (b) in the PATH.

  5. #5
    Registered User ralphwiggam's Avatar
    Join Date
    Sep 2006
    Posts
    32

    Okay

    I'm an idiot.
    It's worked for me in the past but I will do it.

  6. #6
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    You could always print out errno or use perror to see why the program can't find your file.

  7. #7
    Registered User ralphwiggam's Avatar
    Join Date
    Sep 2006
    Posts
    32
    Okay. . .tried it and it did not tell me anything. It no longer displays anything after it executes.
    Code:
    #include <stdio.h>
    #include <errno.h>
    
    int main(void)
    {
      FILE *scores;
      int score;
    
      scores = fopen("C:\\scores.txt","r");
    
      {
    	  fscanf(scores,"scores",&score);
    	  printf("scores",score);
       } 
      return(0);
    }
    Last edited by ralphwiggam; 10-02-2006 at 09:14 AM.

  8. #8
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    Code:
    #include <stdio.h>
    #include <errno.h>
    
    int main(void)
    {
      FILE *sfp;
      unsigned int score;
    
      sfp = fopen("C:\\scores.txt","r");
    
      if ( sfp != (FILE *) NULL )
      {
    /* Put a loop around this bit */
    	  fscanf(sfp, "%u", &score);
    	  printf("score %u\n",score);
    /* end of loop */
           fclose(sfp);
       } 
       else
       {
            /* find out what the error is ! */
            printf("open failed with errno=%d\n", errno);
       }
    
      return(0);
    }

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
      if ( sfp != (FILE *) NULL )
    You never need to typecast pointer comparisons to anything when comparing them to NULL.


    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    I know, I just prefer it stylewise.

  11. #11
    Registered User ralphwiggam's Avatar
    Join Date
    Sep 2006
    Posts
    32
    Thanks to both of you. I feel very stupid.
    Problem solved.
    Last edited by ralphwiggam; 10-02-2006 at 02:21 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  4. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  5. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM