fread/fwrite

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • chopficaro
    New Member
    • May 2008
    • 6

    #1

    fread/fwrite

    PLEASE DO NOT BE INTIMIDATED BY THE SIZE OF MY CODE, I BELIEVE THE PROBLEM IS VERY SIMPLE AND IT IS MARKED WITH /*ERROR*/. when i run the exe, i can add 2 patients easily to the file. then i try to display them. it displays the first one just fine, but the second it tries to display the first value in the [1]th element of the array, i get an error message. that part of my code is marked with a /*ERROR*/ just before it. easy to find plz help this is due monday. if u can solve my problem then just give me a jpeg and will erect a 500 meter idol of worship in your image.

    file attached has code
    Attached Files
  • chopficaro
    New Member
    • May 2008
    • 6

    #2
    y cant i see my own post?

    Comment

    • Niheel
      Recognized Expert Moderator Top Contributor
      • Jul 2005
      • 2433

      #3
      fixed, your code caused a bug to show up. fixed it by uploading as attachment.
      niheel @ bytes

      Comment

      • chopficaro
        New Member
        • May 2008
        • 6

        #4
        ok there it is... dont know wny it was invisible b4...
        can u help me out? i would be eternally greatful

        Comment

        • chopficaro
          New Member
          • May 2008
          • 6

          #5
          lol i started the post before u posted lol

          Comment

          • oler1s
            Recognized Expert Contributor
            • Aug 2007
            • 671

            #6
            Learn to be able to identify the problematic portions in your code, and attempt to fix them. So your program has a bug. It's called programming. Now try and fix it.

            If you know the problematic portion of your code, post only that portion. Indicate detailed observations. What do you expect your code to do? When testing that portion of the code, what input did you give. What output did you get. What is the error message you get? What did you find with a debugger? That sort of thing.

            Comment

            • chopficaro
              New Member
              • May 2008
              • 6

              #7
              yo someone just gave me a hint but they were a little unspecific, he said "it probably didnt clean the buffer between READING the 1st file and inputing the SECOND file"
              so how do i do that? FFLUSH(STDIN);? system("cls");?

              Comment

              • oler1s
                Recognized Expert Contributor
                • Aug 2007
                • 671

                #8
                I just took a look at your code, and it's a downright mess. It's questionable how you came up with this code. Interesting things to note: your use of #include "iostream" and "stdio.h" and "string.h" and "process.h" (?). Clearly, you aren't even familiar with how to do the most basic, which is even include standard headers. How did you get your code to compile? Why do you rely on process.h?

                Why are you using the C I/O if you have access to C++ I/O?
                Why are you not using C++ strings?

                Your code is buggy in several areas. You should not use fflush(stdin). fflush is only defined to work on output streams, not input. If fflush(stdin) works for you, it's because your compiler supports a non-standard extension. Guess what, it might not work on your teacher's computer.

                You can't tell whether you hit an EOF, until you actually try to read. After you attempt a read, it either works, or it fails partially or completely. You have somewhere in your code while (!feof). But wait a minute, there is no way to know if your next read will fail on EOF. So guess what, you need to read the documentation for feof. It does not do what you think it does.

                You use several non standard functions, which you shouldn't do as a beginner. What's funny is your use of gets_s. Use fgets, not gets. gets is marked off in all documentation as do not use.

                Your code is a horrible horrible, poorly formatted, mess. The questions you ask and the way your code is written suggest you don't really know what you're doing, and are trying to slap together code that can fly by your teacher. Maybe I'm horribly wrong, but the fact that you have more fundamental issues with your code, like, it's C code with C++ headers, and so on, suggest, a number of things.

                If you really want help, start by rewriting decent code. EDIT: Notably, you may want to (a) decide whether you want to use C or C++ and (b) read a book or tutorial on how to write in said language. Hint: Getting your headers in order might be a good start.

                Comment

                • chopficaro
                  New Member
                  • May 2008
                  • 6

                  #9
                  its also interesting that "numread" returns a zero on the first patient and a 1 on the 2nd patient

                  Comment

                  • oler1s
                    Recognized Expert Contributor
                    • Aug 2007
                    • 671

                    #10
                    its also interesting that "numread" returns a zero on the first patient and a 1 on the 2nd patient
                    numread has the return value for the fread function. What does this return value indicate?

                    Comment

                    Working...