Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Redirection in command line "<"

Redirection in command line "<"

Scheduled Pinned Locked Moved C / C++ / MFC
c++question
8 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    uusheikh
    wrote on last edited by
    #1

    Hi, I know that if i do this; program1.exe > out.txt, all the output of program1.exe will be written to out.txt file (printf, cout). But, what happens when i do program1.exe < input.txt? Imagine input.txt contains some data, how would i load the data into program1.exe with the above command? What C++ code should i write to handle that? Also, if i do program1.exe < input.txt, the argc in main() will only return 1, that is program1.exe. Why not 3? (for program1.exe, <, and input.txt) Explanantion appreciated. Thanks. (I'm using win32 console app on VC++)

    L F 2 Replies Last reply
    0
    • U uusheikh

      Hi, I know that if i do this; program1.exe > out.txt, all the output of program1.exe will be written to out.txt file (printf, cout). But, what happens when i do program1.exe < input.txt? Imagine input.txt contains some data, how would i load the data into program1.exe with the above command? What C++ code should i write to handle that? Also, if i do program1.exe < input.txt, the argc in main() will only return 1, that is program1.exe. Why not 3? (for program1.exe, <, and input.txt) Explanantion appreciated. Thanks. (I'm using win32 console app on VC++)

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      uus831 wrote:

      But, what happens when i do program1.exe < input.txt?

      stdin is changed from the keyboard to input.txt To read the data you can use anything that reads from stdin line scanf(), getch() etc

      uus831 wrote:

      Also, if i do program1.exe < input.txt, the argc in main() will only return 1, that is program1.exe. Why not 3? (for program1.exe, <, and input.txt)

      Because the < input.txt is not an argument to your application, its an instruction to the os to modify what stdin points to

      1 Reply Last reply
      0
      • U uusheikh

        Hi, I know that if i do this; program1.exe > out.txt, all the output of program1.exe will be written to out.txt file (printf, cout). But, what happens when i do program1.exe < input.txt? Imagine input.txt contains some data, how would i load the data into program1.exe with the above command? What C++ code should i write to handle that? Also, if i do program1.exe < input.txt, the argc in main() will only return 1, that is program1.exe. Why not 3? (for program1.exe, <, and input.txt) Explanantion appreciated. Thanks. (I'm using win32 console app on VC++)

        F Offline
        F Offline
        fefe wyx
        wrote on last edited by
        #3

        Note that stdin and stdout are also file handles. When no redirection occurs, stdin is the keyboard and stdout is the monitor. In the form of program1.exe > out.txt, stdout is assigned with a file handle to the file out.txt, instead of the monitor. So all the output goes into out.txt instead of the monitor. In the form of program1.exe < in.txt, stdin is assigned with a file handle to the file in.txt, instead of the keyboard. So whenever you read from stdin, e.g. when you call the scanf() function, the data come form in.txt, not the keyboard. Redirecting is handled by the OS, not by the program, so the program itself will know nothing about the redirection, and redirection directives would be passed to program as parameters. Thus argc in main is 1, not 3.

        U 1 Reply Last reply
        0
        • F fefe wyx

          Note that stdin and stdout are also file handles. When no redirection occurs, stdin is the keyboard and stdout is the monitor. In the form of program1.exe > out.txt, stdout is assigned with a file handle to the file out.txt, instead of the monitor. So all the output goes into out.txt instead of the monitor. In the form of program1.exe < in.txt, stdin is assigned with a file handle to the file in.txt, instead of the keyboard. So whenever you read from stdin, e.g. when you call the scanf() function, the data come form in.txt, not the keyboard. Redirecting is handled by the OS, not by the program, so the program itself will know nothing about the redirection, and redirection directives would be passed to program as parameters. Thus argc in main is 1, not 3.

          U Offline
          U Offline
          uusheikh
          wrote on last edited by
          #4

          Thanks for the info. But how do i determine how much data to read from the file? I mean, if the file in.txt contains 10 rows of data, how can i determine that i need to call scanf 10 times? Or, i cant?

          M D 3 Replies Last reply
          0
          • U uusheikh

            Thanks for the info. But how do i determine how much data to read from the file? I mean, if the file in.txt contains 10 rows of data, how can i determine that i need to call scanf 10 times? Or, i cant?

            M Offline
            M Offline
            Maxwell Chen
            wrote on last edited by
            #5

            uus831 wrote:

            But how do i determine how much data to read from the file? I mean, if the file in.txt contains 10 rows of data, how can i determine that i need to call scanf 10 times? Or, i cant?

            To read file, I suggest you not to use DOS-shell indirection (< ) but use program arguments instead. Such as:

            MyTest.exe -i "sample.txt"
            or
            MyTest.exe /i "sample.txt"

            In the above example, your application handles the command-line arguments and gets the file name(s). Then you can use fstream to load the content of the whole file(s), or to read part of the file(s).

            Maxwell Chen

            U 1 Reply Last reply
            0
            • U uusheikh

              Thanks for the info. But how do i determine how much data to read from the file? I mean, if the file in.txt contains 10 rows of data, how can i determine that i need to call scanf 10 times? Or, i cant?

              M Offline
              M Offline
              Maxwell Chen
              wrote on last edited by
              #6

              As the Windows built-in tool, fc.exe, it compares two files. The command for using this tool is as:

              fc /b C:\Temp\hello.txt D:\Doc\hello_2.txt

              Maxwell Chen

              1 Reply Last reply
              0
              • M Maxwell Chen

                uus831 wrote:

                But how do i determine how much data to read from the file? I mean, if the file in.txt contains 10 rows of data, how can i determine that i need to call scanf 10 times? Or, i cant?

                To read file, I suggest you not to use DOS-shell indirection (< ) but use program arguments instead. Such as:

                MyTest.exe -i "sample.txt"
                or
                MyTest.exe /i "sample.txt"

                In the above example, your application handles the command-line arguments and gets the file name(s). Then you can use fstream to load the content of the whole file(s), or to read part of the file(s).

                Maxwell Chen

                U Offline
                U Offline
                uusheikh
                wrote on last edited by
                #7

                Ok, thanks man. That's a better idea.

                1 Reply Last reply
                0
                • U uusheikh

                  Thanks for the info. But how do i determine how much data to read from the file? I mean, if the file in.txt contains 10 rows of data, how can i determine that i need to call scanf 10 times? Or, i cant?

                  D Offline
                  D Offline
                  David Crow
                  wrote on last edited by
                  #8

                  uus831 wrote:

                  But how do i determine how much data to read from the file? I mean, if the file in.txt contains 10 rows of data, how can i determine that i need to call scanf 10 times?

                  fscanf() will tell you when it has read past the end of the file stream:

                  while (fscanf(stdin, ...) != EOF)
                  {
                  ...
                  }

                  "Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman

                  "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups