Question again?

M

Milk

HI,
i try to read my HEX file, but i dunno why i just can read the first one in
my file ?
anyone can help me?

My code:
char c[5000000];
char d[5000000];

int main()
{

ifstream ifs;
ifs.open("opcode.txt",ios::binary);
if (ifs)
{

ifs >> c;
if
{
cout<< c <<endl;
}

else
{
cout <<"no file"<<endl;
}


~ Let us linux ~
 
B

Bill Seurer

Milk said:
HI,
i try to read my HEX file, but i dunno why i just can read the first one in
my file ?

Read the first one what?
char c[5000000];
char d[5000000];

What happens if your file is 5 million characters long? You shouldn't
code things like this as constants. You don't use "d" in your code below.
int main()
{

ifstream ifs;
ifs.open("opcode.txt",ios::binary);
if (ifs)
{

ifs >> c;
if

What is this "if" supposed to do?
{
cout<< c <<endl;

Does your file actually contain displayable characters? If not, what do
you think this will do?
}

else
{
cout <<"no file"<<endl;
}

Count your { and }. The numbers should match. Your main says it
returns an "int" so where's your return statement?
 
C

Christopher Benson-Manica

Milk said:
i try to read my HEX file, but i dunno why i just can read the first one in
my file ?
anyone can help me?

Possibly, if you phrased your question better... I'll add to what was
already posted, however.
char c[5000000];
char d[5000000];

1) What if your implementation doesn't like two 5 megabyte stack
buffers? Many implementations don't.
2) You want unsigned char; unadorned chars may or may not be signed,
and signed chars may not get along with binary data.
 
J

John Harrison

Milk said:
HI,
i try to read my HEX file, but i dunno why i just can read the first one in
my file ?
anyone can help me?

My code:
char c[5000000];
char d[5000000];

int main()
{

ifstream ifs;
ifs.open("opcode.txt",ios::binary);

We had this question fairly recently, try this

ifs.open("opcode.txt",ios::binary|ios::input);

If that fixes it then its a bug in your implementation of the standard
library.

john
 
M

Milk

i would like to make my program know the HEX in my input file, for example
(0x27a50004(in hex) = addu $s1, $s2, $s3)
then my program read it know 0x27a50004 after that the program will add it
for me and i need to store the s1 value register in my other array.
therefore later i can call it back and add s1 +s2 something like that~
i just dunno why if i don't have char d[5000000]; this array i just can read
the first line ~
if i have char d[5000000]; i can read second line, so did any one can teach
me??
thanks a lot


My code::
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

char c[5000000];
char d[5000000];

int main()
{

ifstream ifs;
ifs.open("opcode.txt",ios::binary);
if (ifs)
{
ifs >> c;
ifs >> d;
ifs.close();
}
else
{
cout <<"no file"<<endl;
}

cout << c << endl;
cout << d << endl;
return 0;

}
Milk said:
HI,
i try to read my HEX file, but i dunno why i just can read the first one in
my file ?
anyone can help me?

My code:
char c[5000000];
char d[5000000];

int main()
{

ifstream ifs;
ifs.open("opcode.txt",ios::binary);
if (ifs)
{

ifs >> c;
if
{
cout<< c <<endl;
}

else
{
cout <<"no file"<<endl;
}


~ Let us linux ~


~ Let us linux ~
 
K

Karl Heinz Buchegger

Milk said:
HI,
i try to read my HEX file, but i dunno why i just can read the first one in
my file ?
anyone can help me?

My code:
char c[5000000];
char d[5000000];

That's not a good idea if you are dealing with truely binary data.
unsigned char is the way to go.
int main()
{

ifstream ifs;
ifs.open("opcode.txt",ios::binary);
if (ifs)
{

ifs >> c;

Since you are trying to read binary data, operator >> is not what
you want to use. Use the streams read function as in:


#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
string FileName;
unsigned char c;

cout << "Enter filename: ";
cin >> FileName;

ifstream InFile( FileName.c_str(), ios::binary );

if( InFile ) {
while( InFile.read( (char*)&c, 1 ) )
cout << hex << (unsigned int)c << ' ';
}

return 0;
}


Good luck to your project.
 
J

John Harrison

Milk said:
i would like to make my program know the HEX in my input file, for example
(0x27a50004(in hex) = addu $s1, $s2, $s3)

You need to use read not >>.

long opcode;
ifs.read((char*)&opcode, sizeof opcode);

john
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,164
Messages
2,570,898
Members
47,440
Latest member
YoungBorel

Latest Threads

Top