?
Solved

Input file from command line using '<' operator

Posted on 2003-10-28
10
Medium Priority
?
177 Views
Last Modified: 2012-05-04
I am currently working on a project which requires the program to get the input file from the command line using '<', not from a prompt within the program.  I understand how to get the information out of the file using the std::cin command, but what I was wonder is how I could retrieve the actual filename of the input file that is entered.

example:
>> ./myprog<inputvar.txt

How do I get the name of inputvar to use in my main function?

Thanks,
Ant_Dogg
0
Comment
Question by:Ant_Dogg
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 9634697
>>How do I get the name of inputvar to use in my main function?

You won't need it - just read the input from 'stdin', that's where the shell will direct the contents of the file to.

E.g.

cin >> InputFromStdIn;

or

fgets ( InputFromStdIn, stdin);

or

gets ( InputFromStdIn);
0
 
LVL 15

Expert Comment

by:efn
ID: 9635392
If there is a way to do this at all, it will be platform-specific.  If you specify your target operating system, someone may be able to advise you.
0
 

Expert Comment

by:gdean
ID: 9636149
geesh jkr you're too fast 6 mins
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Ant_Dogg
ID: 9637029
I'm using linux.  The problem entails the input file to be entered from the command prompt, playing with the data (I have not problem with this part), and then making an output file using the name of the input file with a suffix on it.  If the input file were test.txt, I would want the output file to be textout.txt, so on and so forth.  Does this make more sense?

Antdogg
0
 
LVL 13

Expert Comment

by:SteH
ID: 9640053
You can use on any platform
>> ./myprog<inputvar.txt > inputvarout.txt
and use stdout to write your output.
0
 

Author Comment

by:Ant_Dogg
ID: 9645694
That would work except for the fact that I have to generate multiple output files, each with a different suffix on it.  If the input file was input.txt, I would have to make input1.txt, input2.txt, input3.txt.  What I really need to know how to do is how to put the input filename from the command line (via '<') into a string variable.
0
 
LVL 13

Expert Comment

by:SteH
ID: 9648416
Since the command line does not contain the part after < could you do the redirection?
./myprog -i inputvar.txt
now you find
inputvar.txt as argv[2] in your main. And it is up to your program to redirect stdin to this file.
0
 

Author Comment

by:Ant_Dogg
ID: 9651760
That sounds like it would solve my problem, SteH, however I am having trouble getting the files opened.  Would I code:

      ifstream inFile(argv[2].c_str());          // open input stream

      ofstream outFile(argv[2].var);       //open output stream

0
 
LVL 9

Expert Comment

by:tinchos
ID: 10546256
No comment has been added lately, so it's time to clean up this question.
I will leave the following recommendation for this question in the Cleanup topic area:

PAQ with points refunded

Please leave any comments here within the next four days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Tinchos
EE Cleanup Volunteer
0
 

Accepted Solution

by:
ee_ai_construct earned 0 total points
ID: 10586140
PAQed, with points refunded (125)

ee_ai_construct
Community Support Moderator
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question