Thread: Opening files...

  1. #1
    prestomrmime
    Guest

    Opening files...

    I'm getting mad at my compiler. I can write this program without a problem and have it compile properly:

    Code:
    #include <stdio.h>
    main()
    {
    	FILE *fp = fopen("products.txt", "r");
    }
    I know it doesn't do anything, but it's a test. See in another program, I have this:

    Code:
    FILE *fp = fopen("products.txt", "rb");
    And I get this error:

    Improper use of a typedef symbol in function

    Can anyone tell me what the limitations are for that statement?

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    In the broken program, did you #include <stdio.h> ?

    Post more code from the broken program.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    prestomrmime
    Guest
    Well ok, here's the whole function. I do have the include statement, but the funny thing is that the program uses that syntax and same statement in a dozen other places in the program, and the compiler doesn't care. One wierd thing also, is that it's saying that a statement that clearly has a semicolon, does not.

    Here is the code for the products structure. It's located above the main function.

    Code:
    typedef	struct
    	{
    		int prodnum, prodtype, prodquan;
    		double prodcost, prodprice;
    		char proddesc[81];
    	} products;
    Code:
    void change()
    {
    
    
    	FILE *limitfile = fopen("limits.txt", "r");
    	double l1, l2, l3, l4, l5, l6, l7, l8, l9, l10;
    	/*Assign values to the limit variables.*/
    	fscanf(limitfile, "%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf", &l1, &l2, &l3, &l4, &l5, &l6, &l7, &l8, &l9, &l10);
    	fclose(limitfile);
    
    	products product_list3;
    	int y=0;
    	int x=0;
    	char z;
    	printf("\n\nEnter the record number you wish to change. : ");
    	scanf("%d", &y);
    	FILE *fp = fopen("products.txt", "rb");
    	fread(&product_list3, sizeof(product_list3), y, fp);
    	fclose(fp);
    
    	if (product_list3.prodtype == 0)
    	{
    		printf("\n\nRecord is deleted.  Would you like to restore it? y/n: ");
    		scanf("%c", &z);
    		if (z=='y')
    		{
    			printf("\n\nEnter a new product type : ");
    			product_list3.prodtype = getint(l3, l4);
    		}
    		else break;
    	}
    	else
    	{
    		printf("\n\nEnter the option you wish to change:");
    		printf("\n1. Product Number\n2. Product Type\n3. Product Description\n4. Product Quantity\n5. Product Cost\n6. Product Price.\n");
    		while (x<1 || x>6)
    		{
    			printf("\n:: ");
    			scanf("%d", &x);
    		}
    		printf("\n\nEnter new value. : ");
    		switch(x)
    		{
    			case 1 :
    				product_list3.prodnum = getint(l1, l2);
    				break;
    			case 2 :
    				product_list3.prodtype = getint(l3, l4);
    				break;
    			case 3 :
    				getstring(product_list3.proddesc);
    				strmorph(product_list3.proddesc);
    				break;
    			case 4 :
    				product_list3.prodquan = getint(l5, l6);
    				break;
    			case 5 :
    				product_list3.prodcost = getreal(l7, l8);
    				break;
    			case 6 :
    				product_list3.prodprice = getreal(l9, l10);
    				break;
    		}
    	}
    
    	fp = fopen("products.txt", "ab");
    	fwrite(&product_list3, sizeof(product_list3), y, fp);
    	fclose(fp);
    	printf("\n\nChange successful.");
    }

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Unless your compiler supports the latest C standard (C99), you must declare your variables at the top of the code block:
    Code:
    int foo()
    {
      /* Declare first */
      char a;
      int i;
    
      /* Now the code here */
    }
    >>else break;
    What are you break'ing out of ? That line is misplaced.

    >>fscanf(limitfile, "%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf", &l1, &l2, &l3, &l4, &l5, &l6, &l7, &l8, &l9, &l10);<<
    Sorry, but that is horrible Use an array to store the numbers.

    >>fread(&product_list3, sizeof(product_list3), y, fp);
    This won't work properly, y could be 5 (for example, meaning fread will load up 5 lots of data, each the size of 1 structure. This will obviously not fit into the single structure you are giving it.

    Tips:
    Always check that fopen() worked
    Always check that your IO reading functions worked
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    prestomrmime
    Guest
    Ok ok I know some of my code is crude, I'll get to that later.

    You were right, the problem was that the variables had to be declared at the top of all functions.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Opening ASCII files in C?
    By Mavix in forum C Programming
    Replies: 6
    Last Post: 04-25-2007, 02:23 PM
  2. Need help opening a series of files
    By ramparts in forum C Programming
    Replies: 9
    Last Post: 11-14-2006, 05:49 PM
  3. Opening files with UNICODE file names
    By decohk in forum Linux Programming
    Replies: 2
    Last Post: 11-09-2006, 05:25 AM
  4. opening files
    By angelic79 in forum C Programming
    Replies: 3
    Last Post: 10-19-2004, 06:52 AM
  5. Opening files - Giving options?
    By wwwGazUKcom in forum C++ Programming
    Replies: 3
    Last Post: 09-18-2001, 07:06 AM