Thread: Methods and file pointers

  1. #16
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by drater View Post
    But i do receive Warning: formal parameter 2 different from declaration.
    I was hoping you will check ALL the parameters types not only the first
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  2. #17
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Take a closer look at the definition and the prototype:
    Code:
    int fileScan(FILE** fp, char* cityName, int population, int squareMileage,
    			  int pollution, int crime, int expense, int highways);
    Code:
    int fileScan(FILE** fp, char cityName, int population, int squareMileage,
    			  int pollution, int crime, int expense, int highways);
    See that second argument there? Something is amiss there.
    Or you could just copy your entire function definition and replace the declaration with it. That will make sure they match 100%.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #18
    Registered User
    Join Date
    Feb 2008
    Posts
    47
    Alright, all my prototypes now match correctly and I no longer get any different from declaration errors. But for some reason when I compile, my program crashes after it outputs the table header (which is before my fileScan is implemented in my main)

  4. #19
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I think we're going to need a little more code that what you've shown.
    And I hope you fixed the scanf call too?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #20
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    have you fixed the fscanf code to receive pointers instead of integers?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #21
    Registered User
    Join Date
    Feb 2008
    Posts
    47
    All fixed. Thank you to everyone that contributed and helped me learn more about file pointers and header files.

  7. #22
    Registered User
    Join Date
    Feb 2008
    Posts
    47
    when doing something like this in main:

    Code:
    	/*	Checks if the last scanned livibility rating is less than the current	*/
    		if (&livabilityTemp < livability) 
    		{
    
    			compareInputFileValues(&livabilityTemp, livability, &cityNameTemp, cityName);
    		}
    and this as my function:

    Code:
    void compareInputFileValues(double *livabilityTemp, double livability, char *cityNameTemp[], char cityName[])
    {
    /*	Holds the value of the current city livibility rating in livibilityTemp,
    	if the current livibility rating(livibility) is larger than the highest
    	previous recording(livibilityTemp)	*/
    	*livabilityTemp = livability; 
    			
    /*	Creates a string copy of cityName if the current city livibility rating(livibility)
    	is larger than the	highest previous recording of livibility(livibilityTemp)	*/
    	strncpy( *cityNameTemp[], cityName[], 30 ); 
    }
    Would the file pointers go on the temp files only?
    Last edited by drater; 02-22-2008 at 03:09 AM. Reason: Added file pointers

  8. #23
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you have no file pointers in a sample. So what you are asking again?

    strncpy( cityNameTemp, cityName, 30 );

    check the strncpy prototype and compare it parameters to the variable types you provide to it
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  9. #24
    Registered User
    Join Date
    Feb 2008
    Posts
    47
    I editted my previous post and added the brackets to my char arrays and added file pointers. Did i do it correctly?

  10. #25
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I don't know how you've defined all those variables,
    livabilityTemp, livability, cityNameTemp, cityName,
    though they're probably in a post further back, so I can't check that right now.

    However, what is the point in passing in a temp argument when you're just taking a copy of the original value?
    double *livabilityTemp, double livability
    If the caller needs to have a copy, it can do it itself and if compareInputFileValues needs a copy, it too, can do it by itself. There's no need for that temp variable as I see.

    strncpy( *cityNameTemp[], cityName[], 30 );

    This is also wrong. Remove the []. [] should only be used if you need to access an index, a subscript. But since it's empty, it won't compile.
    ([] can be used in arguments, as you've seen, but never in the function body code.)
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #26
    Registered User
    Join Date
    Feb 2008
    Posts
    47
    I am reading from a file, and I have a number based on the set of numbers in the file. I am comparing each line for the largest number.

    Code:
    	if (&livabilityTemp < livability) 
    		{
    
    			compareInputFileValues(&livabilityTemp, livability, &cityNameTemp, cityName);
    		}

  12. #27
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you copy array of pointers to char into array of pointers to double using fucntion designed for copying strings. What except the crash do you want to receive as an output?

    passing pointer to &cityNameTemp you will definitely NOT receive and array of pointers.

    So start with showing variable declaration
    Then show function calls using this vars
    Then function prototypes and definitions
    Then explain what these functions suppose to do

    And I still do not see any file pointer.
    It should be declared as

    FILE* fPointer;
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  13. #28
    Registered User
    Join Date
    Feb 2008
    Posts
    47
    input/output/computation methods in separate file

    Code:
    void requestFileName(FILE **fp) 
    {   
    	char inputFilename[30];				// Character Array to hold the users specified input file name
    	printf("Enter the name of the file to be scanned: "); 	
    	scanf("%s", inputFilename);			// Scans the input file specified by the user 	
    	printf("\n");						// adds a space in between file names and file statistics 	
    	*fp = fopen(inputFilename, "r");	// open the file as read-only 
    }
    
    
    
    /*	Stores values from the input file into the variables cityName, population, squareMileafe, pollution,
    	crime, expense, and highways. */
    int fileScan(FILE *fp, char *cityName, int *population, int *squareMileage, int *pollution, int *crime, int *expense, int *highways)
    {
    	int fileScan = fscanf(fp, "%s %d %d %d %d %d %d", cityName, population, squareMileage, pollution, crime, expense, highways);
    	return fileScan;
    }
    
    void printCityTableHighestRating(int count, char cityNameTemp[], double livabilityTemp)
    {
    	printf("\n");	// adds a space in between the data table and table summary
    	printf("Of the %d cities, the most liveable was %s with a score of %3.2f \n",  count, cityNameTemp, livabilityTemp);
    	printf("\n");	// adds a space in between the files table summary and "Press any key to continue..."	
    }
    
    
    /*	Compares the current cities livibility rating(livibility) with the highest scanned
    	city livibility rating(livibilityTempt) and then stores the highest rating as
    	well as the name of that city to be used later in a summary report */
    void compareInputFileValues(double livabilityTemp, double livability, char cityNameTemp[], char cityName[])
    {
    /*	Holds the value of the current city livibility rating in livibilityTemp,
    	if the current livibility rating(livibility) is larger than the highest
    	previous recording(livibilityTemp)	*/
    	livabilityTemp = livability; 
    			
    /*	Creates a string copy of cityName if the current city livibility rating(livibility)
    	is larger than the	highest previous recording of livibility(livibilityTemp)	*/
    	strncpy( cityNameTemp, cityName, 30 ); 
    }
    main:
    Code:
    while(fileScan(fp, cityName, &population, &squareMileage, &pollution, &crime, &expense, &highways) != EOF)	// get next char from input file until we reach EOF
    	{
    
    
    			/*	Checks if the last scanned livibility rating is less than the current	*/
    		if (livabilityTemp < livability) 
    		{
    
    			compareInputFileValues(livabilityTemp, livability, cityNameTemp, cityName);
    		}
    	}
    	printCityTableHighestRating(count, cityNameTemp, livabilityTemp);
    	fclose(fp);	// close input file
    }
    I cut out some useless code in my main, for conserving space, but basically the program reads a text file filled with a city and then a bunch of numbers. It manipulates those numbers and stores them temporarily in a integer or double and then I print them out in a table like format. While I am doing this I want it to compare the last input for livability with the latest one to be scanned. If the newly scanned version is greater, it would store it as the new largest livability rating. This is so at the end of my program it can say, so-and-so-city had the best livability rating out of #of cities with a livability rating of #

  14. #29
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    if you want your compareInputFileValues to update the livabilityTemp variable it should be passed by the pointer
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  15. #30
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    As I see it, you have a multitude of problems in the code. Let's start small.
    I'll start with a suggestion here:
    Code:
    while(fileScan(fp, cityName, &population, &squareMileage, &pollution, &crime, &expense, &highways) != EOF)	// get next char from input file until we reach EOF
    	{
    
    
    			/*	Checks if the last scanned livibility rating is less than the current	*/
    		if (livabilityTemp < livability) 
    		{
    
    			compareInputFileValues(livabilityTemp, livability, cityNameTemp, cityName);
    		}
    	}
    	printCityTableHighestRating(count, cityNameTemp, livabilityTemp);
    	fclose(fp);	// close input file
    }
    I don't know how you did it in the original code, but this one is just confusing. You would think the bracket in green ends the while loop, but that's not true. In this code it's redundant (read: not used), so what might be better is this:
    Code:
    	while(fileScan(fp, cityName, &population, &squareMileage, &pollution, &crime, &expense, &highways) != EOF)	// get next char from input file until we reach EOF
    	{
    
    
    			/*	Checks if the last scanned livibility rating is less than the current	*/
    		if (livabilityTemp < livability) 
    		{
    
    			compareInputFileValues(livabilityTemp, livability, cityNameTemp, cityName);
    		}
    	}
    	printCityTableHighestRating(count, cityNameTemp, livabilityTemp);
    	fclose(fp);	// close input file
    Less confusion.

    Now, requestFileName wants to open the file the user inputs. I can understand you don't want to pass an array with the filename back, so rename the function and make sure it takes FILE** (or simply returns a FILE*) since it's going to have to pass that opened file stream back.

    Now for this:
    Code:
    int fileScan = fscanf(fp, "%s %d %d %d %d %d %d", cityName, population, squareMileage, pollution, crime, expense, highways);
    This doesn't work! You'll 99% likely to get a crash. Not to mention a warning or two.
    Fscanf reads information and you must therefore pass a pointer to the variables you want it to read into. See if you can fix that.

    Code:
    void printCityTableHighestRating(int count, char cityNameTemp[], double livabilityTemp)
    What's with the silly argument names? If it's going to print a city name, it should be named cityName and not cityNameTemp. The same goes for the last.

    And as for the function
    Code:
    void compareInputFileValues(double livabilityTemp, double livability, char cityNameTemp[], char cityName[])
    All I see is that it copies livability into livabilityTemp and cityName into cityNameTemp. In other words, the function is useless. Better to do that in the main code instead. Not to mention you aren't passing buffer sizes, so there's a good chance the function can do a buffer overrun. And that's very bad.

    And as for the main code:
    Code:
    while(fileScan(fp, cityName, &population, &squareMileage, &pollution, &crime, &expense, &highways) != EOF)
    Would you mind showing where you define all those variables?

    Oh and a very important point here. I thought I already mentioned it before.
    Code:
    scanf("%s", inputFilename);
    Never, ever read strings with scanf without specifying buffer size. Better yet, use fgets. Read my signature for more information.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  2. Class methods in header or source file?
    By TriKri in forum C++ Programming
    Replies: 13
    Last Post: 09-17-2007, 05:23 AM
  3. sequential file help plz
    By dutrachr in forum C Programming
    Replies: 4
    Last Post: 04-18-2006, 11:41 AM
  4. File I/O Question
    By Achy in forum C Programming
    Replies: 2
    Last Post: 11-18-2005, 12:09 AM
  5. Constructive Feed Back (Java Program)
    By xddxogm3 in forum Tech Board
    Replies: 12
    Last Post: 10-10-2004, 03:41 AM