Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

EDIT: 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.
Posted
Updated 28-Sep-11 20:59pm
v3
Comments
AspDotNetDev 20-Sep-11 19:03pm    
I'm confused. What's your question? What are you actually having a problem doing?
AspDotNetDev 20-Sep-11 19:21pm    
I deleted your answer and moved the text into your question (you can update your question rather than post text as an answer, or you can reply directly to a comment such as this one). I still don't see what you are having trouble with. I see that you have described functionality, but there are several steps to implement it and I'm not sure which step you are having trouble with.
AspDotNetDev 20-Sep-11 19:42pm    
Is it reading the command line argument that you are having trouble with? Or are you having trouble opening a file based on the path? Regarding your second item, are you unsure of how to pass parameters to functions?
AspDotNetDev 20-Sep-11 21:29pm    
C++ file I/O: http://www.cplusplus.com/doc/tutorial/files/

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
 
Share this answer
 
v4
Comments
Emilio Garavaglia 22-Sep-11 3:38am    
"I'm afraid to say, by some long if statement."
Not to mention a map between strings an functors! ;-)
Sergey Alexandrovich Kryukov 22-Sep-11 21:36pm    
Thank you for this note.
Yes of course, but as I say, "why torturing yourself?". The initial idea confines it all to messing with ugly strings and code which is not confirmed by the compiler, instead of... software development.
--SA
Check this small program

C#
#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"
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900