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

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

问题描述

我必须做一个作业,我必须编写一个 C 程序,它从控制台获取输入文件名作为命令行参数.
它应该将数据从 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
字符串"列表(只要它们在 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
", argc);
    printf("0: %s
", argv[0]);
    if (1<argc)
    {
        printf("1: %s
", 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天全站免登陆