Replace an image in C using binary mode

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Savage
    Recognized Expert Top Contributor
    • Feb 2007
    • 1759

    #16
    Originally posted by DeMan
    use length in your write as well as your read:
    eg
    [code=cpp]
    fwrite(x, sizeof(x[0]), length, background);
    [/code]
    Yup,u heard TheMan

    ;)

    Savage

    Comment

    • niskin
      New Member
      • Apr 2007
      • 109

      #17
      Originally posted by DeMan
      use length in your write as well as your read:
      eg
      [code=cpp]
      fwrite(x, sizeof(x[0]), length, background);
      [/code]
      I have done this now and the image has still not been transferred. I opened the image in notepad to see if any more had been transferred than before. There are now 3 characters, before there was one.

      Comment

      • Savage
        Recognized Expert Top Contributor
        • Feb 2007
        • 1759

        #18
        Originally posted by niskin
        I have done this now and the image has still not been transferred. I opened the image in notepad to see if any more had been transferred than before. There are now 3 characters, before there was one.
        Maybe malloc doesn't allocate enough memory.

        Try allocating memory for different data type.

        Comment

        • niskin
          New Member
          • Apr 2007
          • 109

          #19
          Originally posted by Savage
          Maybe malloc doesn't allocate enough memory.

          Try allocating memory for different data type.
          Nope no change when allocating memory for char or float.

          Comment

          • DeMan
            Top Contributor
            • Nov 2006
            • 1799

            #20
            Have you tried printing out the intermediate variables.....
            Is the length you get from fseek correct?
            What is the size of x?
            Can you narrow down whether the read or write is causing the error?

            Comment

            • Savage
              Recognized Expert Top Contributor
              • Feb 2007
              • 1759

              #21
              Originally posted by DeMan
              Have you tried printing out the intermediate variables.....
              Is the length you get from fseek correct?
              What is the size of x?
              Can you narrow down whether the read or write is causing the error?
              The problem is in length.try printing it out.

              Savage

              Comment

              • Savage
                Recognized Expert Top Contributor
                • Feb 2007
                • 1759

                #22
                Originally posted by Savage
                The problem is in length.try printing it out.

                Savage
                Sorry for double post but I found the frustration maker(tm):

                it was fseek:

                right now ur fseek is called like:fseek(newp ic,SEEK_END,0) ,but it should be

                fseek(newpic,0, SEEK_END)

                Savage

                Comment

                • niskin
                  New Member
                  • Apr 2007
                  • 109

                  #23
                  Originally posted by Savage
                  Sorry for double post but I found the frustration maker(tm):

                  it was fseek:

                  right now ur fseek is called like:fseek(newp ic,SEEK_END,0) ,but it should be

                  fseek(newpic,0, SEEK_END)

                  Savage
                  Right well the content of the file is now: Ä 0 h"0 (when opened in notepad)

                  It is not displaying any image.

                  Printing length and x does not work because the compiler is telling me that I'm trying to do an invalid conversion from int to char.

                  Comment

                  • Savage
                    Recognized Expert Top Contributor
                    • Feb 2007
                    • 1759

                    #24
                    Originally posted by niskin
                    Right well the content of the file is now: Ä 0 h"0 (when opened in notepad)

                    It is not displaying any image.

                    Printing length and x does not work because the compiler is telling me that I'm trying to do an invalid conversion from int to char.
                    How did u tryed to print length?

                    It should be the same as the size of the file that u are coping

                    Savage

                    Comment

                    • niskin
                      New Member
                      • Apr 2007
                      • 109

                      #25
                      Originally posted by Savage
                      How did u tryed to print length?

                      It should be the same as the size of the file that u are coping

                      Savage
                      I just did printf( length ).

                      Comment

                      • Savage
                        Recognized Expert Top Contributor
                        • Feb 2007
                        • 1759

                        #26
                        Originally posted by niskin
                        I just did printf( length ).
                        Like:

                        printf("%d",len gth);

                        ??

                        Savage

                        Comment

                        • niskin
                          New Member
                          • Apr 2007
                          • 109

                          #27
                          Originally posted by Savage
                          Like:

                          printf("%d",len gth);

                          ??

                          Savage
                          Lol I can't believe I missed that, sorry for being stupid. It printed: 101054

                          I have also just opened stbedes.jpg in notepad again. It appears that it has 4 characters but it seems to have a lot of spaces, and I would guess there are 101050 spaces because that would make up the rest of length and there are so many spaces that it is quite believable. Why is it coming up with 4 characters and 101050 spaces though?

                          Comment

                          • DeMan
                            Top Contributor
                            • Nov 2006
                            • 1799

                            #28
                            Incidently (though I don't think this is the cause of your problem),
                            you are allocating WAY more memory than you need.....
                            you don't need to multiply (length* sizeof(int)) = the length is already the total number of bytes

                            Comment

                            • Savage
                              Recognized Expert Top Contributor
                              • Feb 2007
                              • 1759

                              #29
                              Originally posted by DeMan
                              Incidently (though I don't think this is the cause of your problem),
                              you are allocating WAY more memory than you need.....
                              you don't need to multiply (length* sizeof(int)) = the length is already the total number of bytes
                              Yes that's true.

                              However when u remove sizeof(int) final file will have size of 0 bytes,or atleast that's what happens to me.


                              Savage

                              Comment

                              • DeMan
                                Top Contributor
                                • Nov 2006
                                • 1799

                                #30
                                you could use sizeof(char)... .

                                Comment

                                Working...