从命令行参数读取文件 [英] Read file from command line argument

查看:85
本文介绍了从命令行参数读取文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好

我正在使用C ++开发程序.
有什么方法可以实现使用argv [2]命令行参数在程序中读取的文件,该文件可以解码为放置在同一目录中的文件,然后将该tfile进一步传递给其他函数以进行播放.

任何指针都将真正有帮助

从OP发布的答案中移出:

你好.抱歉,不清楚.这是我的要求.


我在与代码相同的目录中有一个文件tfile.

我需要编写一个程序xyz.cpp

xyz.cpp的可执行文件应该是abc.

我希望每当我跑步时:

./abc功能参数tfile参数

tfile传递到函数"parameter-for-function"中,此后我可以在函数之间传递此文件以读取此文件.

Hi All

I am using C++ to develop a program.
Is there any way by which i can implement the file read in my program using argv[2] command line argument that can be decoded as a file placed in the same directory and then i passed this tfile further to other functions to play with it.

Any pointers would be really helpful

Moved from answer posted by OP:

Hi. Sorry for being unclear. Here are my requirements.


I have a file tfile in the same directory as of code.

i need to write a program xyz.cpp

that xyz.cpp ''s executable will be abc suppose.

I wish that whenever i run :

./abc parameter-for-function parameter-for-tfile

the tfile is passed into the function "parameter-for-function" and thereafter i can pass this file between functions to read this.

推荐答案

我仍然看不到任何问题您自己解释需要做的这篇文章.您只需要确认吗?您只需要分而治之"的技术即可.

1)首先,编写一个应用程序abc.exe,它接受两个参数,函数名argv[0],文件名argv[1].始终验证参数.首先,验证用户确实使用argc提供了两个参数,而不是验证函数和文件是否存在.如何验证一个功能是否存在并选择一个功能?恐怕要说很长的if语句.这是C ++,您的整个想法很糟糕,我必须说,仅此而已.在此应用程序中,读取文件.我希望您知道如何读取具有其名称的文件.使用相同的模式来编写其他一些应用程序,以用于参数,文件类型等.

2)编写应用程序xyz.exe,接受三个参数:argv[0]代表子应用程序可执行文件名称,argv[1]代表功能名称,argv[2]代表文件名称.再次验证所有参数,如上文所述.存在作为argv[0]传递的应用程序的附加验证.
3)运行以argv[0]传递的应用程序,传递另外两个参数作为子应用程序的参数. xyz.exe中的参数argv[1]argv[2]将成为子应用程序中参数argv[1]argv[2]的值.怎么运行呢?这取决于您的平台.例如,在Windows上,这将是对Windows API CreateProcess http://msdn.microsoft.com/en-us/library/ms682425%28VS.85%29.aspx [ http://goffconcepts.com/techarticles/development/cpp/createprocess.html [ ^ ].在其他平台上,您将总是找到一些API,例如fork(例如在Linux上),"OS"等.

这一切都清楚吗?也许我真的不明白什么会造成任何困难.

现在,我提到这种方法非常糟糕.为什么要用所有这些字符串值和验证来折磨自己,却要处理很多错误,甚至不提可能的错误?更好地使用DLL或共享库(本质上是相同的).

—SA
I still see no question in this post as you explain by yourself what you need to do. Do you just need confirmation? You just need the technique "Divide and Conquer".

1) First, write an application abc.exe accepting two parameters, argv[0] for function name, argv[1] for file name. Always validate parameters. First, validate that the user really supplied two parameters using argc, than validate that a function and the file exists. How to validate that a function exists and select one? I''m afraid to say, by some long if statement. This is C++, and your whole idea is pretty bad, I must say, that''s it. In this application, read the file. I hope you know how to read the file having its name. Write some other applications using the same schema for parameters, file type, etc.

2) Write the application xyz.exe accepting three parameters: argv[0] for child application executable name, argv[1] for function name, argv[2] for file name. Again, validate all parameters as I explained above. Additional validation is existing of the application passed as argv[0].

3) Run the application passed as argv[0], pass two other parameters as the parameters of child application. The parameters argv[1] and argv[2] in xyz.exe will become the values for parameters argv[1] and argv[2] in the child application. How to run it? It depends on your platform. For example, on Windows, this will be the call to Windows API CreateProcess, http://msdn.microsoft.com/en-us/library/ms682425%28VS.85%29.aspx[^]; this is a simple code sample: http://goffconcepts.com/techarticles/development/cpp/createprocess.html[^]. On other platforms, you will always find some API like fork (like on Linux), "OS", etc.

Is that all clear? Perhaps I really don''t understand what could cause any difficulties.

Now, I mentioned that this approach is pretty bad. Why torturing yourself with all that string values and validations and getting a lot of errors to handle, not even mentioning possible bugs? Better use DLLs or Shared Libraries (which is essentially the same).

—SA


检查此小程序

Check this small program

#include "stdafx.h"
#include "iostream.h"
#include <stdio.h>
#include"conio.h"
#include "string.h"

void FunctionOne(FILE * pFile)
{
    cout<<"Code for file operation";
}

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




const char* functionName ="FunctionOne";
const char* functionArg =(const char *)argv[1];

     if(strcmp(functionName,functionArg) == 0)
     {
         cout<<"InFun";
          FILE * pFile;
         if(argc<2)
         {
            pFile=0;

         }
         else
         {
            pFile = fopen(argv[2] , "r");
         }
            FunctionOne(pFile);
     }


    getch();

    return 0;
}





从命令提示符处调用它





Call it from command prompt

abc.exe FunctionOne "C:\Program Files\Microsoft Visual Studio\MyProjects\abc\Debug\tst.txt"


这篇关于从命令行参数读取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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