Thread: How many chars does it take to fill up 1 megabyte?

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    241

    How many chars does it take to fill up 1 megabyte?

    How many chars(i.e. A) does it take to fill up 1 megabyte?

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    It depends on your system, but on most platforms a char is 1 byte.

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Ye who looks he finds!
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Well one megabyte is 1,048,576 bytes and the 'char' type in C++ is at least 8 bits (or one byte) but I don't think the standard guarantees it is always one byte in size.

    Also, it depends on the character encoding. ASCII, for example, uses 7 bits for each character while for Unicode the size of each character can vary. (See this tutorial for more info)
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  5. #5
    Registered User
    Join Date
    Sep 2005
    Posts
    241
    I found another way to do what I am trying to do. I can just compare the size of the file to the original size of the file... just need to know how now.
    The comments are what I need to do.
    Code:
    See new post.
    Last edited by bikr692002; 04-05-2006 at 03:05 PM.

  6. #6
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Just a tip for you, a new programmer...
    Code:
    std::cout<<"To select Secure Disk Wipe, input SDW.\n";
    std::cout<<"To select Secure Disk Cleanse, input SDC.\n";
    ...these are not operations you want to be messing around with until you're very familiar to programming.
    Sent from my iPadŽ

  7. #7
    Registered User
    Join Date
    Sep 2005
    Posts
    241
    Quote Originally Posted by SlyMaelstrom
    Just a tip for you, a new programmer...
    Code:
    std::cout<<"To select Secure Disk Wipe, input SDW.\n";
    std::cout<<"To select Secure Disk Cleanse, input SDC.\n";
    ...these are not operations you want to be messing around with until you're very familiar to programming.
    I am making my own way, if you take a look at the selections, I am using my own method of Cleansing the disk and I am going to figure out a way to (safely) wipe it.
    Code:
    #include <fstream>
    #include <iostream>
    #include <windows.h>
    int main()
    {
    	std::string File,Option;
    	int Loop,SizeOfDisk,SizeOfFile,OrigSizeOfFile;
    	Loop=1;
    	std::cout<<"Welcome to C.J.'s Security 'suite'!\n";
    	std::cout<<"Please enter what you want to do.\n";
    	std::cout<<"To select Secure Disk Wipe, input SDW.\n";
    	std::cout<<"To select Secure Disk Cleanse, input SDC.\n";
    	std::cout<<"To select Secure File Deletion, input SFD.\n";
    	std::cout<<"To select ---this option is not yet available---.\n";
    	std::cout<<"To select ---this option is not yet available---.\n";
    	std::cout<<"Option:";
    	getline(std::cin,Option,'\n');
    	if(Option=="SDW")
    	{
    	    std::cout<<"Sorry,This option is not yet available";
    	    std::cin.get();
    	}    
    	if(Option=="SDC")
    	{
    		std::cout<<"Please enter Disk letter(Only letter):";
    		getline(std::cin,File,'\n');
    		if(File.length()!=2)
    		{
    			while(Loop==1)
    			{
    				File="";
    				std::cout<<"\nPlease enter Disk Letter Only:";
    				getline(std::cin,File,'\n');
    				if(File.length()==2)
    				{
    					Loop=0;
    				}
    			}
    			Loop=1;
    		}       
    		File=File+":\\SecureDiskCleanse.txt";
    		std::ofstream a_file(File.c_str());
    		while(Loop==1)
    		{
    			a_file<<"Secure Disk Cleanse.";
    			/*CheckSizeOfDisk*/
    			if(SizeOfDisk==0)
    			{
    				Loop=0;
    			}
    		}
    		a_file.close();
    		File="DEL "+File;
    		system(File.c_str());
    		std::cin.get();
    	}
    	if(Option=="SFD")
    	{
    		std::cout<<"Please enter file path and name.:";
    		getline(std::cin,File,'\n');
    		/*GET ORIGINAL SIZE OF FILE*/
    		std::ofstream a_file(File.c_str(), std::ios::trunc);
    		while(Loop==1)
    		{
    			a_file<<"Secure File Deletion.";
    			/*GET SIZE OF FILE*/
    			if(SizeOfFile>(OrigSizeOfFile+10))
    			{
    				Loop=0;
    			}    
    		}
    		a_file.close();
    		File="DEL "+File;
    		system(File.c_str());
    		std::cin.get();
    	}
    	else
    	{
    	    std::cin.get();
    	}
    	return 0;
    }

  8. #8
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Yeah, you're making a system() call to "DEL"... that's not exactly secure. You see, messing with file deletion is like juggling knives... you better know exactly what you're doing before you attempt it.
    Sent from my iPadŽ

  9. #9
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    hope you've got good backups!
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  10. #10
    Registered User
    Join Date
    Sep 2005
    Posts
    241
    I know ^_^ Thanks for your warning though. I am putting in error checking to make shure that the input is legit...

  11. #11
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    >>but I don't think the standard guarantees it is always one byte in size

    Yes, the standards say one character = 1 byte. But does not say how may bits make up that byte. I've heard there are some 4-bit operating systems, where 1 byte = 4 bits. But there are also 128-bit operating systems where 1 byte = 128 bits. And in the next year or so MS-Windows will be a 64-bit operating system where 1 byte = 64 bits.

  12. #12
    Registered User
    Join Date
    Apr 2006
    Posts
    6
    Just use this program

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    
    {
    cout << "The size of an int is:\t\t"    << sizeof(int)    << " bytes.\n";
    cout << "The size of a short int is:\t" << sizeof(short)  << " bytes.\n";
    cout << "The size of a long int is:\t"  << sizeof(long)   << " bytes.\n";
    cout << "The size of a char is:\t\t"    << sizeof(char)   << " bytes.\n";
    cout << "The size of a float is:\t\t"   << sizeof(float)  << " bytes.\n";
    cout << "The size of a double is:\t"    << sizeof(double) << " bytes.\n";
    
    cin.get();
    }

  13. #13
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    Quote Originally Posted by Ancient Dragon
    >>but I don't think the standard guarantees it is always one byte in size

    Yes, the standards say one character = 1 byte. But does not say how may bits make up that byte. I've heard there are some 4-bit operating systems, where 1 byte = 4 bits. But there are also 128-bit operating systems where 1 byte = 128 bits. And in the next year or so MS-Windows will be a 64-bit operating system where 1 byte = 64 bits.
    Careful! that's not quite right.

    I don't have the standard here, so I can't check what it say's about one char = one byte, but you're probably right about that.

    But to say that on a 64-bit os a byte is 64 bits is totally wrong. You use win32 at the moment. Tell me, is a byte on XP 32 bits? You're confusing pointer size with byte size (that's actually an over-simplification, but it'll do for now).
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  14. #14
    Registered User
    Join Date
    Sep 2005
    Posts
    241
    Well, how can I check the free space on a harddrive and the size of particular files???

  15. #15
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    Quote Originally Posted by bikr692002
    Well, how can I check the free space on a harddrive and the size of particular files???
    that's fairly platform dependant. if you're happy to write platform-dependant code, there should be plenty on msdn.

    Otherwise you could look at boost.filesystem which I think has some functions that'll help you.
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question On how to Fill icmp_data
    By Antigloss in forum Linux Programming
    Replies: 3
    Last Post: 04-25-2007, 09:42 PM
  2. Replies: 3
    Last Post: 12-22-2004, 07:29 PM
  3. Counting how many chars in a string
    By tigs in forum C Programming
    Replies: 4
    Last Post: 08-05-2002, 12:25 AM
  4. really got stuck with unsigned chars
    By Abdi in forum C Programming
    Replies: 7
    Last Post: 06-11-2002, 12:47 PM
  5. fancy strcpy
    By heat511 in forum C++ Programming
    Replies: 34
    Last Post: 05-01-2002, 04:29 PM