文件重定向

文件重定向

main1





#define   _CRT_SECURE_NO_WARNINGS//关闭安全检查
#include<stdio.h>
#include<stdlib.h>

void main1()
{
	char str[100] = { 0 };
	scanf("%s", str);
	printf("str=%s\n", str);
	system(str);




}

void main2()
{



	char str[100] = { 0 };
	fscanf(stdin,"%s", str);
	fprintf(stdout,"str=%s\n", str);
	system(str);


}

//打开5次计算器 5 calc
void  main3()
{
	char *path = "C:\\Users\\yincheng01\\Desktop\\newcmd.txt";
	
	int num=0;
	char docmd[30] = { 0 };
	scanf("%d%s", &num, docmd);//接受键盘输入
	FILE *pf;
	pf = fopen(path, "w");//写入
	if (pf == NULL)
	{
		printf("文件打开失败");
	}
	else
	{
		fprintf(pf, "for /l %%i in (1,1,%d) do %s", num, docmd);
		fclose(pf);
	}

    system("pause");
}

void main4()
{

	char cmd[100] = { 0 };
	int num = 0;
	char docmd[30] = { 0 };
	char *path = "C:\\Users\\yincheng01\\Desktop\\newcmd.txt";
	FILE *pf = fopen(path, "r");//读取
	if (pf == NULL)
	{
		printf("文件打开失败");
		return;
	}
	else
	{
		fscanf(pf, "for /l %%i in (1,1,%d) do %s", &num, docmd);
		printf("num=%d,docmd=%s", num, docmd);
	}


	system("pause");
}

void main()
{
	int num ;
	scanf("num=%d", &num);//必须精确对应
	printf("num=%d", num);
	system("pause");
}
注:
fprintf(pf, "for /l %%i in (1,1,%d) do %s", num, docmd);
这里是用两个百分号代表一个百分号

你可能感兴趣的:(C++)