eranga262154 22 Junior Poster

All about the String class. Check the documentation for more.

eranga262154 22 Junior Poster

I suggest 2D Graphics. Work on Intelli JIdea.

eranga262154 22 Junior Poster

Definitely you need to use 2D Graphic to do this.

eranga262154 22 Junior Poster

I don't think that explanation of such topics on a thread is not easy. As jwenting says, better to refer a book or whatever and question after you comes with. On the web you can find a lot. One of my recommend is the Sun official web site, as most of the people suggested.

eranga262154 22 Junior Poster

did some work with JAVASWING. with UI creation.
My intension over here, is that i can view the Constriants visually and control using the GUI interface.

Do you recommend any source code examples?
Just to show some constraints that i set for my program. such as (a timetabling problem)

1. No student should assigned to CM401 at the same time
2. no classes allocated during 12:00PM to 1:00PM

I am looking for the data-visualization and constraint satisfaction to be visualized.

What you mean data-visualization. I think you may use data base to do this.

eranga262154 22 Junior Poster

>>What is the wrong with this code
Nothing that I know of, other than it doesn't work :) This produces the wrong value, probably because line 10 is only one byte of a 4-byte number.

Thanks, I got the point and stuck with this whole night. When I use it only on byte count there in my code.

Thanks again,

eranga262154 22 Junior Poster

Thanks, I'll work with your suggestion and if I found any difficulties right back here.

eranga262154 22 Junior Poster

Thanks,

What is the wrong with this code.

char_traits<char>::char_type ch = next4[4] ;
char_traits<char>::int_type int_val ;
int_val = char_traits<char>::to_int_type (ch) ;
eranga262154 22 Junior Poster

I've read 4 consecutive bytes from a binary file, and store the value as follows,

int main()
{
char next4[4] ; 

while(!fileopen.eof())
{
        fileopen.read (next4, 4 ) ;
}

}

What I want to do is, that value in next4 want to convert into a decimal/int value. How can I do that. Search on MSDN and unable to find a clear explanation.

eranga262154 22 Junior Poster

Ok' I'll put mu code here. Actually I have no important on first four bytes. I need to read next four byes. Here what I have tried up to now.

#include <iostream>
#include <fstream>

using namespace std ;

int main ()
{
	ifstream fileopen ;
	char buffer[100 ] ;
	char c ;

	fileopen.open ( "G00046_002_01.srf", ios :: in | ios :: out | ios :: binary ) ;

	if(fileopen.is_open())
	{
		fileopen.seekg(0, ios :: beg) ; // Move pointer to the beginning
		while(!fileopen.eof())
		{
			fileopen.read(buffer, 50) ;
			fileopen.seekg(4) ;
			fileopen >> c ;        // Just to check the file read correctly 
			cout << c << endl ;

                       // Now I want to read the next 4 bytes here

		}
		fileopen.close() ;
	}

	else
	{
		cout << "Unable to open the file\n" ;
	}


	cin.get() ;
	return 0 ;
}

What is your suggestion for me.
Help appreciate

eranga262154 22 Junior Poster

I have a binary file, contain set of stream, and I want to find some information there. What I have done up to now is read the file, open it and count the number of streams three. I'm stuck now.

Just consider one stream. I want to skip first 4 bytes and read the next 4 bytes. The second 4 bytes gives the length of the next part and that is the end of one stream. Same process should follows until EOF is found. My question is how to skip that first 4 bytes and point to the next byte.

eranga262154 22 Junior Poster

I understand the connection you are making, but one can make UIs with Swing without having to directly use any of the Graphics or Graphics2D methods. Only when he gets into wanting to produce charts and graphs would he need to learn the Graphics2D API.

That is the reason that I mentioned them separately. They can be treated as two independent subjects basically.

Of course, you are right. You know what I'm going to point out there.

eranga262154 22 Junior Poster

Actually swing is the base of Java graphic, because when you work on swing you have to cover all the fundamentals. So it is good to start.

By yourself working, it will take little more time to do your work successfully.

eranga262154 22 Junior Poster

I think it is not much difficult to start work with applets and swing by referring a best tutorial. Actually try with worked examples.

Sun official web site always provide the best tutorials on that. All the time I used sun web site to find such informations.

Here is a link.

If you like to refer a book, if you have time to spend on that, this is the best book I recommended.

Java 2D API Graphics, by: by Vincent J. Hardy

eranga262154 22 Junior Poster

So, use a array. That's the simple way I think you have to do it. Count the number of elements and handle it with the length property of the array and your conditions.

eranga262154 22 Junior Poster

Ok, here's the full code.

#include <iostream>
#include <fstream>
#include <limits>
#include <bitset>

using namespace std ;

int main()
{
	ofstream filebin ;
	filebin.open("RecordBinaryData.txt") ;

	ifstream file ( "path to binary file" ) ; 
	file >> noskipws ;
	typedef bitset< numeric_limits<unsigned char>::digits > bitset ;
        unsigned char byte ;
	long count = 0 ;
	
	if(filebin.is_open())
	{
		while( file >> byte )
		{
			//cout << bitset(byte) ; // to print bits as it is
			filebin << bitset(byte) ;
			count ++ ;
		}
		const long const_count = count ;
		long arr[const_count] ;

		filebin.close() ;
	}

	else
	{
		cout << "Unable to open the file.\n" ;
	}

cin.get() ;
return 0 ;
}

I have attached the binary file as well.

eranga262154 22 Junior Poster

What you mean. Is the not enough details there.

eranga262154 22 Junior Poster

The number of bits in the file is the number of bytes in the file multiplied by the number of bits in a byte.

Ya it's true. What the point here.

Anyway now I know that how many bytes are there. Actually I count them. But now I have another question here. I want to store that number in an array.

So, after counting number of bytes there, equal it to the constant value and put it to the array. But get the error.

error C2057: expected constant expression
error C2466: cannot allocate an array of constant size 0
error C2133: 'block' : unknown size

This is code,

long count = 0 ;

	if(filebin.is_open())
	{
		while( file >> byte )
		{
			filebin << bitset(byte) ;
			count++ ;
		}
		filebin.close() ;

		const long const_count = count ;
		long arr[const_count];
	}

What's wrong there.

eranga262154 22 Junior Poster

Yeah, but I think you have enough info and code from everyone in this thread to help you write more of your program. It's starting to seem like you're asking us to write it for you.

No sir, after reading a lots of tutorials on the web and discussions in this forum I'm really confusing. As I said earlier, I'm new for C++ and my basic knowledge also in lower level.

eranga262154 22 Junior Poster

Now file is in a text file(a notepad). Can I read a bit from there.

eranga262154 22 Junior Poster

Why not read a byte at a time and multiply the result by CHAR_BIT or equivalent?

Can you explain little more. I'm really newbie for those stuff. Help appreciate.

eranga262154 22 Junior Poster

I'm not sure that I have use the correct word for my requirement. Here the issue.

Read a binary file and count the number of bits using following way.

ifstream :: pos_type size ;
char * memblock ;

int main()
{
	ifstream fileread ("RecordBinaryData.txt", ios::in|ios::binary|ios::ate) ;
	if(fileread.is_open())
	{
		size = fileread.tellg () ;
		memblock = new char [size] ;
		fileread.seekg (0, ios :: beg) ;
		fileread.read (memblock, size) ;
		fileread.close () ;

		cout << "\nSize of the file is " << (size * 8) << " bits." ;

		delete[] memblock;
	}
}

Is this correct?

Next I want to store them to an array. So the length should be (size*8), in bits. I tried,

long arr[(size * 8)]

and failed. Why is that?

eranga262154 22 Junior Poster

Do you need to read in your file as binary, or is it just a plain text file?

There what I want. I don't think that repeating it.

I have a binary file and,

1.) read the file as binary (zeros and ones)

2.) count the number of bits

3.) group them, including in predefined number of bits. Say in my file have 1000bits, and I want to group them of 10bits. Simply divide by 10 of 1000.

4.) display some of the groups in a separate text file. Some of them as it is and some of them as decimal.

Actually that binary file is a message which are store in our university server. I want read it, then collect some informations. That's the basic idea.

I think clear enough me to all of you.

eranga262154 22 Junior Poster

>> .. really mad. It,s not displays as a binary code, I mean bunch of zeros and ones.

if all you want to do is display the contents of a file as a sequence of bits (i presume that is what you mean by bunch of zeros and ones),

#include <fstream>
#include <limits>
#include <bitset>
#include <iostream>
using namespace std;

int main()
{
  ifstream file( __FILE__ ) ; file >> noskipws ;
  typedef bitset< numeric_limits<unsigned char>::digits > bitset ;
  unsigned char byte ;
  while( file >> byte ) cout << bitset(byte) ;
}

Great, Thanks.

But I need one thing to do there. I want to count the number of bits. I'll try it now. Do you have any suggestions.

eranga262154 22 Junior Poster

How does that make a difference? A string just handles the memory for the char array instead of making you do it. I don't think there should be any problem with using a string as long as you don't do any text conversions.

ifstream fileread ("path to the binary file");
string contents;
char ch;

// Get the binary data
while ( fileread.get( ch ) ) {
  contents.push_back( ch );
}

// Print the binary data as hex
for ( int i = 0; i < contents.size(); ++i ) {
  cout<< hex << int( contents[i] ) <<" ";
}

fileread.close();

Actually later I want to convert some parts to decimal.

eranga262154 22 Junior Poster

Ok, first what I have done is read a binary file and count the number of bytes there. Then make groups of 20 bytes. From that I can find number of bits, multiplying by 8. I think it is ok.

This is the code use to count the bytes of the file.

long begin, end;
	ifstream filesize ("[I]path of the binary file[/I]");
	
	begin = filesize.tellg();
	filesize.seekg (0, ios::end);
	end = filesize.tellg();
	filesize.close();

	cout << "Size is " << (end - begin) << " bytes.\n";

Then what I want to do is such a small 20byte group should be display in decimal.

I think clear enough. So..

eranga262154 22 Junior Poster

I have a binary data block(a byte), and I want to convert it to decimal. Is there any inbuilt function to do that.

eranga262154 22 Junior Poster

Thanks for all the links.

eranga262154 22 Junior Poster

So, what should I do? Can you explain a little more.

eranga262154 22 Junior Poster

I wrote one as follows.

string line;
	ifstream fileread ("[I]path to the binary file[/I]");

	while(! fileread.eof())
	{
		getline(fileread, line);
		cout << line << endl;
	}

	fileread.close();

Well I read a binary file using this, the command prompt is really mad. It,s not displays as a binary code, I mean bunch of zeros and ones.

Can you say where I'm going wrong.

eranga262154 22 Junior Poster

Wow, I got it. Thanks a lot all of guys comments.

Thanks again,

eranga262154 22 Junior Poster

Can someone explain, how can read a binary file. I'm really nervous with this.

eranga262154 22 Junior Poster

I have put it in my original post.

eranga262154 22 Junior Poster

Ok, Here what I have done.

I used Microsoft Visual Studio .Net 2003.

First click File -> New -> Project

From New Project window, select Visual C++ Project as project type and Win32 Console Project as template.

On the Application Settings, select Console Application and Empty Project.

But I'm fail. What should I do?

eranga262154 22 Junior Poster

Hi all,

I have just start to work on C++.

I want to open a text file, then write few text there. This is the code I wrote for that.

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
	ofstream file;
	file.open ("example.txt");

	if(file.is_open())
		{
			file << "Write a text line";
            		file.close();
		}
	else
		{
			file << "Unable to wirte text";
		}
	return 0;
}

When I compile it gives the following error.

Compiling...
rbf.cpp
Linking...
LIBCD.lib(wincrt0.obj) : error LNK2019: unresolved external symbol _WinMain@16 referenced in function _WinMainCRTStartup
Debug/ReadFile.exe : fatal error LNK1120: 1 unresolved externals

What's wrong there. Can you guys help me to solve it.

Ancient Dragon commented: Thanks for using code tags +18