Thread: C File Handling Problems: Non-Urgent

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    6

    C File Handling Problems: Non-Urgent

    At the moment, I'm stuck with this code.

    Code:
    /******************* PREPROCESSOR DIRECTIVES *************************
    */
    /******** HEADER FILES ****
    */
    #include <conio.h>     			 /* Console I/O Header  */
    #include <stdio.h>			 /* Standard I/O Header */
    #include <string.h>
    #include <ctype.h>
    #include <stdlib.h>
    #include <dos.h>
    #include <time.h>
    
    /**** NAMED CONSTANTS ***
    */
    
    #define filename "c:\\stock.dat"      /* Name of the data file on the disk */
    
    /********** USER-DEFINED AND ENUMERATED DATA TYPES *******************
    */
    
    struct stock_struct // User-Defined data structure
    {
    	char Product_Group_Code[20];
    	char Product_Code[15];
    	char Description[32];
    	int Stock_Level;
    } ;
    
    
    /******************* FUNCTION PROTOTYPES *****************************
    */
    
    	Mainmenu();
    	Flushkeyboardbuffer();
    	Create();
    	Addproducts();
    	Reload();
    	Displayoptions();
    	Saleofproducts();
    	void writeinfo(FILE*, struct stock_struct stock);
    	struct stock_struct getinfo();
    
    /********************* GLOBAL VARIABLES ******************************
    */
    
    char Selection;
    
    /******************** MAIN FUNCTION **********************************
    */
    void main ()
    {
    	clrscr();
    	Mainmenu();	
    
    }
    
    /******************** OTHER FUNCTIONS ********************************
    */
    
    void Mainmenu ()
    {
    
    	/* Keep at this screen till Selection is chosen */
    	do
    	{
    		clrscr();
    
    		/* Choices Selection */
    
    		gotoxy(25,4); textcolor(15); cprintf("Stock Control System");
    		gotoxy(22,6); textcolor(15); cprintf("Please select from the following options:");
    
    		gotoxy(25,8); textcolor(12); cprintf("1.	Create initial database");
    		gotoxy(25,10); textcolor(YELLOW); cprintf("2.	Add details of new products");
    		gotoxy(25,12); textcolor(12); cprintf("3.	Reload the System");
    		gotoxy(25,14); textcolor(YELLOW); cprintf("4.	Display Options");
    		gotoxy(25,16); textcolor(12); cprintf("5.	Sale of products by product description");
    		gotoxy(25,24); textcolor(2); cprintf("Please Make A Selection");
    
    		Selection = getch();
    
    		printf("%c", Selection);
    		getch();
    
    		Flushkeyboardbuffer();
    
           if (Selection == '1')
           {
    		  Create();	/* Go to One Element Module */
           }
           else
    	   if (Selection == '2')
    	   {
    	       Addproducts();	/* Go to Two Element Module */
    	   }
    	   else
    	   if (Selection == '3')
    	   {
    	       Reload();	/* Go to Two Element Module */
    	   }
    	   else
    	   if (Selection == '4')
    	   {
    	       Displayoptions();	/* Go to Two Element Module */
    	   }
    	   else
    	   if (Selection == '5')
    	   {
    	       Saleofproducts();	/* Go to Two Element Module */
    	   }
    
           } while(!(Selection >= '1' && Selection <= '6'));
    }
    
    void Flushkeyboardbuffer()
    {
    	while (kbhit()) getch(); /* Read and get rid of any previous keystrokes left in memory */
    }
    
    void Addproducts()
    {
    	struct stock_struct stockrec;
    	FILE *stockfile;
    	stockfile = fopen(filename, "a"); // The stockfile is linked to disk, then opened for append
    
    	stockrec = getinfo(); // Call function: Get's data from keyboard
    
    	writeinfo(stockfile, stockrec); // call function that writes to file
    
    	fclose(stockfile); // Closes stockfile
    }
    
    struct stock_struct getinfo()
    {
    
    	struct stock_struct stock; // Used to store input data
    	int arrayindex;
    	int strpos;
    	char temp;
    	clrscr();
    
    	gotoxy(10,2); textcolor(14); cprintf("Enter Details as follows: ");
    	gotoxy(14,4); textcolor(14); cprintf("Product Group Code: ");
    	gotoxy(40,4); gets(stock.Product_Group_Code);
    
    	gotoxy(14,6); textcolor(14); cprintf("Product Code: ");
    	gotoxy(40,6); gets(stock.Product_Code);
    
    	gotoxy(14,8); textcolor(14); cprintf("Description: ");
    	gotoxy(40,8); gets(stock.Description);
    
    	gotoxy(14,10); textcolor(14); cprintf("Stock Level: ");
    	gotoxy(40,10); gets(stock.Stock_Level);
    
    	return stock; // Data returned to module
    
    }
    
    
    
    
    
    void writeinfo(FILE *file_handle, stock_struct stock)
    {
    
    	fwrite(&stock, sizeof(stock), 1, file_handle);
    
    }
    I'm trying to get addproducts to write the input from the keyboard and place it into the file. I'm completely stumped at the moment though, and I keep getting type mismatch errors in TurboC++. Any help would be appreciated.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    1 - Void main is wrong. Main returns an integer. Read the FAQ.
    2 - Gets is bad, read the FAQ.
    3 - Gets reads strings, you're trying to read an integer.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    6
    Quote Originally Posted by quzah
    1 - Void main is wrong. Main returns an integer. Read the FAQ.
    2 - Gets is bad, read the FAQ.
    3 - Gets reads strings, you're trying to read an integer.


    Quzah.
    What do you suggest I change gets() to then? The FAQ says use fgets, but it causes more errors than gets does. I fixed the last gets which was with the integer, but past that I'm still lost. I'm still new to C.

    Also, removing void from main adds an error saying that the function should return a value.
    Last edited by EnderMB; 06-15-2006 at 04:04 PM.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It works exactly the same, except you supply the length of what you're reading, and where you're reading from.
    Code:
    fgets( tohere, thismuch, fromhere );
    So something like...
    Code:
    fgets( mystruct.mycharacterarray, lengthofarray, stdin );

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Quote Originally Posted by EnderMB
    Also, removing void from main adds an error saying that the function should return a value.
    Quote Originally Posted by quzah
    1 - Void main is wrong. Main returns an integer. Read the FAQ.
    The FAQ has your answers.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    6
    Quote Originally Posted by quzah
    It works exactly the same, except you supply the length of what you're reading, and where you're reading from.
    Code:
    fgets( tohere, thismuch, fromhere );
    So something like...
    Code:
    fgets( mystruct.mycharacterarray, lengthofarray, stdin );

    Quzah.
    Changed it, but it hasn't solved any of the errors. Now I have a problem with this bit of code, and it saying ") expected"

    Code:
    void writeinfo(FILE *file_handle, stock_struct stock)
    {
    
    	fwrite(&stock, sizeof(stock), 1, file_handle);
    
    }
    I understand that the FAQ helps, but it still doesn't solve the problems with my program, nor does it really help me build on what I've already made.
    Last edited by EnderMB; 06-15-2006 at 04:36 PM.

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    > Now I have a problem with this bit of code, and it saying ") expected"
    Well the function in question looks fine to me.

    Compilers will stop parsing code if something goes awfully wrong. Isn't there another error before that one?

  8. #8
    Registered User
    Join Date
    Apr 2006
    Posts
    6
    Quote Originally Posted by citizen
    > Now I have a problem with this bit of code, and it saying ") expected"
    Well the function in question looks fine to me.

    Compilers will stop parsing code if something goes awfully wrong. Isn't there another error before that one?
    Nope. It's the only error. Somehow, I feel that I'm doing things wrong though. All I want to do is to be able to write 4 different fields to a file, then increment, like a database.

  9. #9
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by EnderMB
    Nope. It's the only error.
    Why not post the current code?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  10. #10
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I really have a problem with your #define.
    Code:
    #define filename "c:\\stock.dat" /* Why is this not a const char[] ? */
    
    FILE *stockfile;
    stockfile = fopen(filename, "a");  
    /*  Since you never check to see if stockfile is a valid FILE pointer,
        we're not even sure if the file is open. *\

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    There's nothing wrong with defining a macro as a string literal. Personally I prefer the upper case macro naming style, but their use is perfectly legal. As a matter of fact, there are macro features that allow for manipulation of string literals as macros. # and ##

    They certianly wouldn't add them to the language if they weren't to be used. Although you could make a pretty good case regarding the inclusion of gets.


    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    How to use fgets():
    FAQ > How do I... (Level 1) > Get a line of text from the user/keyboard (C)

    Code:
    printf("%c", Selection);
    Why not use putchar()?

    The variable temp in getinfo() isn't used.

    You should try Dev-C++ instead of Turbo C++.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  13. #13
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Quote Originally Posted by EnderMB
    Changed it, but it hasn't solved any of the errors. Now I have a problem with this bit of code, and it saying ") expected"

    Code:
    void writeinfo(FILE *file_handle, stock_struct stock)
    {
    
    	fwrite(&stock, sizeof(stock), 1, file_handle);
    
    }
    I do have a problem with the function in question. Change "stock_struct stock" to "struct stock_struct stock". Or typedef your struct.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

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. Problems with file pointer in functions
    By willie in forum C Programming
    Replies: 6
    Last Post: 11-23-2008, 01:54 PM
  3. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  4. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  5. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM