Thread: Need help with files

  1. #1
    Registered User
    Join Date
    May 2008
    Location
    Malta
    Posts
    19

    Need help with files

    hi this is my first post so im having some difficulty in showing my problem

    im having problems from 2 functions.

    the program is meant to write a data file with a structure containign student information (such as nam id and marks), the program is meant to then assign the grade according to mark entered into the file.

    The problem is when i do this, the last record in the file is repeated throughout the whole file.

    The second problem lies when i try to write these records into a sequential file, it writes the headers but leaves the rest blank.

    please find attatched a txt file of the program.

    thanks for your help.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You must never, ever use gets.
    See http://cpwiki.sf.net/Gets
    and http://cpwiki.sf.net/Buffer_overrun
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    May 2008
    Location
    Malta
    Posts
    19
    Quote Originally Posted by Elysia View Post
    You must never, ever use gets.
    See http://cpwiki.sf.net/Gets
    and http://cpwiki.sf.net/Buffer_overrun

    im pretty sure there arent any overuns

    the problem are these files.

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Maybe not if you enter x characters, enter x+1 and you'll have a buffer overrun (That's a security issue, a serious one at that).
    Elysia has been kind enough to point it out, take in the advice.

  5. #5
    Registered User
    Join Date
    May 2008
    Location
    Malta
    Posts
    19
    Quote Originally Posted by zacs7 View Post
    Maybe not if you enter x characters, enter x+1 and you'll have a buffer overrun (That's a security issue, a serious one at that).
    Elysia has been kind enough to point it out, take in the advice.
    ah now i seee, thanks for the heads up.

    Its odd because the lecturer told us it was ok to use the gets function.

    whats really bugging me is that when i try and change a single field it writes the last record 42 times.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Salnitro View Post
    ah now i seee, thanks for the heads up.

    Its odd because the lecturer told us it was ok to use the gets function.

    whats really bugging me is that when i try and change a single field it writes the last record 42 times.
    Your lecturer is obviously not particularly concerned with writing code that is robust and safe.

    Code:
    		for(i=1;i<=42;i++)
    		{
    			
    			fseek(main_program,(i-1)*sizeof(Student),SEEK_SET);
    			fread(&slot,sizeof(Student),1,main_program);
    
    			if(strcmp(slot.id, "")>0)
    			{
    				fprintf( write_file, "%-8s%-9s%-15d%10d%8c\n", slot.name, slot.id, slot.mark1,slot.mark2, slot.grade);
    			}/*end if*/
    		}/*end while*/
    This will do 42 steps through the loop, no matter how much data is in the file, since you don't check if fread() actually did read something or not. If it fails to read, it won't update the content of slot, so it will repeat the same thing over and over. ]

    Checking the return value from functions is another thing that should be done to make sure the code is correct [and of course, coupled with appropriate action when error conditions are detected - just checking it and not doing something about it is pretty pointless].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Salnitro View Post
    Its odd because the lecturer told us it was ok to use the gets function.
    Some teachers are like that, unfortunately. If someone here tells you that something is bad or wrong, you should probably listen.
    We see a lot of bad practice / use of insecure functions here everyday, from all kinds of teachers.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    May 2008
    Location
    Malta
    Posts
    19
    wow guys, this is somewhat disturbing for me,

    i mean im in my first year of electrical engineering and its been 3 years since ive touched programming (basic pascal).

    we are using the book "C how to program" and this lecturer is real bad, are there any tutorials, good sources on how to learn functions of the string library and structures (not dynamic)?

    thanks for all your input, wish i found this site 6 months ago.

    Matsp: how would i go about to to fix the problem then?

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Unfortunately, there appear to be more than one bad teacher out there...

    There are book recommendations in one of the forums, which can guide you to a book that will help. Books are better than Web-tutorials, because (in most cases) books cover a fair bit deeper than the web. The web is great for looking things up, but not necessarily a great learning tool for overall knowledge.

    There are tutorial pages on the main page that this forum belongs to, as well as other places. http://www.cprogramming.com/tutorial or some such (try without the tutorial bit if the link given doesn't work)

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Ressources files
    By mikahell in forum Windows Programming
    Replies: 4
    Last Post: 06-19-2006, 06:50 AM
  2. add source files to embedded VC 4.0
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 06-13-2006, 03:28 AM
  3. *.cpp and *.h files understanding
    By ElastoManiac in forum C++ Programming
    Replies: 4
    Last Post: 06-11-2006, 04:45 AM
  4. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:46 AM
  5. Multiple Cpp Files
    By w4ck0z in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2005, 02:41 PM