Trouble with file input

This is a discussion on Trouble with file input within the C Programming forums, part of the General Programming Boards category; I am writing a program that scans data from a file into an array. It then prints out the contents ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    36

    Trouble with file input

    I am writing a program that scans data from a file into an array. It then prints out the contents of the file and finds the average.

    It compiles and builds without error, but for some reason Microsoft Visual C++ cannot locate my file. The file is exactly where i said it is in the program, and I have switched computers to make sure, and it still will not read my file.

    Is there something wrong in my code that could be keeping it from finding my file?

    Code:
    #include <stdio.h>
    #define MAXSIZE 100
    
    double arrayAverage(double dArray[MAXSIZE], double delements);
    
    int main (void)
    {
    	int i;
    	double delements, dArray[MAXSIZE], dAverage;
    	FILE *pIn;
    
    	pIn = fopen("H:\\CS 1003\\practice\\numbers.txt", "r");
    
    	if (pIn == NULL);
    	{
    		printf("Unable to open file.\n");
    		return(-1);
    	}
    
    	i=0;
    	fscanf(pIn, "%lf", &dArray[i]);
    	while (dArray[i] != EOF)
    	{
    		delements = i+1;
    		i++;
    		fscanf(pIn, "%lf", &dArray[i]);
    	}
    
    	printf("Original series of numbers is as follows:\n");
    
    	for (i=0; i<delements; i++)
    	{
    		printf("%f\n", dArray[i]);
    	}
    
    	dAverage = arrayAverage(dArray, delements);
    	printf("\nArray average is %f.", dAverage);
    
    	fclose(pIn);
    
    	return (0);
    }
    
    
    double arrayAverage(double dArray[MAXSIZE], double delements)
    {
    	double dAverage, dSum;
    	int i;
    
    	dSum=0;
    	i=0;
    	while (i<delements)
    	{
    		dSum+=dArray[i];
    		i++;
    	}
    
    	dAverage = dSum/delements;
    
    	return (dAverage);
    }

  2. #2
    Just kidding.... fnoyan's Avatar
    Join Date
    Jun 2003
    Location
    Still in the egg
    Posts
    269
    hi
    Code:
    	if (pIn == NULL); <-- this shouldn't be here
    	{
    		printf("Unable to open file.\n");
    		return(-1);
    	}
    take care on the if statement. there mustn't be ";"

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    36
    Nope, that didn't seem to do it...

    I've tried the rest of my code by getting rid of the file input stage and manually entering the numbers, and it works fine.

    But why this is not working boggles my mind.
    Last edited by w274v; 12-17-2005 at 04:34 PM.

  4. #4
    Just kidding.... fnoyan's Avatar
    Join Date
    Jun 2003
    Location
    Still in the egg
    Posts
    269
    hi

    yes, the ";" makes the printf set unreleted to if statement. That's why printf statement will always be evaluated regarless of the value of pIn. Check you C book!
    Code:
    #include <stdio.h>
    #define MAXSIZE 100
    
    double arrayAverage(double dArray[MAXSIZE], double delements);
    
    int main (void)
    {
    	int i;
    	double delements, dArray[MAXSIZE], dAverage;
    	FILE *pIn;
    
    	pIn = fopen("./numbers.txt", "r");
    
    	if (pIn == NULL)
    	{
    		printf("Unable to open file.\n");
    		return(-1);
    	}
    
    	i=0;
    	fscanf(pIn, "%lf\n", &dArray[i]);
    	while (dArray[i])
    	{
    		delements = i+1;
    		i++;
    		fscanf(pIn, "%lf\n", &dArray[i]);
    	}
    
    	printf("Original series of numbers is as follows:\n");
    
    	for (i=0; i<delements; i++)
    	{
    		printf("%f\n", dArray[i]);
    	}
    
    	dAverage = arrayAverage(dArray, delements);
    	printf("\nArray average is %f.", dAverage);
    
    	fclose(pIn);
    
    	return (0);
    }
    
    
    double arrayAverage(double dArray[MAXSIZE], double delements)
    {
    	double dAverage, dSum;
    	int i;
    
    	dSum=0;
    	i=0;
    	while (i<delements)
    	{
    		dSum+=dArray[i];
    		i++;
    	}
    
    	dAverage = dSum/delements;
    
    	return (dAverage);
    }
    Since you are using fscanf(), you must care about format of your txt file. In this code, i added "\n" to your fscanf() statement. And the format of numbers.txt is as follows
    Code:
    10
    41
    5
    7

  5. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    36
    i dont think you understood me. i removed the ";" and it still didnt work

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    So what exactly does "didn't work" mean? Try this link for some good reading, which if followed, will completely avoid this type of post, and yours for that matter, in the future.

    To all the would-be whiners out there, who don't like my link, chew on this: I have actually solved my problems before by taking the time to form them into proper questions to ask someone. By taking the time to write up what it's doing, what it isn't, what it should be, etc, and forming it into a post, I have worked through the issue on more than one occasion, so I never ended up having to ask. On more than one occasion. It's a great practice to get into. Good questions aid in getting good answers.


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

  7. #7
    Just kidding.... fnoyan's Avatar
    Join Date
    Jun 2003
    Location
    Still in the egg
    Posts
    269
    "w274v", are you kiddind with me :P. the code i posted was compiled (with gcc under linux, so i think VC++ will also give no error mesaage) succesfully and worked with no error. Prints the values in the 'numbers.txt' and then prints out the avarage...

    With ";" after if statement, your program never goes beyond exit(-1). Evaluates if statement, and then evaluates the printf and exit functions respectively. The things I have just described are THE BASICS OF C! Read your C book or get a new one....

    See the reading Quzah suggested!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. fscanf in different functions for the same file
    By bchan90 in forum C Programming
    Replies: 5
    Last Post: 12-03-2008, 08:31 PM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 01:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21