Solved

Input file from command line using '<' operator

Posted on 2003-10-28
10
170 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
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
 

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
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.

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now