亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

例如,我在代碼中寫了如下內容

例如,我在代碼中寫了如下內容

C++
富國滬深 2023-01-12 19:15:30
void main(){printf("你好");system("ping 127.0.0.1");printf("再見");}如何將“你好”和“再見”打到屏幕上,將ping的信息打到"ping.result"文件里?
查看完整描述

2 回答

?
慕容森

TA貢獻1853條經驗 獲得超18個贊

可以利用重定向實現system函數輸出到文件。
例如:

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;

public class Test {
publicstaticvoid main(String[] args) {

PrintStream printStream = null;
PrintStream sysout = System.out;
PrintStream syserr = System.err;
try {
File file = new File("c:\\systemout.log");

if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
//TODO Auto-generated catch block
e.printStackTrace();
}
}
printStream = new PrintStream(new FileOutputStream(new File(
"c:\\systemout.log"),true));

// set output to file instead of console
System.setOut(printStream);
System.setErr(printStream);
System.out.println("Before Redirect:System.out.println");
System.err.println("Before Redirect:System.err.println");
} catch (FileNotFoundException e) {
//TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (printStream !=null) {
printStream.close();
}
// Reset the output to console
System.setOut(sysout);
System.setErr(syserr);

System.out.println("After Redirect:System.out.println");
System.err.println("After Redirect:System.err.println");
}

}


查看完整回答
反對 回復 2023-01-15
?
不負相思意

TA貢獻1777條經驗 獲得超10個贊

最簡單的方法就是利用重定向實現


system("ping 127.0.0.1 >xxxx");//xxxx為要寫入的文件名,包括路徑


查看完整回答
反對 回復 2023-01-15
  • 2 回答
  • 0 關注
  • 108 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號