Solved

Input file from command line using '<' operator

Posted on 2003-10-28
10
171 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SendMessage fails while PostMessage works 2 107
Lambda for random numbers problem 7 107
Least Squares Curve Fitting 4 61
Copy output image from TWindowsMediaPlayer 6 23
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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 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.

896 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

13 Experts available now in Live!

Get 1:1 Help Now