从终端读取input.txt文件和output.bmp文件(C编程) [英] Read input.txt file and also output.bmp file from terminal (C-programming)

查看:185
本文介绍了从终端读取input.txt文件和output.bmp文件(C编程)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须做一个作业,其中我必须编写一个C-Programm,并在其中从控制台获取输入文件名作为命令行参数。

它应该从input.txt文件(输入文件包含bmp文件的信息-颜色等)到生成的output.png文件。 20 20个参数代表output.png图像的宽度和高度。

I have to do an assignment where I have to write a C-Programm, where it gets the input-file-name from the console as command line parameter.
It should move the data from the input.txt file (the input file has the information for the bmp file - color etc.) to the generated output.png file. The 20 20 parameters stand for width and height for the output.png image.

因此,例如控制台请求(在Linux上测试)将如下所示:

So the console-request for example (tested on Linux) will look like this:

./main input.txt output.bmp 20 20

我知道这段代码读取了input.txt文件并将其显示在屏幕上。

I know that this code reads an input.txt File and puts it on the screen.

FILE *input;
int ch;
input = fopen("input.txt","r");
ch = fgetc(input);
while(!feof(input)) {
    putchar(ch);
    ch = fgetc(input);
}
fclose(input);

例如,这会将其写入output.png文件。

And this would (for example) write it to the output.png file.

FILE *output;
int i;
     output = fopen("ass2_everyinformationin.bmp", "wb+"); 
 for( i = 0; i < 55; i++)               
 {
     fputc(rectangle_bmp[i], output);
 }
 fclose(output);

但是此代码仅在我直接在代码中硬编码名称而不通过使用的情况下有效一个命令行参数。

我没有任何线索,如何实现它,我也没有在互联网上找到任何有用的信息,也许有人可以帮助我。

But this code works only, if I hard-code the name directly in the code, not by using a command line parameters.
I don't have any clue, how to implement that and I also didn't find any helpful information in the internet, maybe someone can help me.

问候

推荐答案

标准main()的完整原型为

The full prototype for a standard main() is

int main(int argc, char* argv[]);

您会得到一个带有参数数量的整数, argc

a字符串列表(只要它们存在于C中), argv

You get an int with the number of arguments, argc and
a list of "strings" (as far as they exist in C), argv.

例如,可以使用

#include "stdio.h"
int main(int argc, char* argv[])
{

    printf("Number: %d\n", argc);
    printf("0: %s\n", argv[0]);
    if (1<argc)
    {
        printf("1: %s\n", argv[1]);
    }
}

开始使用参数。

请注意,这只是执行命令行参数的基本示例,因此没有实现任何目的。这与赋予竞争的StackOverflow策略相匹配,该策略为分配任务提供帮助,而又不会解决所有问题。

Note that this is intentionally not implementing anything but a basic example of using command line parameters. This matches an accpeted StackOverflow policy of providing help with assignments, without going anywhere near solving them.

这篇关于从终端读取input.txt文件和output.bmp文件(C编程)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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