Results 1 to 7 of 7

Thread: Im not sure whats wrong wth the code

  1. #1

    Thread Starter
    Member
    Join Date
    Jan 2011
    Posts
    40

    Im not sure whats wrong wth the code

    Code:
    //constants
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <string.h>
    #define RANDOM 256
    #pragma warning(disable: 4996)
    
    int main(int argc, char *argv[])
    {
    	//declarations
    	char MiscNumbers[RANDOM];
    	int data = 0;
    	int sum = 0;
    	int range = 0;
    	float mean = 0.0;
    	float variance = 0.0;
    	float deviation = 0.0;
    
    	FILE *fp;
    	FILE * outFileHandle = NULL;
    	
    	fp = fopen("\\zeus\profile$\955290767\Desktop\StdDev\one.dat" , "r");
    	
    	if (fp == NULL)
    	{
    		strncpy(one.dat, argv[1], RANDOM);
    	}
    	else
    	{
    		puts("Enter the name of the file to read (source):");
    		fgets(one.dat, RANDOM, stdin);
    		if(one.dat[strlen(one.dat) - 1 == '\n')
    				prog2a[strlen(prog2a) - 1] = '\0';
    		else
    				while(getchar() != '\n');
    	}
    
    	fp = fopen(argc[0], "r");
    	if(fp == NULL)
    	{
    			printf("Could not open file %s for input.\n"
    					 "Press any key to Continue" , one.dat );
    			getchar();
    			return EXIT_FAILURE;
    	}
    
    
    
    	while (fscanf(fp, "%d" , &numberInSet) != EOF)
    	{
    		printf("The results from file dat1.dat are: /n");
    		printf("--------------------------------------- /n");
    		printf("| # of data | Sum  |   Range    | Mean   | variance  | standard deviation | /n");
    		
    		for (i=0; i<numberInSet; i++)
    		{
    			printf("| %d        | %d   |   %d to %d | %f     | %f        | %f                 | /n");
    		}
    	}
    	
    	//calculations
    	//bring the numbers from the file copy it to the arrays and store it. then
    	//close the file and calculate all the rest
    
    	getchar();
    	return EXIT_SUCCESS;
    I know i havent worked on the calculations yet but i asked the compiler to bring a file from a place but that file name Which is one.dat, compiler cant seem to find it or something cause its unidentified.

  2. #2
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Im not sure whats wrong wth the code

    Are you saying that it fails to open one.dat? Is that network resource accessible from your computer? Can you take that very path and navigate to it in your OS?
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  3. #3

    Thread Starter
    Member
    Join Date
    Jan 2011
    Posts
    40

    Re: Im not sure whats wrong wth the code

    the one.dat, "one" part is highlighted in red

  4. #4
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Im not sure whats wrong wth the code

    I take it you are using visual studio?
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  5. #5
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Im not sure whats wrong wth the code

    What is the error message?
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  6. #6
    Junior Member
    Join Date
    May 2011
    Posts
    23

    Re: Im not sure whats wrong wth the code

    Are you certain you have the file in the location you are trying to open it from?
    Furthermore, it would help if you said what the error message says, as I cannot solve a problem when I don't quite understand what the problem is.

  7. #7
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Im not sure whats wrong wth the code

    at first I thought it was the fopen command where it was failing... it isn't.... the problem is this line:
    fgets(one.dat, RANDOM, stdin);

    you need to look at the documentation for fgets... I'm quite certain that the first value isn't correct... I think it's supposed to be the stream to the file... in this case ,that's fp ....


    wait.... wait.... ughnrgh..... you're using one.dat like it means something.... you've used it in the strncpy and other places... it doesn't work like that... one.dat is just a name of a file... it has no meaning to the code.

    in addition, some of your logic doesn't make sense... you're opening a file on the network... if it can't you're doing a string copy... if it can, you're then asking them for the file to open? whaaa? The nyou try to open that file with the same file pointer as the first one with out first closing the original file pointer?

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width