By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,407 Members | 920 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,407 IT Pros & Developers. It's quick & easy.

need some oversight

P: n/a
Hi all,

This is my first time posting so please bear with me.

I would like some criticism of some elementary C++ work I'm doing because
it's been a long long long time and I'm not sure if I'm doing things
correctly/safely.

Particularly I'm having trouble remembering proper use of input streams.
The code I came up with follows at the bottom of the post.

Simply what I need to do is read a file from beginning to end, in hex.
Here is a sample of input:

0 1000
0 2000
0 3000
1 2000
0 4000
2 5000
2 1000

Kind of irrelevent but possibly of interest. The first column of numbers is an action; read,
write, instruction fetch. The second column of numbers is a virtual
address.

The final goal of this program (by Tuesday) is to simulate a handful of
page replacement algorithms.

Regards,
Grant

#include <iomanip.h>
#include <iostream.h>

int main(int argc, char * argv[]) //will be used for cmdline switches
// all input files will come from std input eg. a.out < belady.din
long int a,b;

while(!cin.eof()){
if(!cin.eof())
cin >> hex >> a >> hex >> b;

/* do everything important */

if(!cin.eof())
cout << a << b << endl;
}

}
Jul 22 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Update:The issue I'm noticing is that the last entry is output(read)
twice.

Jul 22 '05 #2

P: n/a
On Sat, 06 Dec 2003 02:12:50 +0000, Grant Austin wrote:
Update:The issue I'm noticing is that the last entry is output(read)
twice.

yet further clarification: that is the last entry is output(read) twice
when I remove the cin.eof statements which IMHO seem a tad kludgy
Jul 22 '05 #3

P: n/a

"Grant Austin" <ga*****@foo.foo.bar.net> wrote in message
news:pa****************************@foo.foo.bar.ne t...
Update:The issue I'm noticing is that the last entry is output(read)
twice.


I'm a little rusty on my C++ as well, but I believe you may need to use a
priming read OR on the last itteration the last line has been read but the
EOF is NOT yet been read (C++ does not read ahead).
Jul 22 '05 #4

P: n/a
Grant Austin wrote:
Hi all,

This is my first time posting so please bear with me.

I would like some criticism of some elementary C++ work I'm doing
because it's been a long long long time and I'm not sure if I'm doing
things correctly/safely.

Particularly I'm having trouble remembering proper use of input
streams. The code I came up with follows at the bottom of the post.

Simply what I need to do is read a file from beginning to end, in hex.
Here is a sample of input:

0 1000
0 2000
0 3000
1 2000
0 4000
2 5000
2 1000

Kind of irrelevent but possibly of interest. The first column of
numbers is an action; read, write, instruction fetch. The second
column of numbers is a virtual address.

The final goal of this program (by Tuesday) is to simulate a handful
of page replacement algorithms.

Regards,
Grant

#include <iomanip.h>
#include <iostream.h>
Those are not standard headers. Use <iomanip> and <iostream> instead.
However, cin, cout and endl will be in namespace std, so you must
qualify them like std::cin.
int main(int argc, char * argv[]) //will be used for cmdline switches
// all input files will come from std input eg. a.out < belady.din
long int a,b;

while(!cin.eof()){
if(!cin.eof())
That if() is redunant, since the loop will only be entered if cin.eof()
is false.
cin >> hex >> a >> hex >> b;

/* do everything important */

if(!cin.eof())
cout << a << b << endl;
}
You are using eof() the wrong way, since eof() will only be true _after_
you tried to read past the end. This means that your loop will be
entered once too often. Further, you will get an endless loop if the
stream can't be read for any other reason than eof. You could write it
like this:

while(std::cin >> std::hex >> a >> std::hex >> b)
std::cout << a << b << std::endl;

if (!std::cin.eof())
std::cout << "Read error\n";

}

Jul 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.