Methods and file pointers

This is a discussion on Methods and file pointers within the C Programming forums, part of the General Programming Boards category; I am pretty confident that I wrote my requestFileName correctly, but my program crashes every time I run it, so ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    47

    Methods and file pointers

    I am pretty confident that I wrote my requestFileName correctly, but my program crashes every time I run it, so I am assuming I wrote out fileScan incorrectly.


    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. */
    void fileScan(FILE **fp, char cityName, int population, int squareMileage, int pollution, int crime, int expense, int highways)
    {
    	fscanf(fp, "%s %d %d %d %d %d %d", cityName, population, squareMileage, pollution, crime, expense, highways);
    }

    In my main method I call fileScan like:

    Code:
    while(fileScan(&fp, cityName, population, squareMileage, pollution, crime, expense, highways) != EOF)	// get next char from input file until we reach EOF
    	{
    //...
             }

    What am i doing incorrectly?

  2. #2
    Aia
    Aia is offline
    Beautiful to C Aia's Avatar
    Join Date
    Jun 2007
    Posts
    124
    Code:
    void requestFileName(FILE *fp)  /* remove one  * */
    {   
    	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);
    	printf("\n");						// adds a space in between file names and file statistics 	
    	fp = fopen(inputFilename, "r");	 /* remove the * in *fp */
    }
    
    /*
     * remove the extra * in fp and add to char *cityName 
     */
    void fileScan(FILE *fp, char *cityName, int population, int squareMileage, int pollution, int crime, int expense, int highways)
    {
    	fscanf(fp, "%s %d %d %d %d %d %d", cityName, population, squareMileage, pollution, crime, expense, highways);
    }

    In my main method I call fileScan like:

    Code:
    /*
     * remove the extra & at fp
     *
     * fileScan does return any value therefore while can't work checking if it is the EOF
     * population, squareMilege, pollution, crime, expense, highways are not the same that in the function;
     * those are a copy
     */
    while(fileScan(fp, cityName, population, squareMileage, pollution, crime, expense, highways) != EOF)	// get next char from input file until we reach EOF
    	{
    //...
             }
    Last edited by Aia; 02-21-2008 at 09:13 PM.
    When the eagles are silent, the parrots begin to jabber. ~Winston Churchill

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    47
    Quote Originally Posted by Aia View Post
    Code:
    void requestFileName(FILE *fp)  /* remove one  * */
    {   
    	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);
    	printf("\n");						// adds a space in between file names and file statistics 	
    	fp = fopen(inputFilename, "r");	 /* remove the * in *fp */
    }
    My professor told me i needed to use FILE **fp. :\ but maybe I misunderstood.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If you're supposed to use FILE **fp, then make sure your fscanf has *fp as the first argument.

    Edit: although I can't think of a good reason (or, for that matter, any reason at all, really) for you to need **fp here. (The whole point of everything in C using FILE * was that you could pass it around functions and everything would work the way you expect.)
    Last edited by tabstop; 02-21-2008 at 09:44 PM.

  5. #5
    Registered User
    Join Date
    Feb 2008
    Posts
    47
    I also get this as a warning:

    "Warning 3 warning C4013: 'fileScan' undefined; assuming extern returning int"

    Is it not working correctly because fileScan is not returning the correct declaration?

  6. #6
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Code:
    fscanf(fp, "%d", &population);
    for most formats (except %s) you need to add & before var - because scanf need a pointer to var where it will store the parsed value
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by drater View Post
    I also get this as a warning:

    "Warning 3 warning C4013: 'fileScan' undefined; assuming extern returning int"

    Is it not working correctly because fileScan is not returning the correct declaration?
    It means just what it says: you haven't defined a function called fileScan.

    Now you may have done so later in the file, but by then the damage is done. You did remember to put a function prototype of fileScan at the top of the file, didn't you?

  8. #8
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    'fileScan' is not returning any return code - it is defined as void
    if you want to return EOF on error - change you 'fileScan' function to return it if fcanf failed
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  9. #9
    Registered User
    Join Date
    Feb 2008
    Posts
    47
    I took out the void, and Now i am getting 2 warnings which I don't understand:
    Warning 3 warning C4028: formal parameter 1 different from declaration
    Warning 4 warning C4028: formal parameter 2 different from declaration


    Code:
    /*	Requests the user to input a valid file name to be scanned */
    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. */
    fileScan(FILE *fp, char cityName, int population, int squareMileage, int pollution, int crime, int expense, int highways)
    { /*     BOTH ERRORS APPEAR HERE     */
    
    	fscanf(fp, "%s %d %d %d %d %d %d", cityName, population, squareMileage, pollution, crime, expense, highways);
    }

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,772
    Reading strings with scanf calls for extra caution. I suggest you read this:
    Need help with altering info on a txt file

    Quote Originally Posted by drater View Post
    I took out the void, and Now i am getting 2 warnings which I don't understand:
    Warning 3 warning C4028: formal parameter 1 different from declaration
    Warning 4 warning C4028: formal parameter 2 different from declaration
    It means your definition and declaration doesn't match.
    And remember that all functions should return something. Not nothing. If it doesn't need to return anything, it should return void. In your case, it should return int.
    And as mentioned, update your prototype to match the new definition.
    Last edited by Elysia; 02-21-2008 at 11:10 PM.
    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. #11
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    do not just remove void - replace it by the proper type for example int

    show the following:
    function prototype
    function definition
    function call
    mark line where the error occured
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by drater View Post
    I took out the void, and Now i am getting 2 warnings which I don't understand:
    Warning 3 warning C4028: formal parameter 1 different from declaration
    Warning 4 warning C4028: formal parameter 2 different from declaration


    Code:
    /*	Requests the user to input a valid file name to be scanned */
    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. */
    fileScan(FILE *fp, char cityName, int population, int squareMileage, int pollution, int crime, int expense, int highways)
    { /*     BOTH ERRORS APPEAR HERE     */
    
    	fscanf(fp, "%s %d %d %d %d %d %d", cityName, population, squareMileage, pollution, crime, expense, highways);
    }
    Try again, and this time pay attention: you do not have a function prototype for fileScan. Therefore your code will not work. Put a function prototype for fileScan at the top of the file, or put it in an included header file.

  13. #13
    Registered User
    Join Date
    Feb 2008
    Posts
    47
    Code:
    /* prototype call */
    
    fileScan(FILE **fp, char *cityName, int population, int squareMileage,
    			  int pollution, int crime, int expense, int highways);

    Code:
    /*  Main Call */
    while(fileScan(fp, cityName, population, squareMileage, pollution, crime, expense, highways) != EOF)	// get next char from input file until we reach EOF
    	{
                //...
             }
    Code:
    /* method */
    fileScan(FILE *fp, char cityName, int population, int squareMileage, int pollution, int crime, int expense, int highways)
    { /*ERRORS: Warning 3 warning C4028: formal parameter 1 different from declaration
    Warning 4 warning C4028: formal parameter 2 different from declaration */
    	fscanf(fp, "%s %d %d %d %d %d %d", cityName, population, squareMileage, pollution, crime, expense, highways);
    }

  14. #14
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    do not you see a difference?

    fileScan(FILE **fp
    fileScan(FILE *fp

    as I said - make it return int

    and actually do not forget to add return statement to the function
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  15. #15
    Registered User
    Join Date
    Feb 2008
    Posts
    47
    I put int in both my prototype call and function call and added a return statement, I no longer receive:
    Warning: formal parameter 1 different from declaration,
    But i do receive Warning: formal parameter 2 different from declaration.

    Also I would like to thank everyone who has responded and put up with my stupidity.

Page 1 of 3 123 LastLast
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-17-2005, 11:09 PM
  5. Constructive Feed Back (Java Program)
    By xddxogm3 in forum Tech Board
    Replies: 12
    Last Post: 10-10-2004, 03:41 AM

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