char **argv[] segfaults when trying to get input from the command line - c++

I'm trying to do the most basic of things and am hitting a brick wall. I'm trying to read in a file name from the command line to use later in my program, but I can't even seem to extract the name from argv[]. Here's the code:
#include <iostream>
#include <string.h>
using namespace std;
int main(int argc, char **argv[]){
cout << "argc = " << argc << "\n\n";
cout << "Filename: " << argv[1] << "\n";
return 0;
}
I'm testing it on inputs that supply an argument of course, since there's no error checking. Here's what I get when I run the program:
./a.out testfilename
*
argc = 2
Filename: 0x7fff56e41d30
Now I understand argv[1] is a pointer to an array of chars, so this output makes sense. However, if I make the following change,
cout << "Filename: " << argv[1] << "\n";
to
cout << "Filename: " << *argv[1] << "\n";
in an attempt to dereference argv[1] to pull out the actual string, I get a segmentation fault..

int main(int argc, char *argv[]){
or
int main(int argc, char **argv){
but not
int main(int argc, char **argv[]){

That should be:
int main(int argc, char *argv[])
^ only one * here

Related

Taking a filename as an input while running a program I compiled in cpp

In cpp, I need to run a program like this
g++ *.cpp -o out
./out <input.txt> <somenumber>
where input.txt is a text file containing lines of information I need to proccess, and somenumber is an integer value I need to use.
I am searching for hours and couldn't find the answer I was looking for,
I found solutions that work like
./out < input.txt
reads the input.txt line as a string which then in the code I can process,
but the assignment says that the code will be run only and specifically as
./out <input.txt> <somenumber>
can anyone help ?
I have wrote some code, in which I wrote my main as
int main(int argc, char* argv[] ){
but when I run
./out <input.txt>
the terminal gives an error saying
" -bash: syntax error near unexpected token `newline' "
edit: typo
You are correct in wanting to accomplish this using argc and argv. Something like this should work
#include <iostream>
#include <string>
int main(int argc, char* argv[]) {
std::string file_name;
std::string number;
if(argc == 3) {
file_name = argv[1];
number = argv[2];
}
std::cout << "Filename: " << file_name << " number: " << number << "\n";
}
By convention, the use of < > to enclose an input parameter signifies that the parameter is mandatory for the command.
Therefore, the command
./out <input.txt> <some_number>
signifies that the two parameters - input.txt and some_number are mandatory.
The command can be run as:
./out input.txt 101
Here is an example of working code:
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char* argv[]) {
string inputFile;
string someNumber;
if(argc != 3) {
cout << "Sorry! Wrong input \n";
cout << "Usage: ./out <input_file_name> <some_number> \n";
return -1;
}
inputFile = argv[1];
someNumber = argv[2];
cout << "Processing ...\n";
cout << "File = " << inputFile << ", Number = " << someNumber << "\n";
return 0;
}
Output:
$ ./out input.txt 101
Processing ...
File = input.txt, Number = 101

debug assertion failed error nptr!=NULL

I get an Debug Assertion Error with expression: nptr!=NULL
my code:
#include <iostream>
using namespace std;
void main(int argc, char* argv[])
{
cout << "Hello Number " << atoi(argv[1]) << endl;
}
can somebody please help me solve this?
Most likely explanation is that you're not passing any parameters to your program, such as you would with the command runme 7.
The argv[argc] string is required to be NULL so this would explain why the assertion is happening.
Check that you have the correct number of parameters before trying to use them:
#include <iostream>
#include <cstdlib>
using namespace std;
int main(int argc, char* argv[]) {
if (argc != 2) {
cerr << "Usage: runme <integer argument>" << endl;
return 1;
}
cout << "Hello Number " << atoi(argv[1]) << endl;
}

Print a wchar_t emoji on console with wcout in C++ strange behavior

I am trying to print an emoji on standard output console in C++ on Mac OS X environment – clang.
This first code works correctly:
#include <iostream>
#include <cwchar>
int main(int argc, const char *argv[]){
char myEmoji[4] = "⛩";
std::cout << "emoji example: " << myEmoji << std::endl;
return 0;
}
and on console I can see:
./emoji ; exit
emoji example: ⛩
logout
When I try this, it works unexpectedly for me:
#include <iostream>
#include <cwchar>
int main(int argc, const char *argv[]){
wchar_t myEmoji = L'⛩';
std::wcout << "emoji example: " << myEmoji << std::endl;
return 0;
}
and this time I get:
./emoji ; exit
emoji example: logout
Where I am wrong?

Thread exception when compiling this basic C++ code in Xcode

Everytime I compile this C++ code I get a thread exception I can't understand. What is wrong here?
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char* argv[]) {
string arg = argv[1];
if (arg == "-r")
cout << "First arg is -r" << endl;
return 0;
}
You forgot to check argc>=2 before assigning argv[1] to the string arg.
Are you sure you are running this program with passing a parameter?
A possible correction:
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char* argv[])
{
if(argc<2)
{
cerr << "Not enough parameters" << endl;
abort();
}
string arg = argv[1];
if (arg == "-r")
cout << "First arg is -r" << endl;
return 0;
}

Enter File Name When Executing Program In C++

I'm learning C++, then i was searching for some codes for learn something in the area that i love: File I/O, but i want to know how i can tweak my code for the user type the file that he wants to see, like in wget, but with my program like this:
C:\> FileSize test.txt
The code of my program is here:
// obtaining file size
#include <iostream>
#include <fstream>
using namespace std;
int main () {
long begin,end;
ifstream myfile ("example.txt");
begin = myfile.tellg();
myfile.seekg (0, ios::end);
end = myfile.tellg();
myfile.close();
cout << "size is: " << (end-begin) << " bytes.\n";
return 0;
}
Thanks!
In the example below argv contains command line arguments as null terminated string array and argc contains an integer telling you how many arguments where passed.
#include <iostream>
#include <fstream>
using namespace std;
int main ( int argc, char** argv )
{
long begin,end;
if( argc < 2 )
{
cout << "No file was passed. Usage: myprog.exe filetotest.txt";
return 1;
}
ifstream myfile ( argv[1] );
begin = myfile.tellg();
myfile.seekg (0, ios::end);
end = myfile.tellg();
myfile.close();
cout << "size is: " << (end-begin) << " bytes.\n";
return 0;
}
main() takes parameters:
int main(int argc, char** argv) {
...
ifstream myfile (argv[1]);
...
}
You could also get clever, and loop for each file specified on the command line:
int main(int argc, char** argv) {
for (int file = 1; file < argc; file++) {
...
ifstream myfile (argv[file]);
...
}
}
Note that argv[0] is a string pointing to the name of your own program.
Main takes two arguments, which you can use to do this. See this:
Uni ref
MSDN reference (has VC specific commands