CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jul 2003
    Posts
    6

    Reading input and parameter

    I would like to know how i can read a parameter and also an input while running the program.

    assume my program name is "readfile"

    I would like to make it in such a way that when i type "readfile parameter < filename", it will run the readfile program and in the program it will read the parameter and also the filename. How can i archieve this ? For your information i am coding this in C and not C++.

  2. #2
    Join Date
    Aug 2002
    Location
    Australia
    Posts
    46
    int main( int argc, char *argv)

    Do you know what argc and argv are?

  3. #3
    Join Date
    Feb 2004
    Posts
    21
    Well, use that:
    Code:
    int main(int argc, char *argv)
    argc shows you the number of entered parameters, and argv contains them.
    In your case the first parameter of argv contain your "parameter", second the "filename".

  4. #4
    Join Date
    Sep 2002
    Location
    Maryland - Fear The Turtle!
    Posts
    7,537
    Not quite everything P & d < denotes a redirection so it becomes stdin...

    do something like:

    HANDLE foo = GetStdHandle(STD_INPUT_HANDLE);
    then
    ReadFile(...) on the handle to the buffer in the file
    Last edited by Mick; March 7th, 2004 at 09:21 PM.

  5. #5
    Join Date
    Jul 2003
    Posts
    6
    Do you hav a sample code on how i can use

    HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE); ??

  6. #6
    Join Date
    Sep 2002
    Location
    Maryland - Fear The Turtle!
    Posts
    7,537
    Originally posted by winsonlee
    Do you hav a sample code on how i can use

    HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE); ??
    Code:
    #include <windows.h>
    #include <iostream>
    using namespace std;
    int main(int argc, char* argv[])
    {
      
        HANDLE  hStdIn = GetStdHandle(STD_INPUT_HANDLE);
        DWORD   dwNumRead = 0;
        BYTE    buffer[65535] = {0};
        while(ReadFile(hStdIn,&buffer,sizeof(buffer),&dwNumRead,NULL) != (dwNumRead == 0) )
        {
            cout << buffer;
            memset(&buffer,0,sizeof(buffer));
        }   
    	return 0;
    }
    /I should know better.
    Last edited by Mick; March 7th, 2004 at 10:39 PM.

  7. #7
    Join Date
    Jul 2003
    Posts
    6
    For your information the window.h is not found in the linux system. Is there anyway i can use the above method ???

  8. #8
    Join Date
    Sep 2002
    Location
    Maryland - Fear The Turtle!
    Posts
    7,537
    Originally posted by winsonlee
    For your information the window.h is not found in the linux system. Is there anyway i can use the above method ???
    That's why we have the non-visual c++ forum to post in, not the visual c++ forum. for linux, or even this you could probably use fopen and the stdinput handle, then fread...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured