Like Tree9Likes

Null pointer asignement in turbo C on DOSBOX

This is a discussion on Null pointer asignement in turbo C on DOSBOX within the C Programming forums, part of the General Programming Boards category; The code is as follows. Code: struct encrypt { unsigned int signature; unsigned long hash; unsigned start_offset; unsigned long long ...

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    22

    Null pointer asignement in turbo C on DOSBOX

    The code is as follows.
    Code:
    struct encrypt
    {
    	unsigned int signature;
    	unsigned long hash;
    	unsigned start_offset;
    	unsigned long long length;
    } encfile;
    
    
    void main()
    {
    	unsigned long crchash;
      //	unsigned long long adlerhash;
    
    	char filename[25];
    	char filename2[25] ="enc";
    	char password[20];
    	unsigned long long len;
    	unsigned int *sig;
    	FILE *file1, *file2;
    	char *buffer;
    	char *buffer2;	//buffer to hold the file data.
    	clrscr();
    	fflush(stdin);
    
    	printf("\n\n\t\t\t\tFILE ENCRYPTER V 0.1B");
    	printf("\\n\nEnter Filename: ");
    	gets(filename);
    	if(file1 = fopen(filename,"rb") == NULL)
    	{
    		printf("\n\n\n\n\t\t\tError: Cannot open File. ");
    		exit(-1);
    	}
    	fseek(file1, 0, SEEK_END);
    	len = ftell(file1);
    	fseek(file1, 0, SEEK_SET);
    
    	if((buffer = (char *) malloc(len+1)) == NULL)
    	{
    		clrscr();
    		printf("Cannot allocate memory...in first malloc");
    		fclose(file1);
    		exit(-2);
    	}
    
    	fread(buffer, len, 1, file1);
    
    	memcpy(sig, buffer, 2);
    	if(*sig != 0x0E1C)
    	{
    
    
    		clrscr();
    		printf("Enter password to encrypt the file: ");
    		gets(password);
    		crchash = crc32(password, strlen(password));
    		encfile.signature = 0x0E1C;
    		encfile.hash = crchash;
    		encfile.start_offset = 0x06;
    		encfile.length = len;
    
    		if((buffer2 = (char *) malloc(sizeof(encfile)+len+1)) == NULL)
    		{
    			clrscr();
    			printf("Cannot allocate memory...in second malloc");
    			fclose(file1);
    			exit(-2);
    		}
    
    		memcpy(buffer2, &encfile, sizeof(encfile));
    		memcpy(buffer2, buffer, len);
    
    		strcat(filename2, filename);
    
    		if(fopen(filename2, "wb") == NULL)
    		{
    			printf("\n\n\n\t\tCannot create file...");
    			fclose(file1);
    			exit(-1);
    		}
    
    		fwrite(buffer, sizeof(encfile)+len+1, 1, file2);
    
    		printf("\n\nFile encrypted....");
    		getch();
    	}
    
    
    
    }
    Pls take note that i have only done the encrypting conditon . but the error is on the fairst malloc call to allocate memeory to the buffer..
    Can you please guide me what am i doing wrong.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    2,701
    Can you please guide me what am i doing wrong.
    Start with
    void main()
    gets()
    fflush(stdin)
    but the error is on the fairst malloc call
    What first malloc call? I don't see malloc anywhere.

    But probably the biggest mistake is:
    turbo C
    Jim

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    22
    @jim, i cant use any other compile bcoz its for my college project and i cant use other compilers that 16 bit turbo c compiler. I think malloc shouldnt fail on small files but is failing so to my knowledge i must be doing some wrong pointer operations there, but cant figure out myself.. Can someone elaborate it..

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,488
    It's time to start naming and shaming schools which still push this ancient TurboCrap.

    FFS, it's like trying to teach modern English, when all your reference material was written by Shakespeare.

    The mere fact that you need to run it in an emulator like DosBox should tell you all you need to know about how obsolete it is.

    > if(file1 = fopen(filename,"rb") == NULL)
    namely that == has higher precedence than =

    if ( a = b == NULL )
    is
    if ( a = (b == NULL) )
    which results in a being either true(1) or false(0)
    stahta01 likes this.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    2,273
    Have you tried printing the value of "len" to the screen (after line 35) to verify it is a valid value?

    You might need another way to find the size of the file:

    Setting the file position indicator to end-of-file, as with fseek(file, 0, SEEK_END), has
    undefined behavior for a binary stream (because of possible trailing null characters) or for any stream
    with state-dependent encoding that does not assuredly end in the initial shift state.

    7.19.3

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    2,543
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  7. #7
    Registered User
    Join Date
    Mar 2010
    Posts
    22
    Yes like matticus said, i tried printing len and it returned to be -1. so that means i am not getting the file size correctly.
    So how can i get file size correctly... and pls take note that i am doing this on a binary file not a plain text file. if it would had been a plain text i could have counted till EOF.

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    2,701
    if it would had been a plain text i could have counted till EOF.
    Just because the file is opened in binary mode there is no reason you can't count each character until you reach the end of file.

    Jim

  9. #9
    Registered User
    Join Date
    Mar 2010
    Posts
    22
    @jim is there a EOF character on the end of binary file, no not possible, to my knowledge i dont see that there are any marks or signs of file ending in binary files. The ends are calculated as per the entry in FAT.

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    2,701
    EOF is not a character. Have you tried?

    Jim
    AndiPersti likes this.

  11. #11
    Registered User
    Join Date
    Mar 2010
    Posts
    22
    NO not yet!! its already 2:09 AM and my eyes are not helping me, i will come tomorrow after trying. time for some good sleep now. GOOD NIGHT.

  12. #12
    Registered User
    Join Date
    Jun 2011
    Posts
    2,273
    After you get some rest, here's our FAQ on EOF: FAQ > Definition of EOF and how to use it effectively - Cprogramming.com
    AndiPersti likes this.

  13. #13
    Registered User
    Join Date
    May 2010
    Posts
    2,701
    Also note I am not saying that counting characters in a binary file is the only way, or even the best way, to find the size of your file. But it will work.

    Jim

  14. #14
    Registered User
    Join Date
    Mar 2010
    Posts
    22
    Code:
    for(length = 0; fread(&temp, sizeof(char), 1, filehandle) != EOF; len++);
    So, i did this itertion till EOF and i think it should return length in length.
    but it not working..

  15. #15
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,488
    RETURN VALUE
    fread() and fwrite() return the number of items successfully read or written (i.e., not the number of characters). If an error occurs, or the end-of-file is reached,
    the return value is a short item count (or zero).

    fread() does not distinguish between end-of-file and error, and callers must use feof(3) and ferror(3) to determine which occurred.
    Do you see EOF mentioned here as a return result?
    AndiPersti likes this.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Does static initialization of pointer make it null pointer
    By Saurabh Mehta in forum C Programming
    Replies: 1
    Last Post: 11-22-2012, 11:05 PM
  2. Null Pointer
    By saswatdash83 in forum C Programming
    Replies: 3
    Last Post: 07-15-2008, 04:12 AM
  3. pointer ot NULL
    By anthonye in forum C++ Programming
    Replies: 1
    Last Post: 11-01-2003, 02:43 PM
  4. Null pointer
    By MethodMan in forum C Programming
    Replies: 2
    Last Post: 03-11-2002, 05:49 PM
  5. How I can Show the pointer of mouse int 800*600 256 color mode in Turbo C++?
    By hadizadeh in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 12-12-2001, 06:17 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21