0

I want to be able to write a line in a terminal (as below) that takes a text file in the same directory and inputs its name into an executable file.

cat fileName | executable

I want to be able to read the fileName into c++ code. I already have a code that accesses the lines and reads through the file, it is just receiving fileName from the standard input.

Is there a line of code or a function to read the fileName into a c++ program and store it as a string? I am currently using the below code to read through each line of the text file.

ifstream myfile; 
myfile.open(fileName.c_str());

if( myfile.is_open() )
{   
    while ( getline (myfile ,line) )
    {
                  ......
    }
}
2
  • 2
    just read from standard input, that's where | pipes the data. Commented Jul 22, 2015 at 18:35
  • Just for the record, it's more normal to put filenames on the command line. Then your program can work even on filenames that contain newlines. (Yes, that's possible. No, it's not common or a good idea.) But then you can say ./myprog *.c). Some programs do have an option to read filenames from stdin, for use with find ... | myprog, but you can always find ... -exec myprog {} +, now that find has the + feature for exec. There's always ... | xargs -d'\n' myprog. Commented Jul 22, 2015 at 20:09

3 Answers 3

2

When you do cat filename | executable you are not sending the name of the file to your executable, but its contents. If you wish to send the name, use echo filename | executable or executable filename. Then you can process argc and argv as usual, and do the normal file reading you are showing in your example code.

2
  • Thank you very much! I am new to both Linux and C++ so I didn't know about the echo code. That worked really well.
    – Onpetcow
    Commented Jul 22, 2015 at 18:44
  • The way to say "thank you" on Stackoverflow is to press the upward pointing triangle. You should also mark one of these answers as correct by clicking the check mark. Commented Jul 22, 2015 at 18:49
1

All you need to do is read from stdin into a variable (e.g. "fname"):

int main () {
  string fname;
  cin >> fname
  ifstream myfile;
  myfile.open (fname);
  if (myfile.is_open() ) {   
    while ( getline (myfile, line) ) {
      ...

To accomodate either "pipe the name" or "use argc/argv" (as many *nix commands do):

int main (int argc, char *argv[]) {
  string fname;
  if (argc == 1) {
    cin >> fname
  }
  else {
    fname = argv[1];
  }
  ifstream myfile;
  myfile.open (fname);
  if (myfile.is_open() ) {   
    while ( getline (myfile, line) ) {
      ...
3
  • Thanks for your help! It was far more straightforward than I was trying to make it as most things are.
    – Onpetcow
    Commented Jul 22, 2015 at 18:44
  • What type of command line in a linux terminal would you use to pipe the file name through? I have been trying to use "cat filename | program" but it doesn't seem to read the file properly.
    – Onpetcow
    Commented Jul 23, 2015 at 17:03
  • No, you want echo filename. "cat" prints the contents of the file. "echo" prints the string "f-i-l-e-n-a-m-e".
    – paulsm4
    Commented Jul 23, 2015 at 17:45
1

You're actually unclear, if you want to read the file from std::cin as it would be supplied by

cat filename | executable

or if you actually get the filename to be opened passed from std::cin.

Most likely you want to have an optional command line parameter (passed to int main(int argc, char* argv[])), and have your reading code rely to a std::istream input source, rather than std::cin or std::ifstream hardcoded.

2
  • How does istream differ from ifstream? Would the method that I used to read the file have to change at all?
    – Onpetcow
    Commented Jul 22, 2015 at 20:21
  • It differs insofar, you could use a reference to std::cin or std::ifstream transparently, and don't need to care, how the stream was actually instantiated. Commented Jul 22, 2015 at 20:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.