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);See that second argument there? Something is amiss there.Code:int fileScan(FILE** fp, char cityName, int population, int squareMileage, int pollution, int crime, int expense, int highways);
Or you could just copy your entire function definition and replace the declaration with it. That will make sure they match 100%.
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)
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
All fixed. Thank you to everyone that contributed and helped me learn more about file pointers and header files.
when doing something like this in main:
and this as my function:Code:/* Checks if the last scanned livibility rating is less than the current */ if (&livabilityTemp < livability) { compareInputFileValues(&livabilityTemp, livability, &cityNameTemp, cityName); }
Would the file pointers go on the temp files only?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 ); }
Last edited by drater; 02-22-2008 at 03:09 AM. Reason: Added file pointers
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
I editted my previous post and added the brackets to my char arrays and added file pointers. Did i do it correctly?
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.)
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); }
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
input/output/computation methods in separate file
main: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 ); }
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 #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 }
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
As I see it, you have a multitude of problems in the code. Let's start small.
I'll start with a suggestion here:
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.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
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:
This doesn't work! You'll 99% likely to get a crash. Not to mention a warning or two.Code:int fileScan = fscanf(fp, "%s %d %d %d %d %d %d", cityName, population, squareMileage, pollution, crime, expense, highways);
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.
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.Code:void printCityTableHighestRating(int count, char cityNameTemp[], double livabilityTemp)
And as for the function
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.Code:void compareInputFileValues(double livabilityTemp, double livability, char cityNameTemp[], char cityName[])
And as for the main code:
Would you mind showing where you define all those variables?Code:while(fileScan(fp, cityName, &population, &squareMileage, &pollution, &crime, &expense, &highways) != EOF)
Oh and a very important point here. I thought I already mentioned it before.
Never, ever read strings with scanf without specifying buffer size. Better yet, use fgets. Read my signature for more information.Code:scanf("%s", inputFilename);