|
-
March 7th, 2004, 08:32 PM
#1
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++.
-
March 7th, 2004, 08:43 PM
#2
int main( int argc, char *argv)
Do you know what argc and argv are?
-
March 7th, 2004, 08:50 PM
#3
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".
-
March 7th, 2004, 09:15 PM
#4
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.
-
March 7th, 2004, 10:06 PM
#5
Do you hav a sample code on how i can use
HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE); ??
-
March 7th, 2004, 10:19 PM
#6
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.
-
March 7th, 2004, 11:47 PM
#7
For your information the window.h is not found in the linux system. Is there anyway i can use the above method ???
-
March 8th, 2004, 01:09 AM
#8
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|