Thread: Why does this program not work on my laptop?!

  1. #1
    Registered User
    Join Date
    Mar 2006
    Location
    London
    Posts
    7

    Why does this program not work on my laptop?!

    Hi guys and gals,

    This program below is part of a program that I will eventually finish in the middle of the week. At the moment all it should do is read from a text file and return the data read to show it's done it successfully. I have written this on the engineering computers at Uni and it works fine, but when I came home to re-run it on my laptop, it does not open the txt file, instead always returning the error message I originally programmed!

    This is tres annoying, so any advice or comments on why this is so would be helpful.

    Thanks!

    Code:
    /**********************************************************************************/
    /* Description: This program will read x and y data values from a given text file */
    /* and will calculate and plot the best straight line fit for these data.         */
    /* Author: Edward King                                                            */
    /* Group: Yc                                                                      */
    /* Date: 10th March 2006                                                          */
    /**********************************************************************************/
    
    
    #include<stdio.h>
    #include<math.h>
    #include<grx20.h>
    
    #define MAX_SIZE 128
    
    float x[MAX_SIZE] ;                                                                                                                                             
    float y[MAX_SIZE] ; 
    int read_data_from_file(void) ; 
    
    #define NO_FILE -1
    
    int main(int argc, char *argv[])
    {
        int pairs_read ;    
    
    
    
    /******************************************************************************/
    /* Function Name: read_data_from_file                                         */
    /* Description: This function will read x and y data values from a text file. */
    /******************************************************************************/
    
        
    
        int read_data_from_file(void)
        {
          // OPENS A FILE:
          
          int i ;	
    
          FILE *fp ; 
          char file_name[MAX_SIZE] ; // Character array for the data filename.
    
          int data_read ;
          
          // Asks the user to enter the name and location of the data file.
          printf("Please enter location and filename of the datafile: ") ;
          scanf("%s" , file_name) ;
         
          fp = fopen(file_name , "r") ; // Opens the file. 
    
          if( fp == NULL ) ; // Checks for error in opening file.
          {
              printf("\nUnable to open file '%s'.\nCheck directory location and filename are correct.\n"
                     "Please close this program and start again.", file_name) ;
                   return NO_FILE ; // Stops program if there is an error in opening the file.
          }
    
          // READS DATA PAIRS FROM THAT FILE:  
        
          i = 0; // Initialises data pair count.
    
          while ( i < MAX_SIZE ) // Checks that i will fit into the array space.
          {
              data_read = fscanf( fp , "%f%f\n" , &x[i] , &y[i]) ;        		
    	
              // The above reads reads data from the file.	
              // 2 floating point numbers are read, x and y.
    		  // Loop continues while 2 data pairs are read and i < 128.
              		
              if( data_read != 2)
                  break ;
                  {
                    printf( "%8.3f %8.3f\n" , x[i] , y[i] ) ;  // Prints the data for user.
                    i++ ; // Increments the number of pairs read.
                  }
          } //End loop.
          
          fclose(fp) ; // Closes the file.
     
          return i ; // Retains the number of pairs read.
          
          } // END OF "read_data_from_file" FUNCTION.
     
     
    
    /***********************************************************************/
    /* Function Name: pairs_read                                           */
    /* Description: This function will print the number of pairs read from */
    /* the function "read_data_from_file" or an error message.              */
    /***********************************************************************/
          
          
          pairs_read = read_data_from_file() ;
          
          {
          if( pairs_read < 1 )
              {
              printf("\n\nNO DATA READ\n\n") ; //Defines error message.
              }
              else
              {
              //Prints number of pairs read.  
              printf("\n\n %d data pairs read from the file \n\n", pairs_read) ;
              }               
          // End IF statement.
          
          } // END OF "pairs_read" FUNCTION.
          
          system ("PAUSE");        
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > int read_data_from_file(void)
    Why is this function nested within main?
    C doesn't have nested functions.

    > if( fp == NULL ) ;
    You're not running the same code - if you were, then your uni compiler would have the same problem of always generating the error message.
    See that ; at the end - this makes the if statement a NOP and makes the following thing in braces always happen.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 04-09-2009, 02:31 AM
  2. Program doesnt work properly.
    By +Azazel+ in forum C Programming
    Replies: 4
    Last Post: 10-12-2007, 06:57 AM
  3. Why this program doesnt work properly
    By enggabhinandan in forum C Programming
    Replies: 12
    Last Post: 10-21-2006, 04:12 AM
  4. Can't get program to work
    By theirishrover12 in forum C Programming
    Replies: 1
    Last Post: 06-08-2002, 11:10 AM
  5. how does this program work
    By ssjnamek in forum C++ Programming
    Replies: 2
    Last Post: 01-02-2002, 01:48 PM