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
Solved

Input file from command line using '<' operator

Posted on 2003-10-28
10
173 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

 

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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying 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

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…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.

791 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