I am having a whole lot of trouble so far with this program. The program prompts the user for the name of a file, which contains floating point numbers, to be opened for reading and store the values into an array. The rest of the program is done...I think. My problem is that everytime I try to run the program it prints my error message for not being able to open the file and exits. Where did I mess up with the reading of the filename or opening the file or I dont know
I know the return values for fopen are EOF, NULL and I think 0 or nothing if it works but it still keeps tripping up on the NULL case I guess
This is one of my first programs and ANY help is much appreciated
THANKS
Ive tried many things so far and this is what I have at the moment.....
Code:#include <stdio.h> #include <stdlib.h> #define FLUSH while (fgetc(fileDataIn) != '\n') #define MAX_SIZE_NAME 100 #define MAX_SIZE_FILE 100 int main(void) { float data[MAX_SIZE_FILE]; char fileName[MAX_SIZE_NAME]; int fileLength; float h; int i; FILE *fileDataIn; int result; printf("Enter File Name: "); scanf("%s", &fileName); //open file fileDataIn = fopen(fileName, "r"); if (fileDataIn == NULL) { printf("Error: Could not open file %s for read.", fileName); exit(1); } //if else while (fscanf(fileDataIn, "%f", &h) != EOF) { for (i = 0; result = fscanf(fileDataIn, "%f", &h); i++) if (result != 1) { FLUSH; } //if data[i] = h; //should store each value read in into consecutive locations in data fileLength = i + 1; } // while fclose(fileDataIn); return 0; }



LinkBack URL
About LinkBacks



