Like Tree3Likes
  • 1 Post By claudiu
  • 2 Post By claudiu

Stock system

This is a discussion on Stock system within the C Programming forums, part of the General Programming Boards category; I have designed a stock system i can read and write to it but i dont know how to edit ...

  1. #1
    Registered User
    Join Date
    May 2012
    Location
    Cardiff, United Kingdom
    Posts
    20

    Stock system

    I have designed a stock system i can read and write to it but i dont know how to edit the data and create a stock level. The code i have is:

    Code:
    #include<stdio.h>#include<stdlib.h>
    #include<conio.h>
    #include<ctype.h>
    
    
    #define FALSE 0
    #define True !FALSE
    
    
    struct stock_data {
    	char name[30];
    	float buy_price;	
     
    	
    };
    	void write_info(void);
    	void read_info(void);
    	void edit_info(void);
    	
    
    
    	void main()
    	{
    		char c;
    		int done=FALSE;
    
    
    		while(!done)
    		{
    			puts("\nstock\n");
    			puts("A - Add New stock\n");
    			puts("L - List stock\n");
    			puts("Q - Quit\n");
    			printf("Your Choice:");
    
    
    			c = getch();
    			c = toupper(c);
    				
    			switch(c)
    			{
    			case('A'):
    					puts("Add New stock\n");
    					write_info();
    			case('L'):
    					puts("Lists stock\n");
    					read_info();
    			case('Q'):
    
    
    					puts("quit\n");
    					done = True;
    					break;
    			default:
    					puts("?");
    					break;
    			}
    		}
    	}
    	void write_info(void)
    	{
    		FILE *stocks;
    		struct stock_data stock;
    		char input[12];
    		printf("Enter stock name:");
    		gets(stock.name);
    		printf("how much did it cost?");
    		stock.buy_price = (float)atof(gets(input));
    		
    		
    
    
    		stocks = fopen("stock.txt","a");
    		if(stocks==NULL)
    		{
    			puts("error opening fie");
    			exit(1);
    		}
    
    
    		fwrite(&stock,sizeof(stock),1,stocks);
    
    
    		fclose(stocks);
    		puts("Stock updated");
    	}
    
    
    	void read_info(void)
    	{
    		FILE *stocks;
    		struct stock_data stock;
    		int x;
    
    
    		stocks = fopen("stock.txt","r");
    		if(stocks==NULL)
    		{
    			puts("No data in file");
    			return;
    		}
    
    
    		while(True)
    		{
    			x = fread(&stock,sizeof(stock),1,stocks);
    
    
    			if(x==0) break;
    
    
    			printf("\nStock name: %s\n",stock.name);
    			printf("Cost= %.2fGBP \n",stock.buy_price);
    		
    		}
    
    
    		fclose(stocks);
    	}

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Get rid of void main() replace it with int main(void) and return 0.

    Get rid of conio.h it is an old DOS header that is now obsolete.

    Get rid of gets() replace all gets() calls with fgets(). Look up its prototype and how to use it.

    Then come back and post your fresh code.
    Salem likes this.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    To edit existing data, read it into memory, make the changes and write the data back to the file. You need some data structure to keep your structs in.

  4. #4
    Registered User
    Join Date
    May 2012
    Location
    Cardiff, United Kingdom
    Posts
    20
    ok so ive changed gets to fgets but i dont have any idea how to edit the data in a file ive gone through books after books and websites just dont see to find it anyway .
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<ctype.h>
    
    #define FALSE 0
    #define True !FALSE
    
    struct stock_data {
    	char name[30];
    	float buy_price;	
     
    	
    };
    	void write_info(void);
    	void read_info(void);
    	void edit_info(void);
    	
    
    	void main()
    	{
    		char c;
    		int done=FALSE;
    
    		while(!done)
    		{
    			puts("\nstock\n");
    			puts("A - Add New stock\n");
    			puts("L - List stock\n");
    			puts("Q - Quit\n");
    			printf("Your Choice:");
    
    			c = getch();
    			c = toupper(c);
    				
    			switch(c)
    			{
    			case('A'):
    					puts("Add New stock\n");
    					write_info();
    			case('L'):
    					puts("Lists stock\n");
    					read_info();
    			case('Q'):
    
    					puts("quit\n");
    					done = True;
    					break;
    			default:
    					puts("?");
    					break;
    			}
    		}
    	}
    	void write_info(void)
    	{
    		FILE *stocks;
    		struct stock_data stock;
    		char input[12];
    		printf("Enter stock name:");
    		fgets(stock.name);
    		printf("how much did it cost?");
    		stock.buy_price = (float)atof(fgets(input));
    		
    		
    
    		stocks = fopen("stock.txt","a");
    		if(stocks==NULL)
    		{
    			puts("error opening fie");
    			exit(1);
    		}
    
    		fwrite(&stock,sizeof(stock),1,stocks);
    
    		fclose(stocks);
    		puts("Stock updated");
    	}
    
    	void read_info(void)
    	{
    		FILE *stocks;
    		struct stock_data stock;
    		int x;
    
    		stocks = fopen("stock.txt","r");
    		if(stocks==NULL)
    		{
    			puts("No data in file");
    			return;
    		}
    
    		while(True)
    		{
    			x = fread(&stock,sizeof(stock),1,stocks);
    
    			if(x==0) break;
    
    			printf("\nStock name: %s\n",stock.name);
    			printf("Cost= %.2fGBP \n",stock.buy_price);
    		
    		}
    
    		fclose(stocks);
    	}

  5. #5
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    First of all you are still using void main(). See my signature.

    Secondly, you do realize that writing to a file is one of the most common operations you have to do in programming. Just saying you couldn't find a way to do this is extremely suspicious. What exactly have you read about and why did it not help with your problem?
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  6. #6
    Registered User
    Join Date
    May 2012
    Location
    Cardiff, United Kingdom
    Posts
    20
    Ok I can write to a file and read from it but I don't know how to search for a piece of data write it into a temp file for editing then write it back into a the original file. Thabks

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,340
    > fgets(stock.name);
    Well in the last 3 days, you didn't bother to lookup how fgets() was declared (in the manual page), it has 3 parameters, not 1.
    It also means that you never bothered to compile this code either.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Not to mention you got rid of conio but are still using getch(). For God's sakes stop being lazy and fix this nonsense.
    memcpy and rags_to_riches like this.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stock Data
    By bskaer1 in forum C++ Programming
    Replies: 0
    Last Post: 10-16-2010, 11:59 PM
  2. Get stock quotes from google
    By rtbcoop in forum C++ Programming
    Replies: 8
    Last Post: 09-09-2009, 04:03 PM
  3. Stock Quotes
    By jmd15 in forum Networking/Device Communication
    Replies: 4
    Last Post: 08-31-2006, 04:35 PM
  4. amd stock fans suck!!
    By Kinasz in forum Tech Board
    Replies: 25
    Last Post: 05-05-2004, 09:14 AM
  5. Stock market
    By Liger86 in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 05-20-2003, 05:12 PM

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