Thread: file opening?

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    255

    file opening?

    ok im writing a program for school and the book said to open files one way and the compiler gave me an error i messed with and changed it to a different way and it ran but then crashed trying to open the file so i must be getting half way right but im not sure?

    and also yes im aware you people hate gets() but i ahve to use it for school so deal with it. and if this isnt formatted 100% like C its probably cause this on a .cpp file and also our teacher doesnt really care as this is mainly a class on data structures but with a new dialect and that is C. and im using // comments not old /**/ so dont kill me for it.

    however logic and other errors feel free to let me have it.

    Code:
    #include <ctype.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    struct data
    {
    	int cust_id;
    	char cust_name[30];
    	char state[3];
    	char dis_code;
    	double balance;
    	int outstanding_orders;
    };
    
    int get_int(char msg[]);
    double get_double(char msg[]);
    void printfile(data records[],int elementsize);
    int binary(data records[], int elementsize, char cust_id_search);
    
    
    int main()
    {
    	data records[12];
    	int response;
    	int elementsize = 0;
    	int found;
    	int cust_id_search;
    	char filename[30];
    	char buf[10];
    	FILE *fp;
    
    	response = get_int("ENTER A CHOICE(use the numbers):\n1.LOAD THE FILE\n2.LIST THE FILE\n3.SEARCH THE FILE\n4.ADD A RECORD\n5.EXIT THE PROGRAM\n");
    	while(response != 5)
    	{
    		if(response == 1)
    		{
    			
    			printf("Enter the name of the file: \n");
    			gets(filename);
    			//fp = fopen(filename,"r+");
    			//if(fp == NULL)
     //the above two lines fix the problem but it crashes when
     //i try and open file
    			if(fp = fopen(filename,"r+")== NULL)//ERROR 1
    			{
    				printf("CANNOT OPEN FILE\n");
    			}
    			else
    			{
    				//fp = fopen(filename,"r+");
    				fscanf(fp,"%d%s%s%c%f%d",records[elementsize].cust_id,
    					records[elementsize].cust_name,
    					records[elementsize].state,
    					records[elementsize].dis_code,
    					records[elementsize].balance,
    					records[elementsize].outstanding_orders);
    				while(!feof(fp))
    				{
    					elementsize++;
    					fscanf(fp,"%d%s%s%c%f%d",records[elementsize].cust_id,
    					records[elementsize].cust_name,
    					records[elementsize].state,
    					records[elementsize].dis_code,
    					records[elementsize].balance,
    					records[elementsize].outstanding_orders);
    				}
    			}
    		}
    		else if(response == 2)
    		{
    			printfile(records,elementsize);
    		}
    		else if(response == 3)
    		{
    			cust_id_search = get_int("Enter the customer ID youd like to search for? \n");
    			found = binary(records,elementsize,cust_id_search);
    			if(found == -1)
    				printf("DATA NOT FOUND\n");
    			else 
    				printf("%d\n%s\n%s\n%c\n%f\n%d",records[found].cust_id,
    				records[found].cust_name,
    				records[found].state,
    				records[found].dis_code,
    				records[found].balance,
    				records[found].outstanding_orders);
    		}
    		else if(response == 4)
    		{
    			records[elementsize+1].cust_id = get_int("enter the new records customer id\n");
    			printf("Enter the customers name\n");
    			gets(records[elementsize+1].cust_name);
    			printf("Enter the customers State\n");
    			gets(records[elementsize+1].state);
    			printf("Enter the customers distribution code\n");
    			gets(buf);
    			records[elementsize+1].dis_code = buf[0];
    			records[elementsize+1].balance = get_double("Enter the customers balance\n");
    			records[elementsize+1].outstanding_orders = get_int("Enter the customers outstanding orders\n");
    			elementsize++;
    			fprintf(fp,"%d\n%s\n%s\n%c\n%f\n%d",records[elementsize].cust_id,
    				records[elementsize].cust_name,
    				records[elementsize].state,
    				records[elementsize].dis_code,
    				records[elementsize].balance,
    				records[elementsize].outstanding_orders);
    		}
    		else;
    		}
    		fclose(fp);
    return 0;
    }
    
    void printfile(data records[],int elementsize)
    {
    	for(int x = 0;x < elementsize;x++)
    		printf("%d\n%s\n%s\n%c\n%f\n%d\n",records[x].cust_id,
    					records[x].cust_name,
    					records[x].state,
    					records[x].dis_code,
    					records[x].balance,
    					records[x].outstanding_orders);
    }	
    	
    int binary(data records[], int elementsize, char cust_id_search)
    {
    	int first = 0;
    	int last = elementsize - 1;
    	int found = 0;
    	int mid;
    	while(first <= last && found == 0)
    	{
    		mid = (first + last)/2;
    		if(records[mid].cust_id == cust_id_search)
    		{
    			found = 1;
    		}
    		else if(records[mid].cust_id < cust_id_search)
    		{
    			first = mid + 1;
    		}
    		else 
    			last = mid - 1;
    	}
    	if(found == 0)
    		mid = -1;
    	else;
    	return mid;
    }
    
    
    //gets an integer from the keyboard
    int get_int(char msg[])
    {
    	char buf[40];
        printf(msg);
        gets(buf);
    	return atoi(buf);
    }
    
    //gets a double from the keyboard
    double get_double(char msg[])
    {
        char buf[40];
        printf(msg);
        gets(buf);
        return atof(buf);
    }
    c:\skool\program 3\proj 3\assignment3.cpp(43): error C2440: '=' : cannot convert from 'bool' to 'FILE *'
    hooch

  2. #2
    Registered User
    Join Date
    Mar 2005
    Posts
    135
    First, you need to fix this: "assignment3.cpp". You're compiling in C++, not C. When you create your source file, make sure you make the file extension .c, and not .cpp.


    xeddiex.

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    255
    well i cant were using visual studio C++.net and it has .cpp not .c and although i dont know why that would be the issue. i already said my format isnt pure C it has some technicalities like that.

    so other than THAT any suggestions?
    hooch

  4. #4
    Registered User
    Join Date
    Mar 2005
    Posts
    135
    It looks like you're using pure C to me. Your IDE is compatible with both C and C++. If you cannot, or are not allowed, to compile in C. Then that's too bad because the file extension you choose can make all the difference in fixing your code. C != C++.

    Sorry,
    - xeddiex.

  5. #5
    Registered User
    Join Date
    Nov 2001
    Posts
    255
    well ill try and run it in dev compiler cause i dont know how to change that file extension on .net

    although at school we have the .net compiler so if anyone can fix it using that then that would be more useful
    hooch

  6. #6
    Registered User
    Join Date
    Nov 2001
    Posts
    255
    ok well it doesnt even compile right on dev so i dont know?
    hooch

  7. #7
    Registered User
    Join Date
    Mar 2005
    Posts
    135
    Quote Originally Posted by ssjnamek
    ok well it doesnt even compile right on dev so i dont know?
    You have the option to choose the file extension you want right after creating a project but before creating a source file; when creating a source file, the popup-window let's you specify a name for your new source file, at this point, it is when you can change the extension. And by the way, If you sucessfully create a file in .c, you'll most-likely get compiler errors at compile time because what C++ considers good, C considers not good, and vise-versa.

    - xeddiex

  8. #8
    Registered User
    Join Date
    Nov 2001
    Posts
    255
    ah well that is interesting and ill be sure to try that out sometime but is this fixable with a .cpp file? since thats kind of what were using and teacher is the one writing grades.

    as is most students are using even less of C than i am as they have a C++.net project created with all those extra files i just chose to make an empty project instead so i know changing the extension from .cpp to .c is going to cause problems.
    hooch

  9. #9
    Registered User
    Join Date
    Mar 2005
    Posts
    135
    Quote Originally Posted by ssjnamek
    ah well that is interesting and ill be sure to try that out sometime but is this fixable with a .cpp file? since thats kind of what were using and teacher is the one writing grades.

    as is most students are using even less of C than i am as they have a C++.net project created with all those extra files i just chose to make an empty project instead so i know changing the extension from .cpp to .c is going to cause problems.
    Yes, it probably is fixable as your file extension stands nows, but, you're not doing the right thing. You're not conforming to the standard, and C++ will let you get away with things that C won't.

    I just compiled your code in .c and there is a ton of errors. And I'm sorry to say that I cannot, and don't want to teach you something that's not right. Maybe someone else well be more lenient then I. Sorry.

    I got to go to sleep anyways, so nighty-night
    - xeddiex

  10. #10
    Registered User
    Join Date
    Nov 2001
    Posts
    255
    well what are the differences i always hear it but when im writing code that LOOKS an awful lot like regular C and its crashes its not real useful to just say its different.

    and i said 100000x times its probably not really pure standard C but i have to deal with it. oh well guess ill find out tommorrow guess this will be late again due to something stupid.

    i do agree he should use a .c file but weve only recently been switching to .net so i really dont think anyone there fully knows it 100% and coupled with the fact this class is taught MAYBE once a year so he probably uses it even less for all i know this could be the first time its been taught using .net.
    hooch

  11. #11
    Registered User
    Join Date
    Feb 2006
    Posts
    155
    if(fp = fopen(filename,"r+")== NULL)//
    instead use,
    if((fp=fopen(filename,"r+"))==NULL)

    this works just fine,i dont find the compiler complaining,and its not even crashing.

  12. #12
    Registered User
    Join Date
    Nov 2001
    Posts
    255
    hmm so i was missing a set of parathensis

    ok it compiles fine and now when i try and load a file it crashes the whole big do you want to send this windows click dont send blah blah blah screen.
    hooch

  13. #13
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    1. Don't use gets(), it's dangerous. See the FAQ.
    2. Don't use gets(), it's dangerous. See the FAQ.
    3. Don't use gets(), it's dangerous. See the FAQ.
    4. Hint: fclose(NULL) crashes.
    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.

  14. #14
    Registered User
    Join Date
    Nov 2001
    Posts
    255
    for school i have to use gets()

    and wait what does fclose(NULL) crash? and i used fclose(fp)?
    hooch

  15. #15
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I meant when fp is NULL, fclose will segfault.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. 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
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM