简体   繁体   English

使用命令行输出到文件

[英]output to a file using command line

I am writing a program that should be able to take command line parameters. 我正在编写一个程序,该程序应该能够接受命令行参数。 Basically, the user must be able to specify a file name through command prompt while calling the program. 基本上,用户在调用程序时必须能够通过命令提示符指定文件名。 ie the program should be able to take in a parameter like: doCalculation -myOutputfile.txt. 即程序应该能够接受一个参数,例如:doCalculation -myOutputfile.txt。 where doCalculation is the name of my program and myOutputfile is the file I want my results written to (ie output the results of my calculations to the specified file name). 其中doCalculation是我的程序的名称,而myOutputfile是我要将结果写入的文件(即,将计算结果输出到指定的文件名)。

So far I can call my function through the command prompt. 到目前为止,我可以通过命令提示符调用函数。 I am not sure how to get my program to write to the filename specified (or create this file if it does not exist already). 我不确定如何使程序写入指定的文件名(如果尚不存在,则创建此文件)。

My code is as follows: 我的代码如下:

int main(int argc, char *argv[])
{
    FILE* outputFile;
    char filename;

    // this is to make sure the code works
    int i = 0;
    for (i = 0; i < argc; i++) {
        printf("argv[%d] = %s\n", i, argv[i]);
    }

    //open the specified file
    filename= argv[i];   
    outputFile = fopen("filename", "r");

    //write to file
    fclose(outputFile);
}

So a couple things I noticed... 所以我注意到了几件事...

  1. If you want to write to the file, use "w" for write-mode instead of "r" for read-mode when opening the file. 如果要写入文件,请在打开文件时将“ w”用于写入模式,而不是“ r”用于读取模式。
  2. You declared filename as a single character rather than a pointer to a string (char *). 您将文件名声明为单个字符,而不是指向字符串的指针(字符*)。 Making it a pointer will allow filenames with a length > 1 (array of chars rather than a single char). 使其成为指针将允许文件名的长度> 1(字符数组而不是单个字符)。
  3. As Ashwin Mukhija mentioned, you're using i after the for loop set it to 2, when infact you want the 2nd (index 1) argument. 正如Ashwin Mukhija所提到的,在for循环将其设置为2之后,您正在使用i,实际上,您需要第二个(索引1)参数。
  4. You had the filename argument in the open() function as a literal "filename" rather than your filename variable. 在open()函数中,文件名自变量作为文字“文件名”而不是文件名变量。

See if this code helps solve your problem, (I also tossed a fprintf() in there to show you how you can write to the file). 看看这段代码是否有助于解决您的问题,(我还在其中扔了一个fprintf()来向您展示如何写入文件)。 Cheers! 干杯!

int main(int argc, char *argv[])
{
    FILE* outputFile;
    char* filename;

    // this is to make sure the code works
    int i = 0;
    for (i = 0; i < argc; i++) {
        printf("argv[%d] = %s\n", i, argv[i]);
    }

    //saftey check
    if(argv[1])
    {
        filename = argv[1];

        //open the specified file
        outputFile = fopen(filename, "w");

        fprintf(outputFile, "blah blah");

        //write to file
        fclose(outputFile );
    }

    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 输出到文件和命令行 - Output into file and command line 使用文件符号和命令行语句从文件中获取输入并将其保存在C中的另一个文件中。 无法获得输出 - Taking input from a file and saving it in another file, in C, using file notation and command line statement. Unable to get output 从命令行将输出重定向到C中的文本文件 - Redirecting output to a text file in C from the command line 如何更新命令行输出? - How to update command line output? 如何从输入文件中读取并保存每行的某些部分并将其输出到命令行? - How to read from an input file and save certain parts of each line and output it to the command line? 使用 gcc 命令行从 .c 文件构建 .so 文件 - Build .so file from .c file using gcc command line 使用 GCC 命令行从 .c 文件构建 .ko 文件 - Build .ko file from .c file using GCC command line 使用命令行参数在 C 中查找和替换文件中的单词 - Using command line arguments to find and replace a word in a file in C 使用命令行参数读取文本文件不起作用 - Read a text file using command line arguments doesn't work 如何使用文件,环境和/或命令行初始化C程序? - How to initialise a C program using file, environment and/or command line?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM