编写C语言程序输出程序命令行参数
时间: 2023-07-16 07:14:20 浏览: 115
以下是一个简单的 C 语言程序,用于输出程序命令行参数:
```c
#include <stdio.h>
int main(int argc, char *argv[]) {
int i;
printf("Number of arguments: %d\n", argc);
for (i = 0; i < argc; i++) {
printf("Argument %d: %s\n", i, argv[i]);
}
return 0;
}
```
在这个程序中,`argc` 表示命令行参数数量,`argv` 是一个指向字符串数组的指针,其中每个字符串都是一个命令行参数。程序遍历所有参数并将它们打印出来。
相关问题
在TC2.0环境下编写程序输出程序的命令行参数
在TC2.0环境下,可以使用C语言编写程序输出命令行参数。以下是示例代码:
```c
#include <stdio.h>
int main(int argc, char *argv[]) {
int i;
printf("命令行参数个数:%d\n", argc);
for (i = 0; i < argc; i++) {
printf("参数%d:%s\n", i+1, argv[i]);
}
return 0;
}
```
在命令行中运行编译好的可执行文件时,可以在命令行中添加参数,例如:
```
myprog.exe arg1 arg2 arg3
```
程序会输出:
```
命令行参数个数:4
参数1:myprog.exe
参数2:arg1
参数3:arg2
参数4:arg3
```
其中,`argc` 表示命令行参数的个数,`argv` 是一个字符串数组,存储了每个参数的字符串。在循环中遍历 `argv` 数组,即可逐个输出参数。
编写一个c语言程序,可以在命令行输入参数,完成指定文件的缩放,并存储到新文件,命令行参数如下 zoom file1.bmp 200 file2.bmp 第一个参数为可执行程序名称,第二个参数为原始图像文件名,第三个参数为缩放比例(百分比),第四个参数为新文件名
以下是一个简单的 C 语言程序,可以实现您所描述的功能:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <ctype.h>
#include <math.h>
#include <malloc.h>
#include <windows.h>
#include <wingdi.h>
#pragma comment(lib, "gdi32.lib")
int main(int argc, char *argv[])
{
if (argc != 4)
{
printf("Usage: zoom source_file scale_percent target_file\n");
return -1;
}
char *source_file = argv[1];
int scale_percent = atoi(argv[2]);
char *target_file = argv[3];
HBITMAP hBitmap = (HBITMAP)LoadImageA(NULL, source_file, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
if (hBitmap == NULL)
{
printf("%s not found\n", source_file);
return -1;
}
BITMAP bm;
GetObject(hBitmap, sizeof(bm), &bm);
int new_width = (int)(bm.bmWidth * scale_percent / 100.0);
int new_height = (int)(bm.bmHeight * scale_percent / 100.0);
HDC hdcScreen = GetDC(NULL);
HDC hdcSrc = CreateCompatibleDC(hdcScreen);
HDC hdcDst = CreateCompatibleDC(hdcScreen);
HBITMAP hBitmapSrc = CreateCompatibleBitmap(hdcScreen, bm.bmWidth, bm.bmHeight);
HBITMAP hBitmapDst = CreateCompatibleBitmap(hdcScreen, new_width, new_height);
SelectObject(hdcSrc, hBitmap);
SelectObject(hdcDst, hBitmapDst);
BitBlt(hdcSrc, 0, 0, bm.bmWidth, bm.bmHeight, hdcSrc, 0, 0, SRCCOPY);
SetStretchBltMode(hdcDst, COLORONCOLOR);
StretchBlt(hdcDst, 0, 0, new_width, new_height, hdcSrc, 0, 0, bm.bmWidth, bm.bmHeight, SRCCOPY);
DeleteDC(hdcSrc);
DeleteObject(hBitmapSrc);
ReleaseDC(NULL, hdcScreen);
SaveBitmap(hBitmapDst, target_file);
DeleteDC(hdcDst);
DeleteObject(hBitmapDst);
return 0;
}
```
您可以将以上代码保存为zoom.c,并在命令行中运行以下命令来编译程序:
```
gcc zoom.c -o zoom.exe -lgdi32
```
然后,您就可以在命令行中输入以下命令来运行程序:
```
zoom file1.bmp 200 file2.bmp
```
其中,file1.bmp 是原始图像文件名,200 表示缩放比例为200%(即放大两倍),file2.bmp 是新文件名。程序会自动读取原始图像,按照指定比例缩放,并将结果保存为新文件。如果原始图像文件不存在,则会输出错误提示信息。
阅读全文
相关推荐















