Why does reading a file that doesn't exist print garbage characters? - c++

I try to understand the file reading in c++ and try to read a file that doesn't exist deliberately
//includes ommited
int main(int argc, char ** argv)
{
if(argc != 1)
throw std::exception();
std::ifstream file(argv[0]);
std::string content((std::istream_iterator<char>(file)), std::istream_iterator<char>());
std::cout << content.c_str() << std::endl;
}
DEMO
It prints the following:
ELF
Why is it supposed to mean? Do I just get UB by doing this? Since I'm a Java coder I expected that some exception will be thrown if we try to read a file that doesn't exist...

argv[0] contains path to your executable.
http://www.gnu.org/software/libc/manual/html_node/Program-Arguments.html
The file name of the program being run is also included in the vector as the first element; the value of argc counts this element.
Just try to print it's contents:
std::cout << argv[0] << std::endl;
You probably want to use argv[1].
"ELF" is the begin of file header of Executable and Linkable Format.

Related

How to take characters in a text file as arguments

I'm fairly new to C++, I'm trying to figure out how to take in arguments from a text file and use them in my program.
So if I wanted to include a text file whatever.txt in a command, it would use specified lines as arguments.
Like if the text file looked like this:
1 2
and I wanted to use 1 and 2 from it, as arguments in my program.
So far from what I've gathered, I need something akin to this:
int main (int argc, char const *argv[]) {
to start, but not sure where to go from here.
I'm trying to mess around with certain stuff, but I'm still pretty new and outside of loops and things I can't do much with the language yet!
Short of giving you the code that will do this for you (which would ruin the fun of learning how to do these things), the steps I would follow would be:
Take one argument from the command line as the file name you want to read the information from. Think of running your script like this:
$ ./myscript whatever.txt
Then, argv[0]="./myscript" and argv[1]="whatever.txt".
Open the file (perhaps ifstream would be good for this).
Loop through each line of the file (maybe using getline to put each line into a string)
Parse each line of the file (I've seen stringstream used for filling variables from a string).
Hopefully that will help you along your way.
int main(int argc, char *argv[])
after retrieving files names in the main function you should open each file and retrieve its contents
Sample code
int main(int argc, char* argv[])
{
for(int i=1; i <= argc; i++) // i=1, assuming files arguments are right after the executable
{
string fn = argv[i]; //filename
cout << fn;
fstream f;
f.open(fn);
//your logic here
f.close();
}
return 0;
}
https://stackoverflow.com/a/30141375/3323444
argc is the number of arguments passed in to your program, argv is an array of character strings holding the names of arguments passed in.
at least there's one parameter passed in which is argv[0] which is name of program.
after building this code run it form a command prompt as:
main.exe data.txt
or simply DRAG AND DROP file "data.txt" on your main.exe
main.exe is your program, data.txt is text file containing data you want to use
#include<iostream>
#include<fstream>
int main(int argc, char* argv[])
{
char c;
if(argc < 2)
{
std::cout << "No file passed to program!" << std::endl;
return 1;
}
std::ifstream in (argv[1] , std::ios::in);
if(!in)
std::cout << "Unable to read file: " << argv[1] << std::endl;
while(in >> c)
std::cout << c << std::endl;
in.close();
std::cin.get();
std::cout << std::endl;
return 0;
}

input file from command line

I'm here to trying to take input from the file. If user run this .exe from cmd and give a filename like example.exe input.txt
then it shows the file and read. But if user doesn't give the file name then it run as simply program's run.
Program is running well when I give the input from cmd during run time it run perfectly, but if I don't give the filename during running this file and run simply example.exe an exception show me the error
exception: invalid null pointer
my code is here:
// inputfile.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include<iostream>
#include<conio.h>
#include<string>
#include<fstream>
using namespace std;
void main(int argc, char* argv[])
{
try {
if (argc > 0)
{
string filename = argv[1];
ifstream in(filename);
in.open(filename);
if (in.is_open())
{
cout << "file opened, do something with file";
}
else
{
cout << endl << "You have Entered Wrong File Name Or File Not Exist in Project's Library" << endl;
}
}
}
catch (exception e)
{
}
cout << endl << "do with the simple program";
_getch();
}
The logic error is in the line
if (argc > 0)
It needs to be
if (argc > 1)
argv[1] is NULL if the program is invoked without arguments.
argc is at least 1, the first argument being the name of the program. When the program is invoked with one argument, argc is 2 and argv[1] is the first argument.
When there is only one argument(always the exec file itself, e.g. using in the way ./exec_file or just double click the exec file), the argv[1] would throw an exception.
Here are some tips:
Try to learn how to debug the code(IDE like Visual Studio, Ecplise or gdb).
VC++ is not standard c++, which means it only runs on Windows, not cross-platform. You'd better learn to use g++ compiler(linux).

File cannot be read in when using Visual Studio 2010 Debugger

So, I am using https://stackoverflow.com/a/298713/1472828 to put an argument "hands.txt" (my agrv[1], which is a file I wanna open) in my command arguments. I have tried both hands.txt and "hands.txt", neither of them worked.
int FileParsing(vector<Card> & v, char * FileName) {
ifstream ifs;
ifs.open(FileName);
if (!ifs.is_open()){
cout << "file cannot be opened." << endl;
} else {
So I use debugger to step through my main:
int main(int argc, char * argv[]){
if (argc !=2 ){
//ErrorMessage();
} else {
...
Debugger tells me that my argc is 2, which is right, but how come every time the debugger just goes to
cout << "file cannot be opened." << endl;
which means the argument just fails at reading it
ifstream ifs;
ifs.open(FileName);
Is there something I missed or I passed the argument in a wrong way?
p.s. The text file was read perfectly from cmd, so it's not the problem of code.
Got the idea from #WhozCraig, while running your program in cmd, the text file is put under debug directory. But if you run it using debugger, you have to put the text file in the same directory with other cpp and h files.

File Input in C++

I've been searching the internet for a while, but all I can find for file input in C++ is when you know the filename.
I'm trying to write a program to perform an addition of 2 numbers that are greater than 0 from a file, but without using scanf or cin. I want to load the file into memory, but all of the code I can find in regards to this situation requires knowledge of the filename. The file is formatted with 2 integers on a single line, separated by a space, and there are multiple lines of integers. The program will output the sum of the two numbers. I can easily do this with scanf, but if I were given a massive file, I would want to load it into memory (save mapping for later).
Loading the file into memory is giving me trouble, because I do not know the filename, nor how to find out, unless the user inputs the name of the file (not going to happen). I want the program to be executed like so, but using the most raw, and basic forms of C++ IO:
./myprog < boatloadofnumbers
How would I start my program to take the whole "boatloadofnumbers" as a file, so I can use more basic functions like read()? (also, what is the above method called? passing input?)
int main(){
int a,b;
while (scanf("%i,%i",&a,&b)>-1){
printf("%i\n",(a+b));
} //endwhile
return 0;
} //endmain
When the program is called as you state, then the content of boatloadofnumbers can be read from std::cin.
This method is called input redirection and is done by the shell, not your program.
Wiht input redirection the shell usually buffers the content of the file. That's a quite fast way to stream a file a single time through a computation.
It's not entirely clear how you're going to read a file when you don't know the filename. Presumably you don't know the filename at compile-time. That's okay, you can get this from the command-line at runtime, like this:
./myprog boatloadofnumbers
Then your filename is in argv[1] and you can access it using a std::ifstream.
If you're being given the input directly on stdin via redirection (such as ./myprog < boatloadofnumbers) you don't need a filename at all, you can just use std::cin.
The following main() will deal with both of these situations:
int main(int argc, char* argv[])
{
if (argc == 2)
{
std::cerr << "Reading from file" << argv[1] << std::endl;
std::ifstream ifs(argv[1]);
if (ifs)
{
sum_lines(ifs);
}
else
{
std::cerr << "Could not read from " << argv[1] << std::endl;
}
}
else
{
std::cerr << "Reading from stdin" << std::endl;
sum_lines(std::cin);
}
}
A sample sum_lines() may look a bit like this:
void sum_lines(std::istream& is)
{
int first = 0, second = 0;
std::string line = "";
while (std::getline(is, line))
{
std::istringstream iss(line);
if (is >> first >> second)
{
std::cout << first << " + " << second << " = " << first + second << std::endl;
}
else
{
std::cerr << "Could not parse [" << line << "]" << std::endl;
}
}
}
This doesn't care from where the input comes, so you can easily inject a std::istringstream for unit-testing. Also, this doesn't read the whole file into memory, just one line at a time, so it should deal with averybigboatloadofnumbers.
With shell redirection, your program can read from the standard input, which may be desirable. However, it may also be desirable to read from a file. It's easy to support both:
cat data > ./prog
./prog < data
./prog -f data
The first two are similar, and the contents of the file data are available from the program's standard input; the third line simply passes a command-line argument. Here's how we support this:
#include <cstdio>
#include <cstring>
void process_input(std::FILE * fp)
{
char buf[4];
std::fread(buf, 4, 1, fp);
// ...
}
int main(int argc, char * argv[])
{
std::FILE * fp = stdin; // already open!
if (argc >= 3 && 0 == std::strcmp(argv[1]. "-f"))
{
fp = std::fopen(argv[2], "rb");
if (!fp)
{
std::fprintf(stderr, "Could not open file %s.\n", argv[2]);
return 1;
}
}
process_input(fp);
if (fp != stdin) { std::fclose(fp); }
}
Equivalently, you can achieve something similar with iostreams, though it's a bit more roundabout to have a nice, universal reference:
#include <fstream>
int main()
{
std::ifstream ifp;
if ( /* as before */ )
{
ifp.open(argv[2], std::ios::binary);
if (!ifp) { /* error and die */ }
}
std::ifstream & infile = ifp ? ifp : std::cin;
process_input(infile);
}

how can I add error checking for empty string case

There is error checking for a missing file or incorrectly typed file name...but for no file_name i.e. the string file_name is empty I get the error
terminate called after throwing an instance of 'std::logic_error'
and then windows throw an error at me as well.
My guess would be to add a check for the empty string... at file_name.str() before creating an instance of type ifstream...but just wanted to check.
void file_to_string(string file_name)
{
string line;
ifstream myfile(file_name.c_str());
if (myfile.is_open())
{
while (myfile.good())
{
getline(myfile, line);
cout << line;
}
myfile.close();
}
else
{
cout << "File : " << file_name << " : did not open";
}
}
int main(int argc, char* argv[])
{
file_to_string(argv[1]);
}
If you don't provide an argument at all, then argv will only contain one string, and argv[1] will be invalid. You'll have to check that before you even try to access it:
int main(int argc, char* argv[])
{
if (argc != 2) {
std::cerr << "Usage: " << argv[0] << " filename\n";
return EXIT_FAILURE;
}
file_to_string(argv[1]);
}
There's no particular need to check whether the string is empty; if it is, then the file will fail to open as it would for any other invalid filename, and your code already handles that.
EDIT: ouch - more coffee required. As was clearly stated, this is a string object not a pointer. So, use the string's provided members to verify it has content. In practice, at the top of the file, use string.empty ala:
if (file_name.empty())
{
// bug out
cout << "Invalid filename" ;
return;
}
EDIT2: As Nim correctly points out, this is not bulletproof as there could be no params supplied (and hence your reference to argv[1] will crater). So, check these cases in main() or in another function to verify the params.
You could do something like:
if (argc < 2)
{
// complain about lack of proper arguments
}
else
{
file_to_string(argv[1]);
}
All that said, in a slightly-more involved case you may face the need to add multiple different command line arguments, or for the arguments to be supplied in an arbitrary order (and so you can't rely on the index of '1' like you are here).
Handling all that properly can get fairly complicated quickly, so take a look at the approaches others have already implemented to handle this (and more - what if your options come from a config file instead of the command line?). One such option is Boost's support for program options; see http://www.boost.org/doc/libs/1%5F39%5F0/doc/html/program%5Foptions.html
the error occurs higher than that - what if an argument (i.e. what is in argv[1] is crap!) is not provided? This is why you should look at proper program options utilities...