devc++ input from file does not work - c++

I'm trying to redirect a .txt content to .exe
program.exe < file.txt
and contents of file.txt are
35345345345
34543534562
23435635432
35683045342
69849593458
95238942394
28934928341
but the first index in array is the file path and the file contents is not displayed.
int main(int argc, char *args[])
{
for(int c = 0; c<argc; c++){
cout << "Param " << c << ": " << args[c] << "\n";
}
system("PAUSE");
return EXIT_SUCCESS;
}
Desired output:
Param0: 35345345345
Param1: 34543534562
Param2: 23435635432
Param3: 35683045342
Param4: 69849593458
Param5: 95238942394
Param6: 28934928341

The myapp < file.txt syntax passes to stdin (or cin if you prefer), not the arguments.

You have misunderstood what argc and argv are for. They contain the command line arguments to your program. If, for example, you ran:
program.exe something 123
The null terminated strings pointed to by argv will be program.exe, something, and 123.
You are attempting to redirect the contents of a file to program.exe using < file.txt. This is not a command line argument. It simply redirects the contents of the file to the standard input of your program. To get those contents you will need to extract from std::cin.

When you say "but the first index in array is the file path and the file contents is not displayed." it sounds like you're trying to read input from argv and argc. The angle bracket shell operator does not work that way. Instead, stdin (what cin and several C functions read from) has the contents of that file. So, to read from the file in the case above, you'd use cin.
If you instead really wanted to have a file automatically inserted into the argument list, I can't help you with the windows shell. However, if you have the option of using bash, the following will work:
program.exe `cat file.txt`
The backtick operator expands into the result of the command contained within, and so the contents are then passed as arguments to program.exe (again, under the bash shell and not the windows shell)

This code does what i was expecting to do with the other one. Thanks everybody who helped.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string line;
while (getline(cin, line))
cout << "line: " << line << '\n';
}

Related

C++ Convert filename input redirection to new output text file with .txt appended to name of new output file

I am trying to use input redirection to scan a file for regular expressions and output those regular expressions with the line number of the file to a new output text file. The output text file is to be the name of the file that was used for the input redirection with ".txt" appended to it. For instance if the program was run as follows:
./scanRegex < scanThisFile.log
Then the output file should be called
scanThisFile.log.txt
I created a simple program as follows ( minus the regex scanning to
isolate the issue ).
main.cpp
#include <iostream>
#include <ios>
#include <fstream>
#include <string>
#include <vector>
int main( int argc, char* argv[] )
{
std::string fileName = argv[1]; //<---===== ??????
std::string txt = ".txt\n";
char outputFile[100];
for( int i = 0; i < fileName.length(); ++i ){
outputFile[i] = fileName[i];
}
for( int i = fileName.length(); i < fileName.length() + 4; ++i ){
outputFile[i] = txt[i - fileName.length()];
}
std::ofstream outfile;
outfile.open(outputFile);
outfile << "It works!!";
outfile.close();
}
When I use
argv[ 0 ]
the program runs but the filename is wrong for what my intention is but is understandable because the program name is the first argument for argv:
a.txt
When I use
argv[ 1 ]
I get the following runtime error:
osboxes#osboxes:~/Desktop/ps7$ ./a < device1_intouch.log
terminate called after throwing an instance of 'std::logic_error'
what(): basic_string::_M_construct null not valid
Aborted (core dumped)
When I use
argv[2]
the program runs but the filename is wrong and full of gibberish (overflow?):
Maybe this is only part of where my problem is. Any help would be
greatly
appreciated.
Thank you.
You're confusing the standard input with your program's command line arguments. Command line arguments are the list of strings that you include on the command line when you invoke your program. For example:
$ ./myProgram arg1 arg2 ... argn
These are read via argv and argc, which respectively represent the "argument vector" and "argument count." By convention, the first argument is the working directory of your program. In this example, you would have:
argv == {"/path/to/myProgram", "arg1", "arg2", ..., "argn"}
argc == n
at the start of main. You must be careful not to read from argv out of bounds by checking argc, as with any raw array.
The standard input, on the other hand, is a stream of data that is supplied to your program throughout the call to main. This is read using std::cin.
int main(int argc, char** argv){
std::string s;
std::cin >> s; // read from standard input
}
When you run this program, it will block at the indicated line until it recieves data from the standard input. This data can be supplied by typing manually while the program is running from the command line:
$ ./myProgram
hello
or by input redirection:
$ echo "hello" | ./myProgram
$ ./myProgram < hello.txt
In the three examples above, s will contain the first word of text from the input, and you can use it for whatever you want on the following line.
Note that std::cin >> s will read text until it reaches the first white-space character. Fortunately, there are easy ways to read an entire line from stdin and to read everything from stdin

C++ input redirection causing error while file arguments work

When I specify an input file as a file argument, like so:
./asm ex1_in
everything works as it should!
However, when I specify an input file using redirection, like so:
./asm < ex1_in
the program throws the error it is supposed to throw when the input file is corrupt or doesn't exist.
This is the part of the code that handles input and file arguments:
int main(int argc, char * argv []) {
ifstream InFile(argv[1], ios::in);
if (!(InFile.is_open())) { /* check file */
fprintf (stderr, "The input file cannot be open or cannot be read. \n");
return 2;
}
if (InFile.is_open()) {
//some stuff
InFile.close();
}
//other stuff
}
I would like to be able to keep using ifstream, as it is very helpful for the rest of the program.
Also, the program is supposed to accept input directly on stdin if no file argument is supplied. Currently, if no file arguments are supplied, it throws the same error, instead of looking for user input.
EDIT: I added a line in main to try to see what's going on, and it looks like when input redirection is used, the program doesn't see argv[1] or anything after it.
cout << argv[0] << " " << argv[1] << " " << argv[2];
prints out only ./asm.
When input redirection isn't used, it prints out:
./asm ex1_in my1_out
You are trying to open argv[1] as a file every time you run the program.
In the first case, argv has a lenght of two (the executable name and the filename) so it opens and does what it needs to do normally, but when you run it redirecting the file to the stdin, argv just contains the executable name, and since argv[1] is a C-style string, it will read everything until it finds a null byte, then pass whatever it read to the std::ifstream constructor, failing each time.
You should use argc to know the number of arguments passed, and use either the file or stdin depending on the number of arguments.

How to print the actual name of the file to the console (C++)

I am using the terminal and C++. My program is opening a text file and doing various things with it. However, towards the very end I just want to print out the file name. How do I do that? I know that argc will look at a certain argument, but this still just looks at the number of the argument. I need the actual string name.
To print out the first argument (in your case the file.txt file name supplied as an argument) use the following code:
#include <iostream>
int main(int argc, char* argv[]){
if (argc > 1){
std::cout << argv[1];
}
else {
std::cout << "No arguments passed in.";
}
}
If you execute the program via: ./test file.txt the console output will be:
file.txt
Please note that argv[0] will print out the name and the (calling) path of your executable which in your case is ./test, not the name of your text file (argument).

std::getline partially reads first and last line and sets eof-bit

I need to read csv-files with C++: the first line of the file contains all column titles, the remaining lines contain floating point data (examples below, files have been shrunk down).
A few files have issues, I'm using the following code
#include <iostream>
#include <fstream>
#include <string>
// Compiled and testen on with Clang++ on Ubuntu 14.04
int main(int argc, char** argv) {
std::ifstream in;
in.open(argv[1]);
if(!in.is_open()) {
std::cerr << "Cannot open file: " << argv[1] << "\n";
return 1;
}
std::string buff;
std::getline(in, buff);
while(!in.eof()) {
std::cout << buff << "\n";
getline(in, buff);
}
in.close();
return 0;
}
For most files this runs okay, reading one line each iteration; example of a 'good' file:
Time,Smile,AU04,AU02,AU15,Trackerfail,AU18,AU09,negAU12,AU10,Expressive,Unilateral_LAU12,Unilateral_RAU12,AU14,Unilateral_LAU14,Unilateral_RAU14,AU05,AU17,AU26,Forward,Backward
0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,33.333,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,20.0
0.3,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,33.333,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0
1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,33.333,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,16.667,0.0
58.3,50.0,0.0,0.0,0.0,33.333,0.0,0.0,0.0,0.0,100.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0
62.4,33.333,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,100.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,20.0
Some files go crazy and set the eof-bit after the first getline. After this first read, buff contains part of the first line and part of the last line; example of a 'bad' file:
Time,Smile,AU04,AU02,AU15,Trackerfail,AU18,AU09,negAU12,AU10,Occlusion,Expressive,Unilateral_LAU12,Unilateral_RAU12,AU14,Unilateral_LAU14,Unilateral_RAU14,AU05,Au17,AU57,AU58
0,0,0,0,0,16.667,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
0.3,0,0,0,0,33.333,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
1.3,0,0,0,0,16.667,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
57.9,66.667,0,0,0,66.667,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
60.3,33.333,0,0,0,66.667,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
And the contents of buff after one call to getline:
Time,Smile,AU04,AU02,AU15,Trackerfail,AU18,AU09,negAU12,AU10,Occlusion,Expressive,Unilateral_LAU12,Unilateral_RAU12,AU14,Unilateral_LAU14,Unilateral_RA60.3,33.333,0,0,0,66.667,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
As you can see, the first line gets mixed with the last line. I can't figure out what's going wrong. Each line ends with a \n, the file ends with an empty \n.
I suppose my question is: why does getline skip to end-of-file while mixing the first and last line for some of the files while others work fine?
Edit: I need to convert a big dataset to a new, more consistent format. The current format is full of inconsistencies (using 0 and 0.0 or AU17 and Au17). Still, these formatting problems should not affect simply reading the file, right?
Edit2:
cat -v -e -t on a good file:
Time,Smile,AU04,AU02,AU15,Trackerfail,AU18,AU09,negAU12,AU10,Expressive,Unilateral_LAU12,Unilateral_RAU12,AU14,Unilateral_LAU14,AU05,AU17,AU26,Forward,Backward^M$
0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,66.667,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0^M$
0.2,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,33.333,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0^M$
etc...
cat -v -e -t on a bad file:
Time,Smile,AU04,AU02,AU15,Trackerfail,AU18,AU09,negAU12,AU10,Occlusion,Expressive,Unilateral_LAU12,Unilateral_RAU12,AU14,Unilateral_LAU14,Unilateral_RAU14,AU05,Au17,AU57,AU58^M0,0,0,0,0,16.667,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0^M0.3,0,0,0,0,33.333,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0^M1.3,0,0,0,0,16.667,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0^M1.4,0,0,0,0,33.333,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0^M1.8,0,0,0,0,50,0,0,0,0,0,0,0,0,0,0,0,0,0,25,0^M2.8,0,0,0,0,50,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0^M3,0,0,0,0,33.333,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0^M31,0,0,0,0,33.333,0,0,0,0,25,0,0,0,0,0,0,0,0,0,0^M31.1,0,0,0,0,50,0,0,0,0,50,0,0,0,0,0,0,0,0,0,0^M31.2,0,0,0,0,66.667,0,0,0,0,50,0,0,0,0,0,0,0,0,0,0^M31.4,0,0,33.333,0,66.667,0,0,0,0,50,0,0,0,0,0,0,0,0,0,0^M31.5,0,0,33.333,0,66.667,0,0,0,0,50,25,0,0,0,0,0,0,0,0,0^M32,0,0,33.333,0,66.667,0,0,0,0,50,25,0,0,0,0,0,0,0,0,25^M32.1,0,0,33.333,0,83.333,0,0,0,0,50,25,0,0,0,0,0,0,0,0,25^M32.2,0,0,33.333,0,83.333,0,0,0,0,25,25,0,0,0,0,0,0,0,0,25^M32.4,0,0,33.333,0,83.333,0,0,0,0,25,0,0,0,0,0,0,0,0,0,25^M32.7,0,0,33.333,0,83.333,0,0,0,0,0,0,0,0,0,0,0,0,0,0,25^M33,0,0,33.333,0,83.333,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0^M33.5,0,0,0,0,83.333,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0^M33.9,0,0,0,0,66.667,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0^M55,33.333,0,0,0,66.667,0,0,0,0,0,25,0,0,0,0,0,0,0,0,0^M55.2,66.667,0,0,0,66.667,0,0,0,0,0,25,0,0,0,0,0,0,0,0,0^M55.8,100,0,0,0,66.667,0,0,0,0,0,25,0,0,0,0,0,0,0,0,0^M56.8,100,0,0,0,66.667,0,0,0,0,0,25,0,0,0,0,0,0,0,0,25^M57.4,66.667,0,0,0,66.667,0,0,0,0,0,25,0,0,0,0,0,0,0,0,25^M57.8,66.667,0,0,0,66.667,0,0,0,0,0,25,0,0,0,0,0,0,0,0,0^M57.9,66.667,0,0,0,66.667,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0^M60.3,33.333,0,0,0,66.667,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
Seems like a big difference, how can I solve this?
It seems that the files are missing the newline character, and instead have only the carriage-return characters (which is equal to ^M or CTRLM).
You can fix it by using using cat with the file, and piping to tr to translate the carriage-return to a newline:
$ cat your-file | tr '\r' '\n' > your-file-fixed
After seeing your comment about the files coming from Max OS, I assume that it's the old pre-OSX versions, when the newline on Mac OS was just a single carriage-return.

getline() returns empty line in Eclipse but working properly in Dev C++

Here is my code:
#include <iostream>
#include <stdlib.h>
#include <fstream>
using namespace std;
int main() {
string line;
ifstream inputFile;
inputFile.open("input.txt");
do {
getline(inputFile, line);
cout << line << endl;
} while (line != "0");
return 0;
}
input.txt content:
5 9 2 9 3
8 2 8 2 1
0
In Enclipse, it goes to infinite-loop. I'm using MinGW 5.1.6 + Eclipse CDT.
I tried many things but I couldn't find the problem.
Since you are on windows try:
} while (line != "0\r");
The last line is stored as "0\r\n". The \n is used as the line delimiter by getline so the actual line read will be "0\r"
or
you can convert the dos format file to UNIX format using command
dos2unix input.txt
Now your original program should work. The command will change the \r\n at the end of the line to \n
Also you should always do error checking after you try to open a file, something like:
inputFile.open("input.txt");
if(! inputFile.is_open()) {
cerr<< "Error opening file";
exit(1);
}
It will create an infinite loop if no line contains exactly 0. For example 0\n is not the same thing as 0. My guess is that that is your problem.
EDIT: To elaborate, getline should be discarding the newline. Perhaps the newline encoding of your file wrong (i.e. windows vs. unix).
Your main problem is working directory.
Because you are specifying a file using a relative path it searches for the file from the current working directory. The working directory can be specified by your dev environment. (Note: The working directory is not necessarily the same directory where the executable lives (this is a common assumption among beginners but only holds in very special circumstances)).
Though you have a special end of input marker "0" you should also check that the getline() is not failing (as it could error out for other reasons (including beady formatted input). As such it is usually best to check the condition of the file as you read it.
int main()
{
string line;
ifstream inputFile;
inputFile.open("input.txt");
while((getline(inputfile, line)) && (line != "0"))
{
// loop only entered if getline() worked and line !="0"
// In the original an infinite loop is entered when bad input results in EOF being hit.
cout << line << endl;
}
if (inputfile)
{
cout << line << endl; // If you really really really want to print the "0"
// Personally I think doing anything with the termination
// sequence is a mistake but added here to satisfy comments.
}
return 0;
}