Read lines from .txt file?

J

James Kanze

Can you point out a tutorial on "how to deal with command line
arguments" in C++ that doesn't implement any type of parser?

There probably isn't a tutorial, because there's nothing to do.
Command line arguments are passed to the application already
parsed, so the client code doesn't have to parse them (unless it
defines some more complex grammar for their contents---not
usually the case).
 
R

Rui Maciel

James said:
There probably isn't a tutorial, because there's nothing to do.
Command line arguments are passed to the application already
parsed, so the client code doesn't have to parse them (unless it
defines some more complex grammar for their contents---not
usually the case).

That's not true. The arguments that are passed through the command line to an application are not parsed. They are simply tokenized and only in a particular way that may or may not be convenient. You still have to parse them, possibly after re-tokenizing them, in order to extract some meaning from the arguments.


Rui Maciel
 
R

Rui Maciel

Daniel said:
Of course it deals with command line arguments. Like any other
possible solution, it requires the command line arguments to be in a
particular format, and parses them out based on the format it
requires.

What you provided isn't a solution. It's broken code that fails one of the most basic rule of software development, which is input validation.

What does it take for you to consider a program "decent"?
Must the code that parses the command line arguments be complex for a
program to be decent? I don't think so...

By "decent" I mean something beyond hello world that's intended to be used more often by other people than the times it was compiled.


Rui Maciel
 
R

Rui Maciel

Michael said:
I have worked in some scientific environment where people wouldn't care
about a language and where happy enough to put rows of values in a file.
After all you can always add a tool to produce the data and ensure its
integrity.

If they had simply implemented a parser to begin with then they wouldn't need an external tool to compensate for the short comings of their poorly designed software.


Rui Maciel
 
J

Jorgen Grahn

Daniel said:
That will suit the OPs purpose? Sure:
int main( int argc, const char* argv[] )
{
if (argc < 4) return EXIT_FAILURE;
int w = lexical_cast<int>(argv[1]);
int h = lexical_cast<int>(argv[2]);
int S = lexical_cast<int>(argv[3]);
// use w, h, and S
}
lexical_cast can be found in boost or easily written
yourself. (Again, add error handling code as necessary.)

That code doesn't deal with command line arguments. It is
nothing more than a flimsy hack that blindly tries to convert
strings to ints.

Of course it deals with command line arguments. Like any other
possible solution, it requires the command line arguments to be in a
particular format, and parses them out based on the format it
requires.
I seriously doubt that there is even a single decent program
out there that relies on that to parse command line arguments.

What does it take for you to consider a program "decent"?
Must the code that parses the command line arguments be complex for a
program to be decent? I don't think so...

Rui isn't polite, but I agree the code above isn't enough. I'd expect
to get a usage message and EXIT_FAILURE if there aren't exactly three
integer arguments (in the range of int).

On Unix, I'd additionally expect full getopt()-style command-line
parsing, e.g.

foo -w 2 -S -4686288 -h42

What's "decent" varies with the OS.

/Jorgen
 
J

Jim Langston

mlt said:
Ian Collins said:
mlt said:
I have a file containing:

w = 23
h = 34
S = 2

I would like to read this text file into variables in a program like:
std::string file = "args.txt";
std::ifstream infile;
infile.open (&file[0]);

int w,h,S;

w = infile.??
h = infile.??
S = infile.??


But how do I read the value on each line in the file after the '=' to a
variable in my program?

Two choices, read the file line by line (using std::getline) and
tokenise the line or read the file token by token (>>).

The fist is better if the input format varies (inconsistent use of white
space for example).

Ok before I look into that, are there any better way to pass multiple
runtime arguments to an application?

"Better" is subjective. There are different ways. The two most common ways
are:

1. Command line parameters
2. Read from a file

Which can also work together with the filename to be read passed as a
command line parameter. It really depeneds on what the command line
parameters are going to be doing and your application. It is usually easier
for the end user for a non console program for the parameters to be saved to
a file by the application itself and read in when initialized. For a
console application sometimes it's better to be bassed as a command line
parameter (cat, ls, etc...).

And, it's even possible that you don't even want to pass them in or read
them but have them input in the application itself. It depends on how often
the values will change and what they represent.
 
D

Daniel T.

Rui isn't polite, but I agree the code above isn't enough. I'd expect
to get a usage message and EXIT_FAILURE if there aren't exactly three
integer arguments (in the range of int).

I did warn that error code needed to be added. I think what I posted
was quite enough considering the OP didn't post any code of his own,
and the question has the feel of a homework assignment.
On Unix, I'd additionally expect full getopt()-style command-line
parsing, e.g.

  foo -w 2 -S -4686288 -h42

What's "decent" varies with the OS.

What is "decent" doesn't vary with how the program parses command line
arguments. Many "decent" programs don't accept any command line
arguments at all.
 

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
473,774
Messages
2,569,596
Members
45,142
Latest member
arinsharma
Top