Thread: Null pointer asignement in turbo C on DOSBOX

  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
    4,632
    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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    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)
    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.

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    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
    4,183
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  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
    4,632
    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
    4,632
    EOF is not a character. Have you tried?

    Jim

  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
    4,513
    After you get some rest, here's our FAQ on EOF: FAQ > Definition of EOF and how to use it effectively - Cprogramming.com

  13. #13
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    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?
    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.

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-23-2012, 12:05 AM
  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, 03:43 PM
  4. Null pointer
    By MethodMan in forum C Programming
    Replies: 2
    Last Post: 03-11-2002, 06: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, 07:17 AM