Thread: file handling and function problem.

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    20

    file handling and function problem.

    this program is meant to read the binary file, display output on the screen then write the binary files contents to a text file.

    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    #define inFileName "parts.dat"
    #define outFileName "test.txt"
    
    typedef struct
    {
    	char description[21];
    	double costPrice;
    } item_struct;
    
    typedef struct
    {
    	char itemNumber[7];
    	item_struct item;
    	int qtyAvailable;
    	int reorderLevel;
    } parts_struct;
    
    
    parts_struct readPart(FILE *);
    void writePart(parts_struct, FILE *);
    void displayRecord(parts_struct );
    void callfunction ();
    
    int main()
    {
    	parts_struct part;
    	FILE *inFile, *outFile;
    
    	inFile = openFile(inFileName, "rb");
    	outFile = openFile(outFileName, "w");
    
    
    	part = readPart(inFile);
    	while(!feof(inFile))
    	{
    		
    		displayRecord(part);
    		callfunction();
    		part = readPart(inFile);
    		 
    	}
    	closeFile(inFile);
    	closeFile(outFile);
    
    	return 0;
    }
    
    
    void displayRecord(parts_struct part)
    {
    	printf("%s %-20s %6.2lf %4d %4d\n", part.itemNumber, part.item.description,
    	       part.item.costPrice, part.qtyAvailable, part.reorderLevel);
    }
    
    void writePart(parts_struct part, FILE *outFile)
    {
    	
    	fprintf(outFile, "%s %-20s %6.2lf %4d %4d\n", part.itemNumber, part.item.description,
    	       part.item.costPrice, part.qtyAvailable, part.reorderLevel);
    }
    
    parts_struct readPart(FILE *inFile)
    {
    	parts_struct part;
    	fread(&part, sizeof(part), 1, inFile);
    	return part;
    }
    
    void callfunction(void)
    {
    	parts_struct part;
    	FILE* outFile;
    	writePart(part, outFile);
    }

    This seems to be the problem

    Code:
    while(!feof(inFile))
    	{
    		
    		displayRecord(part);
    		callfunction();
    		part = readPart(inFile);
    		 
    	}
    I can get it to write to the text file if I call the writeParts function directly from within the while loop, but I dont understand why it wont work when I ask another function to call it. Pointless I know but Im just curious.

    the program reads the first record from then binary file then stops. So im guessing its my callfunction() thats a problem, most likely im not passing the variables correctly???

    Any ideas?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > while(!feof(inFile))
    Well the reasons why this is bad are in the FAQ.
    Perhaps you should start there.

    Code:
    void callfunction(void)
    {
    	parts_struct part;
    	FILE* outFile;
    	writePart(part, outFile);
    }
    Neither part or outFile are initialised.
    Try passing them as parameters.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    feof() is only bad IF you don't know how to use it!

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    20
    Thanks Salem,

    by passing parameters you mean passing the variables through the functions rather then declaring them in callfunction? if you understand that...

    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    #define inFileName "parts.dat"
    #define outFileName "test.txt"
    
    typedef struct
    {
    	char description[21];
    	double costPrice;
    } item_struct;
    
    typedef struct
    {
    	char itemNumber[7];
    	item_struct item;
    	int qtyAvailable;
    	int reorderLevel;
    } parts_struct;
    
    
    parts_struct readPart(FILE *);
    void writePart(parts_struct, FILE *);
    void displayRecord(parts_struct );
    void callfunction (parts_struct, FILE *);
    
    int main()
    {
    	parts_struct part;
    	FILE *inFile, *outFile;
    
    	inFile = openFile(inFileName, "rb");
    	outFile = openFile(outFileName, "w");
    
    
    	part = readPart(inFile);
    	while(!feof(inFile))
    	{
    		
    		displayRecord(part);
    		callfunction(part, outFile);		
                                    part = readPart(inFile);
    		 
    	}
    	closeFile(inFile);
    	closeFile(outFile);
    
    	return 0;
    }
    
    
    void displayRecord(parts_struct part)
    {
    	printf("%s %-20s %6.2lf %4d %4d\n", part.itemNumber, part.item.description,
    	       part.item.costPrice, part.qtyAvailable, part.reorderLevel);
    }
    
    void writePart(parts_struct part, FILE *outFile)
    {
    	
    	fprintf(outFile, "%s %-20s %6.2lf %4d %4d\n", part.itemNumber, part.item.description,
    	       part.item.costPrice, part.qtyAvailable, part.reorderLevel);
    }
    
    parts_struct readPart(FILE *inFile)
    {
    	parts_struct part;
    	fread(&part, sizeof(part), 1, inFile);
    	return part;
    }
    
    void callfunction(parts_struct part, outFile)
    {
    	writePart(part, outFile);
    }

    I think im getting confused with the function prototypes and headers because im getting a few errors when I try this.

    any ideas?

    Thanks alot.

  5. #5
    Registered User
    Join Date
    Sep 2004
    Posts
    20
    can anyone give me some help?

    I know its something to do with the way im incorrectly passing vairables down through the functions but i cant work it out

  6. #6
    Registered User
    Join Date
    Oct 2004
    Posts
    120
    1.) No need for callFunction. All it does is call the write function.

    2.) In callFuntion, you don't specify the type that outfile is. All parameters have to have their type declared.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > void callfunction(parts_struct part, outFile)
    You missed the type
    void callfunction(parts_struct part, FILE *outFile)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Sep 2004
    Posts
    20
    Thanks guys.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating File Handling Functions
    By td4nos in forum C Programming
    Replies: 6
    Last Post: 06-26-2009, 11:43 AM
  2. File names as function arguments
    By knowNoC in forum C Programming
    Replies: 2
    Last Post: 04-29-2009, 12:15 AM
  3. basic file handling problem
    By georgen1 in forum C Programming
    Replies: 4
    Last Post: 03-05-2009, 06:21 AM
  4. File Handling Problem
    By Erkan in forum C Programming
    Replies: 4
    Last Post: 10-25-2005, 06:29 AM
  5. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM