Home > Software design >  How can i pass the content of file as an arguments to some function?
How can i pass the content of file as an arguments to some function?

Time:09-16

given file a.txt how can I pass the content of this file as arguments to the main function ?

for example:
a.txt:

a b c d e

and the main function is:

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

And I want to pass the content of this a.txt file as arguments to main function. Namely, the arguments will be: a b c d e.

How can I do it via Eclipse compiler ?

CodePudding user response:

Eclipse is not a compiler, that said you can pass the file as arguments with:

cat file.txt | xargs ./yourprogram

or

./yourprogram $(< file.txt)

CodePudding user response:

In Eclipse, you can configure parameters for running your code in the IDE... https://www.tutorialspoint.com/eclipse/eclipse_run_configuration.htm

But programatically you will need a script that reads the contents of the file and constructs the command-line that invokes main() with those file contents as command-line parameters.

Or for an elegant one-liner, see this answer by David Ranieri... https://stackoverflow.com/a/73736630/20006834

  • Related