64,676
社区成员
发帖
与我相关
我的任务
分享
#include<stdio.h>
#include <windows.h>
void main() {
char szOutputPath[MAX_PATH + 1] = "c:\\123.txt";
freopen(szOutputPath, "w", stdout); //重定向
freopen("con", "w", stdout); //恢复重定向
system("cls"); //执行这条语句后,下一条输出的是乱码,否则正常,注释后正常
printf("11\n22\n33\n");
getchar();
}
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
pid_t rw_popen(char* cmd, FILE **rfile, FILE **wfile) {
int pipefd[2],pipefd2[2]; //管道描述符
pid_t pid; //进程描述符
if (pipe(pipefd) < 0) //建立管道
{
printf("rw_popen() pipe create error/n");
return 0;
}
if (pipe(pipefd2) < 0) //建立管道
{
printf("rw_popen() pipe create error/n");
return 0;
}
pid = fork(); //建立子进程
if (pid < 0)
return 0;
if (0 == pid) //子进程中
{
close(pipefd[0]);
dup2(pipefd[1], 1);
close(pipefd[1]);
dup2(pipefd2[0], 0);
close(pipefd2[0]);
close(pipefd[1]);
char *argv[] = { "/bin/sh", "-c", cmd, NULL };
if (execvp("/bin/sh", argv) < 0) //用exec族函数执行命令
exit(1);
}
close(pipefd[1]);
*rfile = fdopen(pipefd[0], "r");
close(pipefd2[0]);
*wfile = fdopen(pipefd2[1], "w");
return pid;
}
void rw_pclose(pid_t pid, FILE *rfile, FILE *wfile) {
int status;
waitpid(pid, &status, 0);
fclose(rfile);
fclose(wfile);
}
int main() {
char buf1[1024];
FILE *file1, *file2;
pid_t pid;
pid = rw_popen("sh", &file1, &file2);
if (pid) {
fputs("pwd;exit;\n",file2);
fflush(file2);
if (fgets(buf1, 1400, file1)) {
puts(buf1);
}
rw_pclose(pid, file1, file2);
}
return 1;
}
#pragma comment(lib,"user32")
#include <stdio.h>
#include <windows.h>
int main() {
SECURITY_ATTRIBUTES sa = {0};
STARTUPINFO si = {0};
PROCESS_INFORMATION pi = {0};
HANDLE hPipeOutputRead = NULL;
HANDLE hPipeOutputWrite = NULL;
HANDLE hPipeInputRead = NULL;
HANDLE hPipeInputWrite = NULL;
BOOL bTest = 0;
DWORD dwNumberOfBytesRead = 0;
DWORD dwNumberOfBytesWrite = 0;
CHAR szMsg[100];
CHAR szBuffer[256];
sa.nLength = sizeof(sa);
sa.bInheritHandle = TRUE;
sa.lpSecurityDescriptor = NULL;
// Create pipe for standard output redirection.
CreatePipe(&hPipeOutputRead, // read handle
&hPipeOutputWrite, // write handle
&sa, // security attributes
0 // number of bytes reserved for pipe - 0 default
);
// Create pipe for standard input redirection.
CreatePipe(&hPipeInputRead, // read handle
&hPipeInputWrite, // write handle
&sa, // security attributes
0 // number of bytes reserved for pipe - 0 default
);
// Make child process use hPipeOutputWrite as standard out,
// and make sure it does not show on screen.
si.cb = sizeof(si);
si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
si.wShowWindow = SW_HIDE;
si.hStdInput = hPipeInputRead;
si.hStdOutput = hPipeOutputWrite;
si.hStdError = hPipeOutputWrite;
CreateProcess (
NULL, "cmd.exe",
NULL, NULL,
TRUE, 0,
NULL, NULL,
&si, &pi);
// Now that handles have been inherited, close it to be safe.
// You don't want to read or write to them accidentally.
CloseHandle(hPipeOutputWrite);
CloseHandle(hPipeInputRead);
// Now test to capture DOS application output by reading
// hPipeOutputRead. Could also write to DOS application
// standard input by writing to hPipeInputWrite.
sprintf(szMsg, "dir *.txt /b\nexit\n");
WriteFile(
hPipeInputWrite, // handle of the write end of our pipe
&szMsg, // address of buffer that send data
18, // number of bytes to write
&dwNumberOfBytesWrite,// address of number of bytes read
NULL // non-overlapped.
);
while(TRUE)
{
bTest=ReadFile(
hPipeOutputRead, // handle of the read end of our pipe
&szBuffer, // address of buffer that receives data
256, // number of bytes to read
&dwNumberOfBytesRead, // address of number of bytes read
NULL // non-overlapped.
);
if (!bTest){
sprintf(szMsg, "Error #%d reading pipe.",GetLastError());
MessageBox(NULL, szMsg, "WinPipe", MB_OK);
break;
}
// do something with data.
szBuffer[dwNumberOfBytesRead] = 0; // null terminate
MessageBox(NULL, szBuffer, "WinPipe", MB_OK);
}
// Wait for CONSPAWN to finish.
WaitForSingleObject (pi.hProcess, INFINITE);
// Close all remaining handles
CloseHandle (pi.hProcess);
CloseHandle (hPipeOutputRead);
CloseHandle (hPipeInputWrite);
return 0;
}
#include <windows.h>
//This will clear the console.
void ClearConsole()
{
//Get the handle to the current output buffer...
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
//This is used to reset the carat/cursor to the top left.
COORD coord = {0, 0};
//A return value... indicating how many chars were written
// not used but we need to capture this since it will be
// written anyway (passing NULL causes an access violation).
DWORD count;
//This is a structure containing all of the console info
// it is used here to find the size of the console.
CONSOLE_SCREEN_BUFFER_INFO csbi;
//Here we will set the current color
if(GetConsoleScreenBufferInfo(hStdOut, &csbi))
{
//This fills the buffer with a given character (in this case 32=space).
FillConsoleOutputCharacter(hStdOut, (TCHAR) 32, csbi.dwSize.X * csbi.dwSize.Y, coord, &count);
FillConsoleOutputAttribute(hStdOut, csbi.wAttributes, csbi.dwSize.X * csbi.dwSize.Y, coord, &count);
//This will set our cursor position for the next print statement.
SetConsoleCursorPosition(hStdOut, coord);
}
}
#include<stdio.h>
int main() {
printf("11\n22\n33\n");
return 0;
}
在cmd窗口中,输入以下命令将程序的输出重定向到文件c:\123.txt中:
cd /d 你的.exe程序所在盘符和文件夹
你的.exe程序 >c:\123.txt