I am new to C++ and I am trying to write a program to take in command line arguments and produce a .desktop file. I am trying to implement identification of the argv values but I keep getting a std::logic_error
My code is:
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string>
using namespace std;
int main(int argc, char* argv[]) {
string name;
string comment;
for(int i = 1; i <= argc; i++) {
char* tmp[] = {argv[i]};
string param = *tmp;
string paramVal = argv[i+1];
if(param == "-h") {
cout << "-h Display this help dialogue" << endl;
cout << "-n Set entry name" << endl;
cout << "-c Set entry comment" << endl;
cout << "-e Set entry executable path" << endl;
cout << "-i Set entry icon" << endl;
break;
}
else if(param == "-n") {
name = paramVal;
i++;
continue;
}
else if(param == "-c") {
comment = paramVal;
i++;
continue;
}
else if(param == "-e") {
}
else if(param == "-i") {
}
else {
cout << "ERROR >>> Unrecognised parameter %s" << param << endl;
}
}
cout << "Name: %s\nComment: %s" << name << comment << endl;
return(0);
}
The program compiles fine (using g++) but when I try to run ./createDesktopIcon -n a -c b I get the following error
terminate called after throwing an instance of 'std::logic_error'
what(): basic_string::_M_construct null not valid
Aborted
Please help as it is very frustrating
Here are the problems I see:
i <= argc
You want to compare i < argc because the argv[argc] element in the array is actually one past the last element in the argv array.
Also, here:
string paramVal = argv[i+1];
This will access the array out of bounds as well.
You might want to look at getopt to do all of this for you.
Related
When I run this program:
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char** argv)
{
if (argc != 2)
{
cerr << "[ERR] usage: " << argv[0] << " expression" << endl;
return 1;
}
string pExpression = argv[1];
size_t stringLength = pExpression.length();
if (pExpression[stringLength - 1] != '#') //If there is no hashtag at the end, append one
pExpression += '#';
cout << "Search for '#' in '" << pExpression << "'..." << endl;
bool found = false;
for (size_t i = 0;i < stringLength;i++)
{
if (pExpression[i] == '#')
found = true;
}
cout << ((found) ? "String contains '#'" : "String doesn't contain '#'") << endl;
return 0;
}
I try to check, if there is a hashtag at the end. When there is none, I append one.
However, if I check for it, my programm "can't find" it.
Here is an example on what I mean:
Because you don't increase stringLength after you add the #. So the for loop stops right in front of it.
I'm new to C++. I'm writing a simple calculator using command line. The command line should have this format:
programname firstNumber operator secondNumber
Here what I got so far:
#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char* argv[])
{
if (argc != 3)
{
cerr << "Usage: " << argv[0] << endl;
exit(0);
}
else
{
int firstNumber = atoi(argv[1]);
char theOperator = atoi(argv[2]);
int secondNumber = atoi(argv[3]);
switch (theOperator)
{
case'+':
{
cout << "The answer is " << firstNumber + secondNumber << endl;
break;
}
case '-':
{
cout << "The answer is " << firstNumber - secondNumber << endl;
break;
}
case '*':
{
cout << "The answer is " << firstNumber * secondNumber << endl;
break;
}
case '/':
{
if (secondNumber == 0)
{
cout << "Can not devide by a ZERO" << endl;
break;
}
else
{
cout << "The answer is " << firstNumber / secondNumber << endl;
break;
}
}
}
}
}
The program does not run. When I run it, it displays an appropriate usage message and end the program. Can anyone please help me?
Others have already given you the answer but you could have very easily figured this one out on your own. Just print what argc is at the point where you know the code is going into:
int main(int argc, char* argv[])
{
if (argc != 3)
{
cout << "argc is: " << argc << endl; // Debug output that you delete later
cerr << "Usage: " << argv[0] << endl;
exit(0);
}
else
And then come back with what argc is. When you find that argc is actually 4 and you want to know what is inside argc you should write some code to print it so that you can figure it out... Like this:
int main(int argc, char* argv[])
{
cout << "argc is: " << argc << endl; // Debug output that you delete later
for (int i = 0; i < argc; ++i)
{
// Print out all of the arguments since it's not working as you expect...
cout << "argv[" << i << "] = " << argv[i] << endl;
}
if (argc != 3)
{
cerr << "Usage: " << argv[0] << endl;
exit(0);
}
else
and you would have very quickly figured out what is wrong...
Please learn how to do this because it will save your but in the future and you won't have to wait for an answer here.
Additionally there is another error in your code.
Why on earth are you converting the + character from a string to an int?
else
{
int firstNumber = atoi(argv[1]);
char theOperator = atoi(argv[2]); // <<< WTF? Why?
int secondNumber = atoi(argv[3]);
switch (theOperator)
You probably want to get rid of the atoi part there and just go with:
char theOperator = argv[2][0]; // First character of the string
Provided that the second argument will always have only one letter... Which you might want to enforce/check. See strlen() and std::string and note that the type of argv[2] is char* (pointer to char).
I also recommend that you read How to debug small programs which is linked from the SO Howto-Ask Help Page. It may help a little. And no, I don't think your question is bad. Debugging small programs is a skill you'll need in the future if you intend to program so it will benefit you to learn it now.
Welcome to programming and C++ :)
The parameter argc also counts the program's name.
Try this:
if (argc != 4) // We expect 4 arguments: programname number operator number
{
cerr << "Usage: " << argv[0] << " <number> <operator> <number>" << endl;
exit(0);
}
In your code, running the program correctly (with all 3 parameters) displays the error message because argc equals 4.
If you type in
programname firstNumber operator secondNumber
You have 4 arguments, not 3.
argv[0] = programname
argv[1] = firstNumber
argv[2] = operator
argv[3] = secondNumber
Looks like your program is working correctly... at least as far as printing the usage message goes.
See also the other comments regarding your use of the operator argument.
I am writing a simple bracket checker. Should be pretty easy. I had it working when it was all in one function, but I am required to also make something for stdin. So I thought it was best to make 2 functions. That being said I am getting an error on the checking if the stack is null on line 82. for whatever reason it is not allowing me to check if the top of my stack is null. I tried in a testing program to see if it was some sort of referencing error or if it was going out of scope by going into the other method. Its not. Should work fine because it is a global variable.
Thoughts on what I am doing wrong? All my interneting and knowledge points me to the idea that I am doing it correctly.
Below is all of the code. its compilable. if I need to clarify anything I would be more than happy to.
Thanks
#include <iostream>
#include <sstream>
#include <stack>
#include <deque>
#include <fstream>
#include <cstdlib>
using namespace std;
stack<char> BracketsCheck;
int linecounter = 0;
int FileNumber = 1;
int pos;
string str ="";
string filename;
int validate(string string)
{
int size = str.size();
for (int i = 0; i < str.size(); i++)
{
pos = i;
if ((str[i] == '(' ) || (str[i] == '[') || (str[i] == '{'))
{
BracketsCheck.push(str[i]);
}
else if (str[i] == ')')
{
if (BracketsCheck.top() == '(')
BracketsCheck.pop();
else
{
cout << filename << ":" << linecounter << ":" << pos << "ERROR: missing open parenthesis" << endl;
return EXIT_FAILURE;
}
}
else if (str[i] == ']')
{
if (BracketsCheck.top() == '[')
BracketsCheck.pop();
else
{
cout << filename << ":" << linecounter << ":" << pos << "ERROR: missing open squre bracket" << endl;
return EXIT_FAILURE;
}
}
else if (str[i] == '}')
{
if (BracketsCheck.top() == '{')
BracketsCheck.pop();
else
{
cout << filename << ":" << linecounter << ":" << pos << "ERROR: missing open curly brace" << endl;
return EXIT_FAILURE;
}
}
}
}
int main(int argc, char* argv[])
{
// BracketsCheck.top() = 'h';
if (argc == 1)
{
cin >> str;
cout << "no arguments" << endl;
validate (str);
return 0;
}
else
{
while (argv[FileNumber] != NULL)
{
filename = argv[FileNumber];
ifstream inFile(argv[FileNumber]);
cout << argv[FileNumber]<<endl;
while (getline(inFile, str))
{
validate(str);
linecounter++;
}
if (BracketsCheck.top() != NULL)
{
cout << "got to null checker" << endl;
cout << filename << ":" << linecounter << ":" << pos << "umatched closing brace" << endl;
return EXIT_FAILURE;
}
FileNumber++;
}
return 0;
}
}
Based on your response to my comments. If you are trying to check if the stack is not empty you should use !BracketsCheck.empty() also:
int validate(string string)
is probably not a good idea since you will hiding the string type.
top() will return a reference or const reference not a pointer and if your stack is empty you should not be calling top.
I would also discourage you from using:
using namespace std;
it is considered bad practice I realize typing std:: all the time can be annoying at first but you really do get used to it after a while.
Finally validate needs a return statement since it is supposed to return int and flowing off the end of function without a return in this case will invoke undefined behavior as per 6.6.3 The return statement paragraph 2 from the draft C++ standard.
I have a C++ executable that, in normal use, accepts a file name as an argument option in the following manner:
executable -i myFile.txt
I want to use Bash process substitution to create a 'virtual file' and send information (simple, line by line data) to this executable in the following manner:
executable -i <(echo "${myData}")
However, my C++ program is not accessing the information when I use this process substitution. The main file reading section of code in the C++ program is the following:
ifstream file1 (fileName1);
string line;
int currentLineNumber = 0;
if (verboseFlag == 1) {cout << "reading data from file " << fileName1 << "..." << endl;}
while (getline (file1, line)){
currentLineNumber++;
if (verboseFlag == 1) {cout << "line " << currentLineNumber << ": ";}
istringstream linestream(line);
string item;
int itemNumber = 0;
while (getline (linestream, item, ',')){
itemNumber++;
if (verboseFlag == 1) {cout << "item " << itemNumber << ": " << item << " ";}
// data
if (itemNumber == 1) {x[currentLineNumber]=atof(item.c_str());}
if (itemNumber == 2) {y[currentLineNumber]=atof(item.c_str());}
}
}
file1.close();
Could you point me in the right direction on solving this reading problem? Is there some better approach that would work for both normal file reading and process substitution 'file' reading?
I'm new to this process substitution and I very much appreciate any assistance on this.
EDIT:
Following some comments, what follows is a minimal working example illustrating the problem I am encountering:
// definition of standard input/output stream objects
#include <iostream>
// manipulate strings as though they were input/output streams
#include <sstream>
// input and output operations
#include <stdio.h>
// file input and output operations
#include <fstream>
// manipulate C strings and arrays
#include <string.h>
// classify and transform individual characters
#include <ctype.h>
// Standard General Utilities Library
#include <stdlib.h>
// getopts (handle command line options and arguments)
#include <unistd.h>
// sstream (handle conversion from char* to double)
#include <sstream>
using namespace std;
double returnDoubleFromPointerToChar(const char *cText){
std::stringstream ss ( cText );
double dText = 0;
ss >> dText;
return dText;
}
int returnNumberOfLinesInFile(const char *fileName1){
int lineCount = 0;
string line;
ifstream file1(fileName1);
while (std::getline(file1, line))
++lineCount;
file1.close();
return lineCount;
}
int main (int argc, char **argv){
char *fileName1 = NULL; // input file name (i) (required input)
int verboseFlag = 0; // verbose flag (v)
int index; // internal variable
int c; // internal variable
opterr = 0;
// process command line arguments and options
while ((c = getopt (argc, argv, "i:v")) != -1)
switch (c){
case 'i':
fileName1 = optarg;
break;
case 'v':
verboseFlag = 1;
break;
case '?':
if (
optopt == 'i'
){
fprintf (stderr, "option -%c requires an argument.\n", optopt);
}
else if (isprint (optopt)){
fprintf (stderr, "unknown option `-%c'.\n", optopt);
}
else {
fprintf (stderr, "unknown option character `\\x%x'.\n", optopt);
}
return 1;
default:
abort ();
}
for (index = optind; index < argc; index++) printf ("non option argument %s\n", argv[index]);
if (verboseFlag == 1){
cout << endl;
cout << "input file name: " << fileName1 << endl;
}
// Determine the number of lines in the input file.
int numberOfLinesInInputFile=returnNumberOfLinesInFile(fileName1);
if (verboseFlag == 1) {cout << "number of lines in input file: " << numberOfLinesInInputFile << endl;}
// number of data points
int n=numberOfLinesInInputFile-1;
// x variable
double x[n];
// y variable
double y[n];
// Access the data in the input file.
ifstream file1 (fileName1);
string line;
int currentLineNumber = 0;
if (verboseFlag == 1) {cout << "reading data from file " << fileName1 << "..." << endl;}
while (getline (file1, line)){
currentLineNumber++;
if (verboseFlag == 1) {cout << "line " << currentLineNumber << ": ";}
istringstream linestream(line);
string item;
int itemNumber = 0;
while (getline (linestream, item, ',')){
itemNumber++;
if (verboseFlag == 1) {cout << "item " << itemNumber << ": " << item << " ";}
// data
if (itemNumber == 1) {x[currentLineNumber]=atof(item.c_str());}
if (itemNumber == 2) {y[currentLineNumber]=atof(item.c_str());}
}
if (verboseFlag == 1) {cout << endl;}
}
file1.close();
return 0;
}
EDIT:
I have added the solution code below (following from a comment by that other guy):
// include WBM C++ library
// #include "lib_cpp.c"
// definition of standard input/output stream objects
#include <iostream>
// manipulate strings as though they were input/output streams
#include <sstream>
// input and output operations
#include <stdio.h>
// file input and output operations
#include <fstream>
// manipulate C strings and arrays
#include <string.h>
// classify and transform individual characters
#include <ctype.h>
// Standard General Utilities Library
#include <stdlib.h>
// getopts (handle command line options and arguments)
#include <unistd.h>
// sstream (handle conversion from char* to double)
#include <sstream>
using namespace std;
// example usage:
// ./graph2d -i data.txt -o data.eps -v
// ./graph2d -i data.txt -o graph.eps -t "training test error versus epochs" -x "epochs" -y "error measure" -v
// ./graph2d -i data.txt -o graph.eps -t "training test error versus epochs" -x "epochs" -y "error measure" -a 70 -b 50 -c 22 -d 7 -v
// ./graph2d -i <(echo "${dataForTrainingErrorVersusEpoch}") -o graph.eps -t "training test error versus epochs" -x "epochs" -y "error measure" -v
double returnDoubleFromPointerToChar(const char *cText){
std::stringstream ss ( cText );
double dText = 0;
ss >> dText;
return dText;
}
int main (int argc, char **argv){
char *fileName1 = NULL; // input file name (i) (required input)
char *fileName2 = NULL; // output file name (o) (required input)
char *graphTitleMain = NULL; // graph title (t)
char *graphTitleAxisx = NULL; // graph x axis title (x)
char *graphTitleAxisy = NULL; // graph y axis title (y)
double axisyMaximum = DBL_MAX; // y axis maximum (a)
double axisyMinimum = DBL_MAX; // y axis minimum (b)
double axisxMaximum = DBL_MAX; // x axis maximum (c)
double axisxMinimum = DBL_MAX; // x axis minimum (d)
int verboseFlag = 0; // verbose flag (v)
int index; // internal variable
int c; // internal variable
opterr = 0;
// process command line arguments and options
while ((c = getopt (argc, argv, "i:o:t:x:y:a:b:c:d:v")) != -1)
switch (c){
case 'i':
fileName1 = optarg;
break;
case 'o':
fileName2 = optarg;
break;
case 't':
graphTitleMain = optarg;
break;
case 'x':
graphTitleAxisx = optarg;
break;
case 'y':
graphTitleAxisy = optarg;
break;
case 'a':
axisyMaximum = returnDoubleFromPointerToChar(optarg);
break;
case 'b':
axisyMinimum = returnDoubleFromPointerToChar(optarg);
break;
case 'c':
axisxMaximum = returnDoubleFromPointerToChar(optarg);
break;
case 'd':
axisxMinimum = returnDoubleFromPointerToChar(optarg);
break;
case 'v':
verboseFlag = 1;
break;
case '?':
if (
optopt == 'i' ||
optopt == 'o' ||
optopt == 't' ||
optopt == 'x' ||
optopt == 'y' ||
optopt == 'a' ||
optopt == 'b' ||
optopt == 'c' ||
optopt == 'd'
){
fprintf (stderr, "option -%c requires an argument.\n", optopt);
}
else if (isprint (optopt)){
fprintf (stderr, "unknown option `-%c'.\n", optopt);
}
else {
fprintf (stderr, "unknown option character `\\x%x'.\n", optopt);
}
return 1;
default:
abort ();
}
for (index = optind; index < argc; index++) printf ("non option argument %s\n", argv[index]);
if (verboseFlag == 1){
cout << endl;
cout << "input file name: " << fileName1 << endl;
cout << "output file name: " << fileName2 << endl;
}
// x variable
vector<int> x;
// y variable
vector<int> y;
// Access the data in the input file.
ifstream file1 (fileName1);
string line;
int currentLineNumber = 0;
if (verboseFlag == 1) {cout << "reading data from file " << fileName1 << "..." << endl;}
while (getline (file1, line)){
currentLineNumber++;
if (verboseFlag == 1) {cout << "line " << currentLineNumber << ": ";}
istringstream linestream(line);
string item;
int itemNumber = 0;
while (getline (linestream, item, ',')){
itemNumber++;
if (verboseFlag == 1) {cout << "item " << itemNumber << ": " << item << " ";}
// data
if (itemNumber == 1) {x.push_back(atof(item.c_str()));}
if (itemNumber == 2) {y.push_back(atof(item.c_str()));}
}
if (verboseFlag == 1) {cout << endl;}
}
file1.close();
int numberOfLinesInInputFile = currentLineNumber + 1;
// number of data points
int n=numberOfLinesInInputFile;
// graph
if (verboseFlag == 1){
cout << "graph main title: " << graphTitleMain << endl;
cout << "graph x axis title: " << graphTitleAxisx << endl;
cout << "graph y axis title: " << graphTitleAxisy << endl;
}
// Create a new canvas.
TCanvas *c1 = new TCanvas(graphTitleMain, graphTitleMain); // #u
// Create a new graph.
//TGraph *graph = new TGraph(n, x, y);
TGraph *graph = new TGraph(n, &x[0], &y[0]);
// Set the graph titles.
graph->SetTitle(graphTitleMain);
graph->GetXaxis()->SetTitle(graphTitleAxisx);
graph->GetYaxis()->SetTitle(graphTitleAxisy);
// Set the marker styles.
graph->SetMarkerColor(2); // red
graph->SetMarkerStyle(kFullCircle); // circle
graph->SetMarkerSize(1); // default size
// Set the graph range, if ranges have been specified in command line options.
if (
axisyMaximum != DBL_MAX &&
axisyMinimum != DBL_MAX
){
if (verboseFlag == 1){
cout << "graph y axis minimum: " << axisyMinimum << endl;
cout << "graph y axis maximum: " << axisyMaximum << endl;
}
graph->GetYaxis()->SetRangeUser(axisyMinimum, axisyMaximum);
}
if (
axisxMaximum != DBL_MAX &&
axisxMinimum != DBL_MAX
){
if (verboseFlag == 1){
cout << "graph x axis minimum: " << axisxMinimum << endl;
cout << "graph x axis maximum: " << axisxMaximum << endl;
}
graph->GetXaxis()->SetRangeUser(axisxMinimum, axisxMaximum);
}
// Draw the canvas, then draw the graph and then save the canvas to an image file.
c1->Draw();
graph->Draw("ALP");
// disable ROOT messages
gErrorIgnoreLevel = 5000;
if (verboseFlag == 1) {cout << "saving file " << fileName2 << "..." << endl;}
c1->SaveAs(fileName2);
if (verboseFlag == 1) {cout << endl;}
return 0;
}
First of all, I trimmed out two thirds of your program and it still shows the problem. This is significantly closer to minimal:
#include <iostream>
#include <fstream>
using namespace std;
int returnNumberOfLinesInFile(const char *fileName1){
int lineCount = 0;
string line;
ifstream file1(fileName1);
while (std::getline(file1, line))
++lineCount;
file1.close();
return lineCount;
}
int main (int argc, char **argv){
char *fileName1 = argv[1];
cout << "input file name: " << fileName1 << endl;
int numberOfLinesInInputFile=returnNumberOfLinesInFile(fileName1);
cout << "number of lines in input file: " << numberOfLinesInInputFile << endl;
ifstream file1(fileName1);
string line;
cout << "File contents: " << endl;
while (getline (file1, line)){
cout << "line: " << line << endl;
}
file1.close();
return 0;
}
The problem here is that you open the file twice. <(process substitution) only runs the command once and streams the result. Bash doesn't take the liberty of running the command again if you want to read output again, since the command could have been doing a lot of other things besides spitting out text.
Make sure your program only opens and reads the contents once, and it'll work. This may require you to rewrite your logic a bit, or just be lazy and read it all into memory at once.
Your code works fine for me (I'm on OS X).
Keep in mind that, unlike real files, "virtual files" are usually pipe endpoints (implemented in bash using file descriptor special files). So, you cannot open, read, and close a virtual file more than once, or you will get nothing the second time around.
If you go in my post history you'll see that i'm trying to develop an interpreter for a language that i'm working on. I want to use size_t using two different codes, but they all return nothing.
Here is the post of what i was trying: http://stackoverflow.com/questions/1215688/read-something-after-a-word-in-c
When i try to use the file that i'm testing it returns me nothing. Here is the sample file(only a print function that i'm trying to develop in my language):
print "This is a print function that i'm trying to develop in my language"
But remember that this is like print in Python, what the user type into the quotes(" ") is what have to be printed to all, remember that the user can choose what put into the quotes, then don't put something like a simple cout, post something that reads what is inside the quotes and print it to all. But here is the two test codes to do this, but all of they don't returns nothing to me:
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
int main( int argc, char* argv[] )
{
// Error Messages
string extension = argv[ 1 ];
if(argc != 2)
{
cout << "Error syntax is incorrect!\nSyntax: " << argv[ 0 ] << " <file>\n";
return 0;
}
if(extension[extension.length()-3] != '.')
{
cout << "Extension not valid!" << endl;
cout << "Default extension *.tr" << endl;
return 0;
}
if(extension[extension.length()-2] != 't')
{
cout << "Extension not valid!" << endl;
cout << "Default extension *.tr" << endl;
return 0;
}
if(extension[extension.length()-1] != 'r')
{
cout << "Extension not valid!" << endl;
cout << "Default extension *.tr" << endl;
return 0;
}
// End of the error messages
ifstream file(argv[ 1 ]);
if (!file.good()) {
cout << "File " << argv[1] << " does not exist.\n";
return 0;
}
string linha;
while (!file.eof())
{
getline(file, linha);
if (linha == "print")
{
size_t idx = linha.find("\""); //find the first quote on the line
while ( idx != string::npos ) {
size_t idx_end = linha.find("\"",idx+1); //end of quote
string quotes;
quotes.assign(linha,idx,idx_end-idx+1);
// do not print the start and end " strings
cout << "quotes:" << quotes.substr(1,quotes.length()-2) << endl;
//check for another quote on the same line
idx = linha.find("\"",idx_end+1);
}
}
}
return 0;
}
The second:
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
int main( int argc, char* argv[] )
{
// Error Messages
string extension = argv[ 1 ];
if(argc != 2)
{
cout << "Error syntax is incorrect!\nSyntax: " << argv[ 0 ] << " <file>\n";
return 0;
}
if(extension[extension.length()-3] != '.')
{
cout << "Extension not valid!" << endl;
cout << "Default extension *.tr" << endl;
return 0;
}
if(extension[extension.length()-2] != 't')
{
cout << "Extension not valid!" << endl;
cout << "Default extension *.tr" << endl;
return 0;
}
if(extension[extension.length()-1] != 'r')
{
cout << "Extension not valid!" << endl;
cout << "Default extension *.tr" << endl;
return 0;
}
// End of the error messages
ifstream file(argv[ 1 ]);
if (!file.good()) {
cout << "File " << argv[1] << " does not exist.\n";
return 0;
}
string linha;
while (!file.eof())
{
getline(file, linha);
if (linha == "print")
{
string code = " print \" hi \" ";
size_t beg = code.find("\"");
size_t end = code.find("\"", beg+1);
// end-beg-1 = the length of the string between ""
cout << code.substr(beg+1, end-beg-1);
}
}
return 0;
}
And here is what is printed in the console:
ubuntu#ubuntu-laptop:~/Desktop/Tree$ ./tree test.tr
ubuntu#ubuntu-laptop:~/Desktop/Tree$
Like i said, it prints me nothing.
See my post in D.I.C.: http://www.dreamincode.net/forums/showtopic118026.htm
Thanks,
Nathan Paulino Campos
Your problem is the line
if (linha == "print")
which assumes the entire line just read in is "print", not that the line STARTS with print.
Also, why would you use 3 separate checks for a .tr extension, vs. just checking the end of the filename for ".tr"? (You should also be checking that argv[1] is long enough before checking substrings...)
getline(file, linha) will read an entire line from the file, so linha never be equal to print.