Thread: Files problem

  1. #1
    Registered User
    Join Date
    Nov 2007
    Location
    Ireland
    Posts
    23

    Files problem

    I must write a prog which is used to calculate vat rates on a product. I have two files

    (These files are written by me and can be layed out any way. I just done them them this way thinking it would be easiest.
    productVat - This is the catagories and the vat rates applying
    Code:
    1	21	Hardware
    2	21	Electrical
    3	18	Appliances
     4	0	Food
    products - This is a list of products. They are in the catagorie, the price, the product order
    Code:
    4	1	Milk
    3	254	Dryer
    3	114	Hoover
    1	100	Drill
    2	3	Plug
    4	2	Bread
    3	98	Microwave
    1	10	Hammer
    2	1	Fuse	
    1	8	Screwdriver
    2	7	Socket
    4	3	Eggs
    The first thing i want to do, and am failing miserably at is opening the files. I want to just print them to screen first to make sure they are opening but I cant get it to work for some reason.

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <errno.h>
    
    /*
    	The cat struct is used for holding details of categories.
    */
    
    struct cat
    {
    	int id;
    	char description[25];
    	float vatrate;
    	
    };
    
    /*
    	The item struct is used to describe a product
    */
    
    struct item
    {
    	
    	char  productname[25];
    	float price;
    	int catid;
    	
    };
    
    void welcome();
    
    typedef struct item product;
    typedef struct cat category;
    
    #define	 productvatfilename "C:\\Documents and Settings\\Jerry\\Desktop\\College\\Prog\\Ass\\Code Warrior\\productVat.txt"
    #define productvatpermission "r"
    
    #define	 productsfilename "C:\\Documents and Settings\\Jerry\Desktop\\College\\Prog\\Ass\\Code Warrior\\products.txt"
    #define productspermission "r"
    
    
    int main ()
    {
            FILE * productvatfile;	//pointer to productvat.txt                      
    	FILE * productsfile;	//pointer to products.txt
    
    
    	productvatfile = fopen(productvatfilename,productvatpermission);	//Open the catfile with read permission
    	productsfile = fopen(productsfilename,productspermission);			//Open the items list with read permission
    	
    
    	welcome();
    }
    
    void welcome()
    {
    	printf ("\t\t\tWelcome\n");
    }
    This much is working (i think). how can i just print the details to screen to make sure they are opening?
    Last edited by Signpost; 11-27-2007 at 06:42 PM.

  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
    > College\Prog
    You're missing \ here (and other places as well).

    > how can i just print the details to screen to make sure they are opening?
    Read the FAQ on using fgets().
    But first, check the return result of the fopen() call.
    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
    Nov 2007
    Location
    Ireland
    Posts
    23
    Yes its a college prog. I dont want my hw done for me. I genuinely need help with it thou. Finding it a very difficult language compared to C++.
    how can i check the result? thats what im not sure of.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    I find an if statement works quite well.
    Start by reading the manual page for fopen to find out what it returns.
    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.

  5. #5
    Registered User
    Join Date
    Nov 2007
    Location
    Ireland
    Posts
    23
    why wont a
    Code:
     printf productsfile;
    work?

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Quote Originally Posted by Signpost View Post
    why wont a
    Code:
     printf productsfile;
    work?
    Try compiling it and see. Also I'd double check your filenames, as both are missing two or three backslashes.

  7. #7
    Registered User
    Join Date
    Nov 2007
    Location
    Ireland
    Posts
    23
    it tells me its expecting a ; but i cant see why. ya i have them fixed actually. must change them above. I know that method is prob pointless but i really cannot understand the files in c

  8. #8
    Registered User
    Join Date
    Nov 2007
    Location
    Ireland
    Posts
    23
    ok. My files are properly opening now.
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <errno.h>
    
    /*
    	The cat struct is used for holding details of categories.
    */
    
    struct cat
    {
    	int id;
    	char description[25];
    	float vatrate;
    	
    };
    
    /*
    	The item struct is used to describe a product
    */
    
    struct item
    {
    	
    	char  productname[25];
    	float price;
    	int catid;
    	
    };
    
    void welcome();
    
    
    typedef struct item product;
    typedef struct cat category;
    
    #define	 VRATES "productVat.txt"
    #define VRATESpermission "r"
    
    #define	 PRODUCT "products.txt"
    #define PRODUCTpermission "r"
    
    
    int main ()
    {
    	//int k;	  
    	//char buf[BUFSIZ] = "Garbage";
    
    	FILE *prods;
    	FILE *rates;
    	
    	  if ((prods = fopen(PRODUCT, "r")) == NULL)
      {
        perror (PRODUCT);
        return (EXIT_FAILURE);
      }
      
        if ((rates = fopen(VRATES, "r")) == NULL)
      {
        perror (VRATES);
        return (EXIT_FAILURE);
      }
      
      /*   k = 0;
        
        while (fgets(buf, sizeof(buf), rates) != NULL)
      {
        printf (buf);
        k++;
      }*/
    
    	welcome();
    
    	fclose(prods);
    	fclose(rates);
    }
    
    void welcome()
    {
    	printf ("\t\t\t\tWelcome\n");
    }
    How can i now load this data into my structs? I want the spaces to distinguish the different fields in the struct. I was thinking of using something like the following:
    Code:
    	//Product array - holds all products in the shop
    	product productsarray[100];
    	//category array - used to hold the categories
    	category categoryarray[100];
    	//basket array holds the items they buy
    	product basket[100];
    but i cant figure out how to do it like this properly. Any help appreciated!

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > printf (buf);
    NEVER pass a format string to printf that you have no control over. For example, all you would need is a single &#37; character in your file, and your program is toast.

    Use
    puts(buff);
    printf("%s",buff);

    As for the rest, you were on the right track
    Code:
    while ( k < 100 && fgets(buf, sizeof(buf), rates) != NULL) {
      if ( sscanf( buff, "%d", &myint ) == 1 ) {
        // assign to array
        k++;
      }
    }
    Change the sscanf line to match the format of the data in the file.
    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.

  10. #10
    Registered User
    Join Date
    Nov 2007
    Location
    Ireland
    Posts
    23
    how can i change the scanf line to suit? I know in C++ i could just assign the items in the file to variables and call them into an array. Whats a template func to add them in c?

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Read the manual page for sscanf.
    You can string together several conversions at the same time if you want.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem compiling files that store functions
    By tiachopvutru in forum C++ Programming
    Replies: 10
    Last Post: 05-30-2008, 05:42 PM
  2. Problem with Header files.
    By chakra in forum C Programming
    Replies: 5
    Last Post: 05-10-2008, 01:30 PM
  3. Strange problem with classes in header files
    By samGwilliam in forum C++ Programming
    Replies: 2
    Last Post: 02-29-2008, 04:55 AM
  4. problem with sending files to a class method..
    By utoots in forum C++ Programming
    Replies: 5
    Last Post: 04-02-2003, 01:38 AM
  5. problem with resource files
    By nickeax in forum Windows Programming
    Replies: 1
    Last Post: 03-14-2003, 02:32 AM