Thread: Huge string manipulation problem.

  1. #1
    Registered User jadaces's Avatar
    Join Date
    Jun 2010
    Posts
    18

    Huge string manipulation problem.

    OK, I have a big problem, I don't know why this is happening to me. I'm trying to output a huge hex string to a FILE, but its not working for some reason it just gives me this in output.txt == ( MZ ) ??????

    Code:
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    
    int main(int argc, char *argv[])
    {
    
    
    
    unsigned char rawData[401408] = {
    	0x4D, 0x5A, 0x90, 0x00, 0x03, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
    	0xFF, 0xFF, 0x00, 0x00, 0xB8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    	0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    	0xF8, 0x00, 0x00, 0x00, 0x0E, 0x1F, 0xBA, 0x0E, 0x00, 0xB4, 0x09, 0xCD,
    	0x21, 0xB8, 0x01, 0x4C, 0xCD, 0x21, 0x54, 0x68, 0x69, 0x73, 0x20, 0x70,
    	0x72, 0x6F, 0x67, 0x72, 0x61, 0x6D, 0x20, 0x63, 0x61, 0x6E, 0x6E, 0x6F,
    	0x74, 0x20, 0x62, 0x65, 0x20, 0x72, 0x75, 0x6E, 0x20, 0x69, 0x6E, 0x20,
    	0x44, 0x4F, 0x53, 0x20, 0x6D, 0x6F, 0x64, 0x65, 0x2E, 0x0D, 0x0D, 0x0A,
    	0x24, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF9, 0xD2, 0x19, 0xBE,
    	0xBD, 0xB3, 0x77, 0xED, 0xBD, 0xB3, 0x77, 0xED, 0xBD, 0xB3, 0x77, 0xED,
    	0xAE, 0xBB, 0x1E, 0xED, 0xB0, 0xB3, 0x77, 0xED, 0xB8, 0xBF, 0x17, 0xED,
    	0xBF, 0xB3, 0x77   /* it is to huge to put all the HEX  on this post,  its [401408] elements long   };    */
    
    
    
      	FILE* Binary_1 = fopen("output.txt","w");
    
      	fprintf(Binary_1,"%s",string1);
    
      	fclose(Binary_1);
    
    
    return(0);
    }

    output.txt = ( MZ )










    But when i try a smaller peace of string of Hex, it outputs correctly.

    Code:
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    
    int main(int argc, char *argv[])
    {
    
    char string1[5] = { 0x68, 0x65, 0x6C, 0x6C, 0x6F };
    
      	FILE* Binary_1 = fopen("output.txt","w");
    
      	fprintf(Binary_1,"%s",string1);
    
      	fclose(Binary_1);
    
    
    return(0);
    }

    output.txt = ( hello )







    I don't know what could be the problem, could someone help me out. Does string creation have a SIZE limit or something.

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    That's because you probably cannot allocate that much memory on the stack. Use dynamic memory allocation instead via malloc().
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Yes, the stack especially has a limit in the number of array elements you may have. ( Don't ask me what's the limit, i don't know )
    Devoted my life to programming...

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    If you are going to handle your char's as a string - using %s for the print format, etc., you should add an end of string char, to the end of the string, as well: '\0'.

    But your first problem is you've gone way over the limit for the stack, and need to use dynamic memory, as mentioned above.

  5. #5
    Registered User
    Join Date
    Jul 2010
    Posts
    13
    Or try to use UNICODE representation instead of ANSI
    ANSI:
    char string1[5]
    UNICODE:
    char wstring1[5]
    when representing data using UNICODE add L before the string

  6. #6
    Registered User
    Join Date
    Jun 2010
    Posts
    182
    Quote Originally Posted by anne03 View Post
    Or try to use UNICODE representation instead of ANSI
    ANSI:
    char string1[5]
    UNICODE:
    char wstring1[5]
    when representing data using UNICODE add L before the string
    What does it change regarding the allocable stack memory?

  7. #7
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Have you tried to declare it as static?

  8. #8
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    You cannot use %s to print the entirety of rawData (is rawData supposed to be the same as string1 in your 1st example?) to a file because it contains 0x00 (the 4th byte for example). The %s format specifier tells fprintf to treat the incoming data as a string and thus to stop at the first null (0x00) character. Even if what you had worked disregarding stack issues, you'd only ever get the first three bytes of that long string into the file. You'd need to use fwrite instead of the fprintf/%s combo.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  9. #9
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Isn't a binary file starting with MZ an executable? What exactly are you doing here? I sort of smell something bad.

  10. #10
    Registered User
    Join Date
    Mar 2009
    Posts
    48
    Isn't a binary file starting with MZ an executable? What exactly are you doing here? I sort of smell something bad.
    Actually, since OP used %s , the hex unsigned chars were interpreted as normal chars and written to the output stream where
    0x4D = 'M',
    0x5A = 'Z',
    0x90 = it did print if you look closely, but actually may not be a printable character. ( so it seems to be a space )

    The example program, which jadaces says prints it correctly may not have produced the desired result, since you wrote to file, you possibly missed 'something'.Try your own program using stdout.
    Code:
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    
    int main(int argc, char *argv[])
    {
    
    char string1[5] = { 0x68, 0x65, 0x6C, 0x6C, 0x6F };
    
      	FILE* Binary_1 = stdout;
    
      	fprintf(Binary_1,"%s",string1);
    
      	
    
    
    return(0);
    }
    Finally, could you post an example of how the output file should be without printing implementation specific chars.
    Last edited by zalezog; 07-14-2010 at 07:54 AM.

  11. #11
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Yeah, how interesting is the Google search for that file size?

  12. #12
    Registered User jadaces's Avatar
    Join Date
    Jun 2010
    Posts
    18
    Thanks for all the replies and yes it's a binary file ill try this with malloc() and see if it works

  13. #13
    Registered User jadaces's Avatar
    Join Date
    Jun 2010
    Posts
    18
    Quote Originally Posted by zalezog View Post
    Actually, since OP used %s , the hex unsigned chars were interpreted as normal chars and written to the output stream where
    0x4D = 'M',
    0x5A = 'Z',
    0x90 = it did print if you look closely, but actually may not be a printable character. ( so it seems to be a space )

    The example program, which jadaces says prints it correctly may not have produced the desired result, since you wrote to file, you possibly missed 'something'.Try your own program using stdout.
    Code:
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    
    int main(int argc, char *argv[])
    {
    
    char string1[5] = { 0x68, 0x65, 0x6C, 0x6C, 0x6F };
    
      	FILE* Binary_1 = stdout;
    
      	fprintf(Binary_1,"%s",string1);
    
      	
    
    
    return(0);
    }
    Finally, could you post an example of how the output file should be without printing implementation specific chars.
    It should output to a .exe file and able to execute like normal.

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    jadaces, how do you explain rags_to_riches' observation?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Registered User jadaces's Avatar
    Join Date
    Jun 2010
    Posts
    18
    Quote Originally Posted by laserlight View Post
    jadaces, how do you explain rags_to_riches' observation?
    Well am trying to incorporate wget.exe, it's exactly that about of bytes. Whats so wrong with trying to do that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  2. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  3. string manipulation problem
    By csj561 in forum C++ Programming
    Replies: 3
    Last Post: 03-18-2005, 11:11 PM
  4. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM