g++ and command line questions

Hi,

I am just wondering what the following would mean...

My c++ code is to be compilable with g++ and needs to be executed with the following command line code (within g++):

c++code < input.txt

where c++code is the c++ program and input.txt is the textfile.

How am I suppose to make my code executable with this(my c++ code), presenting I have:

int main(int argc, char **argv)
{
string fileName;
if (argc > 1)
fileName = argv[1];

objectName.function(fileName);

return 0;
}

Would this work?

Also I am wondering what the flag -f means ? We are to do this from inside the program and make it sort some data.

Any help you could provide would be great!

Thanks in advance!
c++code < input.txt
This will execute the c++code program and redirect stdin from input.txt, so instead of you entering the input the program will get it's input from the file input.txt. You can use std::cin to read the input.
Last edited on
First off, this code isn't going to do what you wanted:
1
2
3
4
5
6
7
8
9
10
int main(int argc, char **argv)
{
	string fileName;
	if(argc > 1)
		fileName = argv[1];

	objectName.function(fileName);

	return 0;
}

You will not get any command line arguments because your file will arrive through std::cin.
1
2
3
4
5
6
7
8
9
int main() // no need for parameters
{
	// file is read using std::cin
	
	std::string line;
	std::getline(std::cin, line); // read one line from input file (input.txt)

	return 0;
}

You can compile the code like this:
1
2
3
g++ -o myprogram myprogram.cpp // Linux

g++ -o myprogram.exe myprogram.cpp // Windows 

I don't know about the -f flag.
man gcc wrote:
Many options have long names starting with -f or with -W---for example, -fmove-loop-invariants, -Wformat and so on. Most of these have both positive and negative forms; the negative form of -ffoo would be -fno-foo.
Last edited on
Thanks for you help.

Is the -f flag something that is specified in the Makefile? The assignment specifies it must be executed from inside our program.

I can not find documentation online about the -f flag or the c++code < input.txt execution, can anyone direct me to a website?

Thanks again.
I can't understand what you mean about the -f flag. Can you post the exact wording from the assignment?
Topic archived. No new replies allowed.