Solved

Input file from command line using '<' operator

Posted on 2003-10-28
10
172 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

776 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