如何在C ++中从文件中读取和执行命令 [英] How to Read and Execute command from file in C++

查看:140
本文介绍了如何在C ++中从文件中读取和执行命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hi guys

我想在visual studio 2010中制作一个基本的C ++控制台应用程序。

i want make a basic C++ console app in visual studio 2010.

我想从文件执行cmd命令c ++ console app。

i want execute cmd command from file c++ console app.

来源:

// Nodehook.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "iostream"
#include "conio.h"
#include<fstream>
#include<iomanip>
using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
	string filename;
	ifstream inputfile;
	ofstream outfile("command.txt");

	
	if (argc>0)
	{
	ifstream inputfile (filename);
	inputfile.open(filename);


	system(filename);
	return 0;
}

命令在文件中:

ping -t 8.8.8.8

推荐答案

您可能希望在尝试之前练习编写简单的C ++程序。 您发布的代码有几个基本问​​题。

You might want to practice writing simple C++ programs before attempt this.  The code you posted has several fundamental problems.

标准标题应该用尖括号(<和>)括起来,而不是用引号括起来。

Standard headers should be enclosed in angle brackets (< and >), not in quotes.

您不使用conio.h或iomanip中的任何内容。 你为什么要包含它们?

You don't use anything from conio.h or iomanip.  Why did you include them?

你打开outfile但从不写它。

You open outfile but never write to it.

你声明输入文件两次。

您尝试使用名称为零字符(一个构造为空的字符串)打开文件。

You attempt to open a file with a name that has zero characters (a string that was constructed as empty).

您永远不会从输入文件中读取。

You never read from inputfile.

检查argc值的目的是什么?

What is the purpose of checking the value of argc?

函数system()接受实际命令,而不是包含命令的文件名。

The function system() takes an actual command, not the name of a file that contains the command.

你缺少至少一个右括号(})。

You are missing at least one closing brace (}).


这篇关于如何在C ++中从文件中读取和执行命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆