Hello!,
I know this my be slightly unrelated question but it's very simple questions and I know many of you will find it very easy. I want to pass several txt files to my c code using make file, but I can't make it right, so please help me in this, here is what I had tried:

.PHONY: all
all: clean
	g++ -o run main.cpp -fopenmp 
	

.PHONY: clean
clean:
	rm -rf run

this is what I do to create an executable run, but I want to pass two txt files to the main function in main.cpp, I have tried a lot of things but I didn't succeed.
Thanks

Recommended Answers

All 6 Replies

I have a feeling you're getting 2 different concepts jumbled together here. Specifically, make files and Command Line Arguments (CLAs).

Are these files source code that you are trying to compile into finished executable code or are they text/data files that you want to pass the names of to your finished program as part of the command line so that your program can process them and the information contained within them?

I want to process the data in them, they are text files "images", and I want my code to process them

Okay. What you are trying to do is accomplished through the use of Command Line Arguments (CLAs), not during the compilation process.

To use CLAs, you need to add 2 arguments to your main(). The first is the argument count, which is commonly called "argc". The second is an array of C-Style strings, commonly called "argv", that contains the argument values.

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

Both arguments are automatically initialized by your operating system from the command line information when you execute the finished program so you don't need to worry about how to call the main() differently or how to get the information into them.

Once you have added those arguments, you can then access the "argv" argument like an array to process the values of the arguments:

#include <iostream>

using std::cout;
using std::endl;

int main (int argc, char *argv[]) {
  for (int i = 0; i < argc; ++i) {
    cout << "Argument " << i << " is: " << argv[i] << endl;
  }

  return 0;
}

With this code, this command line:

DaniWeb.exe blah1 blah2 foo bar baz

Should produce something like this:

Argument 0 is: DaniWeb.exe
Argument 1 is: blah1
Argument 2 is: blah2
Argument 3 is: foo
Argument 4 is: bar
Argument 5 is: baz
Press any key to continue . . .

you are right, I already have done that.
But the problem that I want to pass several files to the main, and I don't want to write the names of the files every time when running the excut., specially I will need to pass a lot of files, maybe 30 or 50 files. I generated the names of files to be processed by matlab and I want to pass the names to the main. I want to invoke a a file and make it do all the job.

Well then it sounds like you want to run a script, or some kind of batch file. If I were using bash as my shell, the file I made would look something like this:

#! /bin/bash

./run inputFile1 inputFile2  inputFile3
./run inputFile4

and so on, and then I would simply enter the name of this one file, and it would run the commands inside.

Why a script/batch file? Wouldn't that require several executions of the program? Why not just list the names of the files in a text file and call it something like "fileList.txt"? Then, open the file using the CLA and process it by reading through it as an inFile stream. That way, you have a single location to enter all the file names, you only need to enter the command line once, and the program only has to start up and end a single time.

Something like this:

#include <iostream>
#include <fstream>
#include <string>

void processListFile(char const * const);

int main (int argc, char *argv[]) {
  for (int i = 1; i < argc; ++i) {
    processListFile(argv[i]);
  }
  return 0;
} //end main()

void processListFile(char const * const fileName) {
  std::ifstream listFile(fileName, std::ios::in);
  if (!listFile.is_open()) {
    std::cout << "Error opening list file.";
    exit(EXIT_FAILURE);
  }
  std::string fileToRead;
  while (getline(listFile, fileToRead)) {
    std::ifstream currentImage(fileToRead.c_str(), std::ios::in);
    if (!currentImage.is_open()) {
      std::cout << "Error opening image file.";
      exit(EXIT_FAILURE);
    }
    //process the image file...
  }
} //end function processFile()

Then list several files in the list file:

image1.jpg
image2.jpg
image3.jpg
etc...
etc...

This would allow you to enter a command line such as:

processFiles.exe fileList.txt

Then your program would open the file "fileList.txt" and process the file names contained within the list.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.