Thread: file opening?

  1. #16
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    for school i have to use gets()
    Really? Get a new school. Ask if you can use fgets().
    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.

  2. #17
    Registered User
    Join Date
    Nov 2001
    Posts
    255
    so i have to change the value of fp before i do fclose(fp)? if so what value does it need? and ya why would fp equaling NULL cause fclose() to crash?
    hooch

  3. #18
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Look, just put the fclose(fp) right here:
    Code:
    			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);
    				}
    
    fclose(fp);
    			}
    And don't use while(!feof()); see the FAQ
    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.

  4. #19
    Registered User
    Join Date
    Nov 2001
    Posts
    255
    i put the fclose(fp) right there and it still crashes why does C file handling have to be so much more confusing than C++ argh....

    and well one other reason i beleive the school uses gets is cause they dont really concern themselves with tricks that are better for certain langauges rather they are trying to use a more generic method for most langauges though i think he is going to start showing us fgets() as we get more into C file handling so im sure ill sure it sooner or later or when i look at these faqs a bit more and worry about more effecient C programming.
    hooch

  5. #20
    Registered User
    Join Date
    Mar 2005
    Posts
    140
    How about some address operators and using the proper format specifier for double

    Code:
    fscanf(fp, "%d %s %s %c %lf %d\n",&records[elementsize].cust_id,
                                       records[elementsize].cust_name,
                                       records[elementsize].state,
                                      &records[elementsize].dis_code,
                                      &records[elementsize].balance,
                                      &records[elementsize].outstanding_orders)
    Make sure your file only has upto 12 entries
    Code:
    records[12]
    Last edited by spydoor; 02-27-2006 at 09:34 PM.

  6. #21
    Registered User
    Join Date
    Nov 2001
    Posts
    255
    still doesnt seem to work for some reason damnit guess ill just ask teacher later cause ive been sick so i didnt goto school today oh well i gotta go out tommorrow for stuff anyway might see if hes there. stupid file opening argh.

    theres actually only 8 records but was told make an array a little bigger so it can hold 1 or 2 more records
    hooch

  7. #22
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Post your updated code so we can see the fscanf calls. You keep forgetting your ampersands (&).
    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.

  8. #23
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Man you guys and your ugly switch menus. You mean to tell me schools can't teach a better way to do this?

    Schools seem to take all the necessary tools for the job and put a lock on them, thus making you use some arcane method and use 150 lines when if you just used the right tool, you could do it AND more in about 30 lines.

    I just don't understand it.

  9. #24
    Registered User
    Join Date
    Nov 2001
    Posts
    255
    stupid & that was the problem lol. i suppose one of you could tell me why i need that and this here book failed to mention that?

    and yes i keep hearing that schools have a hard time preparing you of course i also hear real life programs our millions of lines and done in teams something schools cant effectively do so who knows why schools do what they do. but ya programming does have this insane method of being hard to learn cause no one teaches the basics very well and coming from a guy who has self taught himself mostly i can tell you its next to impossible to get a proper tutorial or anything and even in HS when they finnally had programming the teacher's idea of teaching was "is this correct hairy"....the student in the class that actually understood it all
    hooch

  10. #25
    ex-DECcie
    Join Date
    Dec 2005
    Posts
    125
    Quote Originally Posted by ssjnamek
    stupid & that was the problem lol. i suppose one of you could tell me why i need that and this here book failed to mention that?

    and yes i keep hearing that schools have a hard time preparing you of course i also hear real life programs our millions of lines and done in teams something schools cant effectively do so who knows why schools do what they do. but ya programming does have this insane method of being hard to learn cause no one teaches the basics very well and coming from a guy who has self taught himself mostly i can tell you its next to impossible to get a proper tutorial or anything and even in HS when they finnally had programming the teacher's idea of teaching was "is this correct hairy"....the student in the class that actually understood it all

    Not all real world programs are millions of lines of code done in teams.

    I could be mean-spirited and say that schools don't prepare you well because the folks who are teaching you are teachers and not really programmers, but that would not be a fair statement necessarily.

    As an aside, my stepdaughter (who's a semester away from a BS in Ed, going to be a math teacher) had to do a CS intro to programming class a few semesters back. The professor used Java -- okay, I know a bit of Java and could help. The only problem was that the prof spent all kinds of time talking about objects, inheritance, and stuff like that, before ever mentioning control structures or anything that pertains to programming.

    I was fortunate -- I learned assembler first (on my own) and ended up programming in C with a really good mentor.

    Read other people's code, if you can get your hands on it. Don't be afraid to take a program you have written (that works) and look at it and say "How can I make this better?" "Is there a better technique than what I have used?"

    Get a copy of K&R. Okay, I"m old fashioned, but that book, to me, is the starting point.

    Frequent this forum and others -- read the topics that are here, and look at the code that people post and see if you can find what is wrong. (Hell, I've been programming in C for 20 years now and I still learn stuff in here)

    That will make you a better programmer..... that and lots of hard work
    Mr. Blonde: You ever listen to K-Billy's "Super Sounds of the Seventies" weekend? It's my personal favorite.

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