Thread: files > 65535 bytes wont read in dos

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    5

    files > 65535 bytes wont read in dos

    Hello, I hope you can welcome one more newbie into the ranks!

    For practice I've been trying to make a program (16bit dos) that reads each byte in a file. I've set up an array of 256 elements...array[256] and each time that byte comes up I add one to the corresponding element in the array. Like I said its just for practice.

    My real problem comes when I try to read files that are larger than 65535 bytes. The program (stops reading???) or something once it gets past this point.

    Is this a limitation of 16bit mode (would make sense) and was wondering if there is something I'm missing. Here's the functions I use.

    FILE *filein

    fopen("c:/something.txt","rt");

    input=fgetc(filein);

    fclose(filein);

    Thats the file functions I use and the rest is just simple stuff.

    Thanks for the help!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    You'd better post the whole program.

    You certainly can't read a large file in one single call, but there should be no problem reading the file one character at a time.
    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.

  3. #3
    Registered User
    Join Date
    Nov 2003
    Posts
    5
    Code:
    #include <stdio.h>
    
    void main()
    {
    
    	FILE *filein;
    	int array[256]={0};
    	int input=0;
    	bool done=false;
    
    	filein=fopen("c:/something.zip","r");
    
    	while (!done)
    	{
    		input=fgetc(filein);
    		
    		if (input!=EOF)
    		{
    			array[input]++;
    		}
    		else
    		{
    			done=true;
    		}
    
    	}
    
    	fclose(filein);
    
    	//program goes on to diplay how many hits on each number
    
    	return;
    	
    }
    Works good for the first 65KB...

    I'm using Win98 and MVCS 6.0
    Last edited by Pharoh; 11-11-2003 at 01:54 AM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > void main()
    main returns an int - see the FAQ
    Successful programs end with
    return 0;

    > bool done=false;
    bool is a C++ type

    Rename your prog.cpp to be prog.c, to make sure you're using 'C' language and not C++ (which is the MSVC default)

    > filein=fopen("c:/something.zip","r");
    Use "rb" for binary files - your original post suggested you were reading a text file
    You should also add
    Code:
    if ( filein == NULL ) {
        perror( "Unable to open file" );
        return 1;
    }
    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
    Nov 2003
    Posts
    5
    Thank you Salem.

    using "rb" was the trick in the fopen funtion.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 11-23-2007, 01:48 PM
  2. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM
  3. how to read 4 consecutive bytes as an integer
    By *DEAD* in forum C Programming
    Replies: 8
    Last Post: 01-15-2007, 04:03 PM
  4. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 10:36 AM
  5. reading old dos binary files
    By ronin in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 11-10-2001, 12:43 PM