2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【C言語】_popenで別プログラムを実行する win7 32bit

Last updated at Posted at 2014-06-08
hello.c

# include <stdio.h>

int main (){

	printf("hello world 2014\n");

 	return 0;
}

001.c

# include <stdio.h>

int main (){

    int ret=0;//
	FILE *fp=NULL;
	char *cmdline="hello.exe";

	fp=_popen(cmdline,"w");

    if(NULL == fp)
	{
	 //_pclose(fp);←ここにpcloseを書くとダメ、書くとバグ扱いになる
	 printf("file open error ! \n");
	 return 1;
	}

	_pclose(fp);

 	return 0;
}

環境

OS:windows7 32bit (自作機)
コンパイラ:Borland C++ 5.5.1 for Win32

解説

_popenで別プログラムhello.exeを実行する。
_popenと_pcloseなのは、popenとpcloseではエラーが出るからである。

Error: 外部シンボル '_popen' が未解決(C:\2014\0601\003.OBJ が参照)
Error: 外部シンボル '_pclose' が未解決(C:\2014\0601\003.OBJ が参照)

コンパイルと実行結果

上記ソースをそれぞれ
C:\2014\0608\hello.c
C:\2014\0608\001.c
としてコンパイルして実行する。

c:\2014\0608>bcc32 001.c
Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
001.c:
警告 W8004 001.c 22: 'ret' に代入した値は使われていない(関数 main )
警告 W8004 001.c 7: 'fp' に代入した値は使われていない(関数 main )
Turbo Incremental Link 5.00 Copyright (c) 1997, 2000 Borland

c:\2014\0608>bcc32 hello.c
Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
hello.c:
Turbo Incremental Link 5.00 Copyright (c) 1997, 2000 Borland

c:\2014\0608>hello.exe
hello world 2014

c:\2014\0608>001.exe
hello world 2014

c:\2014\0608>

関連

【C言語】_popenでdirコマンドを実行する win7 32bit
http://qiita.com/kyoshiro-obj/items/b406f54d82786c2b190a

2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?