How to get std::string from command line arguments in win32 application? - c++

So now I have a
int main (int argc, char *argv[]){}
how to make it string based? will int main (int argc, std::string *argv[]) be enough?

You can't change main's signature, so this is your best bet:
#include <string>
#include <vector>
int main(int argc, char* argv[])
{
std::vector<std::string> params(argv, argv+argc);
// ...
return 0;
}

If you want to create a string out of the input parameters passed, you can also
add character pointers to create a string yourself
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char* argv[])
{
string passedValue;
for(int i = 1; i < argc; i++)
passedValue += argv[i];
// ...
return 0;
}

You can't do it that way, as the main function is declared explicitly as it is as an entry point. Note that the CRT knows nothing about STL so would barf anyway. Try:
#include <string>
#include <vector>
int main(int argc, char* argv[])
{
std::vector<std::string> args;
for(int i(0); i < argc; ++i)
args.push_back(argv[i]);
// ...
return(0);
}; // eo main

That would be non-standard because the Standard in 3.6.1 says
An implementation shall not predefine the main function. This function shall not be overloaded. It shall have a return type of type int, but otherwise its type is implementation-defined. All implementations shall allow both of the following definitions of main:
int main() { /* ... */ }
and
int main(int argc, char* argv[]) { /* ... */ }

No. That is not allowed. If present, it is mandated to be char *argv[].
BTW, in C++ main should always be declared to return 'int'

main receives char*. you will have to put the argv array into std::strings yourself.

Related

Can I create fstream before main?

#include <string.h>
#include <fstream>
using namespace std;
ofstream fout(argv[1]);
main function
int main(int argc, const char * argv[]) {
//I try to add "new word" fstream file
fout<<"new words"<<endl;
}
How can I do this? The bottom one didn't work either. It is necessery. It must be before main function
#include <string.h>
#include <fstream>
using namespace std;
string argument;
ofstream fout(argument]);
main function
int main(int argc, const char * argv[]) {
argument=argv[1]
fout<<"new words"<<endl;
}
You cannot initialize your fout before you know what argv is. So, in some sense, impossible. However, you can initialize it with nothing, and open it later when you get your argv:
ofstream fout;
int main(int argc, const char * argv[])
{
fout.open(argv[1]);
fout<<"new words"<<endl;
}
This way, the global fout is accessible to all functions defined after it. However, there is a short period, before opening it in main, when fout is invalid. This is the best you can do, from a logical point of view.

Why doesn't the overload function of [] function here?

#include "stdafx.h"
#include <iostream>
using namespace std;
#include <string.h>
class Array
{
public:
int Length;
char *Arrp;
Array(char *str)
{
Length=strlen(str);
Arrp=str;
}
char & operator[](int index);
};
char & Array::operator[](int index)
{
if(index>=Length||index<0)
{
cout<<"Index "<<index<<" error."<<endl;
return Arrp[0];
}
return Arrp[index];
}
int _tmain(int argc, _TCHAR* argv[])
{
Array a("Good");
cout<<a.Arrp[6]<<endl;
return 0;
}
It doesn't get into the overload function of [] at all. I compare it with lots of examples but what I write never functions.
How can I proceed further?
You're not calling the overloaded function but are directly reading the array itself.
Try:
cout<<a[6]<<endl;

error: C++ requires a type specifier for all declarations?

#include <cstdio>
#include <iostream>
#include <fstream>
#define INPUT_FILE
#ifdef INPUT_FILE
freopen("test.txt", "r", stdin);
#endif
using namespace std;
int main(int argc, char const *argv[])
{
int n;
while(scanf("%d", &n))
printf("%d\n", n);
return 0;
}
I'm trying to pass input to the program via an input file but, the following error pops up,
error: C++ requires a type specifier for all declarations
freopen("test.txt", "r", stdin);
^~~~~~~
1 error generated.
You can't use a function outside of a function or any other executable part of the program.
Your program is equivalent to
#include <cstdio>
#include <iostream>
#include <fstream>
#define INPUT_FILE
freopen("test.txt", "r", stdin); // Makes no sense
using namespace std;
int main(int argc, char const *argv[])
{
int n;
while(scanf("%d", &n))
printf("%d\n", n);
return 0;
}

How to refer to the pointer **environ in a user-defined namespace's member function?

#include <iostream>
namespace A {
void func();
}
void A::func()
{
extern char **environ;
std::cout << environ[0] << std::endl;
}
int main()
{
A::func();
return 0;
}
Like the code above, I just want to use the system-defined pointer **environ in A::func(), but g++ always says:
undefined reference to `A::environ'
How can I use the system-defined variable environ correctly?
Add
#include <unistd.h>
and environ must be in global scope.
So the code would look like this:
#include <iostream>
#include <unistd.h>
extern char **environ;
namespace A {
void func();
}
void A::func()
{
std::cout << environ[0] << std::endl;
}
int main()
{
A::func();
return 0;
}
You just need to declare environ at global scope, which is of course where it is defined, rather than within your function.
If you want to be a little cleaner about it and avoid unnecessary use of global variables, you can declare main this way:
int main(int argc, char* argv[], char* envp[])
Then simply pass envp to your function. It will work the same as the global.

c++ identifier not found when compiling source

Here is my code:
#include "stdafx.h"
#include <iostream>
#include <string>
#include <sstream>
#include <math.h>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int userInput = -9999;
userInput = ReadNumber();
WriteAnswer(userInput);
system("pause");
return 0;
};
int ReadNumber ()
{
int liInput = -9999;
cin >> liInput;
return liInput;
};
void WriteAnswer(int data)
{
cout << data << endl;
};
When I try to compile, it saids :
1>error C3861: 'ReadNumber': identifier not found
1>error C3861: 'WriteAnswer': identifier not found
Why did the above error occurs? and how to solve this problem?
Thanks
C++ sources are compiled from beginning to end.
When the compiler has gotten this far:
#include "stdafx.h"
#include <iostream>
#include <string>
#include <sstream>
#include <math.h>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int userInput = -9999;
userInput = ReadNumber(); // <-- What is this?
It's true -- there is no evidence of ReadNumber existing.
Declare the existence of your functions before they are used.
int ReadNumber ();
void WriteAnswer(int data);
You forgot to type function prototypes.
int ReadNumber ( void );
void WriteAnswer(int );
Put them in your code before calling the functions.
In your code you try to call ReadNumber function, that hasn't been declared yet. The compiler doesn't know anything about this function:
int _tmain(int argc, _TCHAR* argv[])
{
...
ReadNumber(); // call to function ReadNumber, but what is ReadNumber ???
}
// definition of ReadNumber:
int ReadNumber ()
{
...
}
You should declare it first:
// declaration:
int ReadNumber();
int _tmain(int argc, _TCHAR* argv[])
{
...
ReadNumber(); // call ReadNumber that takes no arguments and returns int
}
// definition of ReadNumber:
int ReadNumber ()
{
...
}
You have to write a function prototype or function itself before first call of the function.
In your code compiler see call of the ReadNumber() but it doesn't know what is that function.