need some oversight

G

Grant Austin

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;
}

}
 
G

Grant Austin

Update:The issue I'm noticing is that the last entry is output(read)
twice.
 
G

Grant Austin

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
 
J

Jim Gordon

Grant Austin said:
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).
 
R

Rolf Magnus

Grant said:
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";
 

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

Forum statistics

Threads
474,151
Messages
2,570,854
Members
47,394
Latest member
Olekdev

Latest Threads

Top