Thread: file size problem

  1. #1
    ... arjunajay's Avatar
    Join Date
    May 2005
    Posts
    203

    Talking file size problem

    I created a program that simply allocates some memmory and writes the contents to a file.(i was trying to build the fractal contest for fun ). But some deviuos error has occured.
    My code(below) always show '400 bytes written' but the file size is seen as 401, 402, 403... (ie, each time I run the program the file size increases by 1byte even though i deleted the file twice). This has previously happened when i binary read/write before.


    Can any one please point out what exactly I am missing here?
    P.S.this doesn't happen some times(But, that's very rare.).
    Code:
    class raw_image{
    private:
    unsigned char * matrix;
    long int width, height;
    public:
    raw_image(){
    	matrix = 0;
        width = height = 0;
    	}
    ~raw_image(){
    	if(matrix)delete[] matrix;
    	}
    bool set(long int w, long int h){
        if(matrix)return false;
    	width=w;
    	height = h;
    	if( (width>200) || (height>200) )return false;
    	if(! (matrix = new unsigned char [width*height]) )return false;
    	return true;
    	}
    void send_to_file(fstream & file)const;	//the problen seems to be here
    void friend display(raw_image img);	//just a waste fucntion to check...
    };
    
    //I used a loop other than a single 'write'
    because the max possible size 200x200 won't fit in an 'int'.
    so here might be the problem.
    void raw_image::send_to_file(fstream & file)const{
    	long int size=0;
    	for(int i=0; i<width; i++){
    		file.write(matrix+(i*width), height);
    		size+=height;
    		}
    	cout<<"\n\n"<<size<<"bytes written...\n";
    	}
    //waste function to check some things...
    void display(raw_image img){
    for(int i=0; i<img.height; i++){
    	for(int j=0; j<img.width; j++){
    		if(img.matrix[i*img.height+j])cout<<" 1";
    		else cout<<" 0";
    		}
    	cout<<endl;
    	}
    }
    int main(){
    raw_image blah;
    if(blah.set(20, 20))cout<<"Successful...\n\n";
    display(blah);
    fstream mfile("image.raw", ios::out);
    if(!mfile)cout<<"\n\nError...\n\n";
    else{
    	cout<<"Press any key to write...\n";
    	getch();
    	blah.send_to_file(mfile);
    	}
    return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    I hope that indentation style isn't representative of all your code.

    > because the max possible size 200x200 won't fit in an 'int'.
    It will if you're using a modern OS/Compiler combination.
    If you're still worried, use an unsigned.

    > fstream mfile("image.raw", ios::out);
    Open the file in binary mode, then you'll get what you expect.
    As it is, any bytes which equal '\n' get transformed into '\r\n' pairs, which stretch your file.
    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
    ... arjunajay's Avatar
    Join Date
    May 2005
    Posts
    203
    How silly of me...
    I never noticed that...
    >>indentation...
    This happens when I copy the code into a txt file for adding comments for posting.
    I set the IDE to use '4' spaces for tabs and not the actual and stuf like that.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I set the IDE to use '4' spaces for tabs
    Funny, that usually fixes the problem. What IDE do you use?
    My best code is written with the delete key.

  5. #5
    ... arjunajay's Avatar
    Join Date
    May 2005
    Posts
    203
    No, it's quite all right when I paste it to notepad.
    But I cut off the unwanted portions and modifyt he existing ones for posting then It becomes all noodly...
    >>ide
    Code blocks...

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Does it have an option to "show visible whitespace" ?
    If you're editing older code (older than your last change to indentation rules), then tabs could still exist in your code.

    Or try a search for them.
    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.

  7. #7
    ... arjunajay's Avatar
    Join Date
    May 2005
    Posts
    203
    This is my 'usual' style.
    Ok I admit I'm not that great on Indentation.
    But it doesn't really matter as I'm absent minded and loose track of my program if it spans over more than 1page( Talk about short memmory! ).
    please don't start on the importance of of prety typing...
    I've been hearing that for a really long time... <:< | >:<
    Code:
    #include<iostream.h>
    #include<conio.h>
    #include"../codebl~1/_complex.cpp"
    #include"../codebl~1/rawimage.cpp"
    
    int main(){
    clrscr();
    Complex z, c;
    
    raw_image fractal;
    fractal.set(200, 200);
    unsigned char * canvas = fractal.get_array();
    for(double i=0; i<200; i++){
    	for(double j=0; j<200; j++){
    		z.set(0,0);
    		c.set( (i-100)/200, (i-100)/200 );
    
    		for(int k=0; k<64; k++){
    			z= (z*z) + c;
    			if(z.modulus()>4){ cout<<"broken"; break; }
    		}
    		canvas[j*100+i] = k*4;
    	}
    }
    
    fstream file("image.raw", ios::out|ios::binary);
    if(!file)cout<<"\n\nError opening file...\n\n";
    else{
    	fractal.send_to_file(file);
    	file.close();
    	cout<<"\nSent to file...\n";
    }
    getch();
    return 0;
    }

  8. #8
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Just something to consider: Good indentation will likely help you keep track of longer, more complicated programs.

    Cheers
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  9. #9
    ... arjunajay's Avatar
    Join Date
    May 2005
    Posts
    203

    Angry heap prob...

    Ok now now I've always heard that you've got ton,n,tons of space in the heap, but I get an out of memmory when I try to dynamically allcoate:
    Code:
    long int x=200;
    unsigned char* canvas = new unsigned char[x*x];
    This is really getting wierd...
    Now I can't draw in a canvas size larger than 200*200pixels;
    All my fractals have to be small ity bity 100x100 or 200x200 sizes...
    Last edited by arjunajay; 08-01-2005 at 09:26 AM. Reason: I like editing my posts...

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Perhaps because your compiler belongs in a museum, and despite having GB of RAM in your machine, you can still only access 64K of it at once, and an absolute maximum of 640K.

    Oh, and if you don't start making an effort to indent with any degree of consistency, people will start ignoring you.
    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.

  11. #11
    ... arjunajay's Avatar
    Join Date
    May 2005
    Posts
    203

    Talking

    I think my indentation has consistency. because,
    There is a new tab after a block has started.
    Except in the case of main(); and classes.
    Code:
    class ABC{
    private:  /no indentation.
    Blah obj1;
    public:
    ABC(){
        Indenting();  indenting for other functions and blocks;
    }
    };
    
    int main(){
    No_indenting_for(main);
    for(int bb=0; bb<8000; bb++)
       indenting_for_loops_and_other_blocks();
    return 0;
    }
    Yeah I know its' wierd...
    I'll try changing when I post.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Subtle(?) File I/O Problem
    By cecomp64 in forum C Programming
    Replies: 9
    Last Post: 07-16-2008, 11:39 AM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  3. Invalid conversion from 'void*' to 'BYTE' help
    By bikr692002 in forum C++ Programming
    Replies: 9
    Last Post: 02-22-2006, 11:27 AM
  4. File I/O problem
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 12
    Last Post: 09-03-2005, 12:14 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM