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");
}