如何在 C++ 中创建一个在命令行上给出名称的文件?

发布于 2024-12-19 10:17:48 字数 87 浏览 0 评论 0原文

我正在做一些 C++ 教程,到目前为止我非常擅长。然而,有一件事情让我的知识获取困惑、脱轨,让我很头疼。

如何创建一个在命令行中指定名称的文件?

I'm doing some C++ tutorials, and so far I am pretty damn good at it. However, there is one thing that perplexed and forced off the rail my knowledge acquiring, which gives me a headache.

How to create a file with its name given on the command line?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月下客 2024-12-26 10:17:48

您问的是如何从命令行获取字符串来命名要打开的文件?

#include <iostream>
#include <cstdlib>
#include <fstream>

int main(int argc,char *argv[]) {
    if(2>argc) {
        std::cout << "you must enter a filename to write to\n";
        return EXIT_FAILURE;
    }
    std::ofstream fout(argv[1]); // open a file for output
    if(!fout) {
        std::cout << "error opening file \"" << argv[1] << "\"\n";
        return EXIT_FAILURE;
    }
    fout << "Hello, World!\n";
    if(!fout.good()) {
        std::cout << "error writing to the file\n";
        return EXIT_FAILURE;
    }
    return EXIT_SUCCESS;
}

You're asking about how to get a string from the command line to name the file to open?

#include <iostream>
#include <cstdlib>
#include <fstream>

int main(int argc,char *argv[]) {
    if(2>argc) {
        std::cout << "you must enter a filename to write to\n";
        return EXIT_FAILURE;
    }
    std::ofstream fout(argv[1]); // open a file for output
    if(!fout) {
        std::cout << "error opening file \"" << argv[1] << "\"\n";
        return EXIT_FAILURE;
    }
    fout << "Hello, World!\n";
    if(!fout.good()) {
        std::cout << "error writing to the file\n";
        return EXIT_FAILURE;
    }
    return EXIT_SUCCESS;
}
无畏 2024-12-26 10:17:48

您需要解析命令行参数并使用其中之一作为文件的文件名。请参阅此代码:

#include <stdio.h>

int main ( int argc, char *argv[] )
{
    if ( argc != 2 ) /* argc should be 2 for correct execution */
    {
        /* We print argv[0] assuming it is the program name */
        printf( "usage: %s filename", argv[0] );
    }
    else 
    {
        // We assume argv[1] is a filename to open
        FILE *file = fopen( argv[1], "r" );

        /* fopen returns 0, the NULL pointer, on failure */
        if ( file == 0 )
        {
            printf( "Could not open file\n" );
        }
        else 
        {
            int x;
            /* read one character at a time from file, stopping at EOF, which
               indicates the end of the file.  Note that the idiom of "assign
               to a variable, check the value" used below works because
               the assignment statement evaluates to the value assigned. */
            while  ( ( x = fgetc( file ) ) != EOF )
            {
                printf( "%c", x );
            }
            fclose( file );
        }
    }
}

请参阅此处了解更多详细信息: http://www.cprogramming.com/tutorial /c/lesson14.html

You need to parse the command line parameters and use one of them as the file name for your file. see this code:

#include <stdio.h>

int main ( int argc, char *argv[] )
{
    if ( argc != 2 ) /* argc should be 2 for correct execution */
    {
        /* We print argv[0] assuming it is the program name */
        printf( "usage: %s filename", argv[0] );
    }
    else 
    {
        // We assume argv[1] is a filename to open
        FILE *file = fopen( argv[1], "r" );

        /* fopen returns 0, the NULL pointer, on failure */
        if ( file == 0 )
        {
            printf( "Could not open file\n" );
        }
        else 
        {
            int x;
            /* read one character at a time from file, stopping at EOF, which
               indicates the end of the file.  Note that the idiom of "assign
               to a variable, check the value" used below works because
               the assignment statement evaluates to the value assigned. */
            while  ( ( x = fgetc( file ) ) != EOF )
            {
                printf( "%c", x );
            }
            fclose( file );
        }
    }
}

See here for more details: http://www.cprogramming.com/tutorial/c/lesson14.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文