Ryan Rapini
Ryan Rapini

Reputation: 385

String turns up empty after find_last_of() and substr()?

Self-teaching myself C++, and I know I'm missing something critical, but I can't for the life of me figure out what it is.

Forgive the huge block of code, I was tempted to trim it down to the critical elements, but I figured if I left it intact, you folks might have other educational criticisms about my coding style...

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <fstream>

using namespace std;

//  main routine
int main(int argc, char *argv[]) {
    // will store filetype here for later
    string filetype = "";
    string filename;

    // if no arguments, die.
    if (argc < 2) {
        cout << "ERROR: Nothing to do." << endl;
        return 1;
    }

    // if more than one argument, die.
    else if (argc > 2) {
        // TODO: support for multiple files being checked would go here.
        cout << "ERROR: Too many arguments." << endl;
        return 1;
    }

    // otherwise, check filetype
    else {
        string filename = argv[1];
        cout << "Filename: " << filename << endl;
        //searching from the end, find the extension of the filename
        int dot = filename.find_last_of('.');
        if (dot == -1){
            // TODO: Add support for filenames with no extension
            cout << "ERROR: Filename with no extension." << endl;
            return 1;
        }
        string extension = filename.substr(dot); 
        if (extension == ".htm" || extension == ".html"){
            filetype = "html";
        }
        else if (extension == ".c"){
            filetype = "c";
        }
        else if (extension == ".c++" || extension == ".cpp") {
            filetype = "cpp";
        }
        else {
            cout << "ERROR: unsupported file extension" << endl;
            // TODO: try to guess filetype from file headers here
        }
    }

    cout << "Determined filetype: " << filetype << endl;
    cout << "Filename: " << filename << endl;

    return 0;
}
                                            // All done :]

The issue I'm having is mysterious. I put the argument passed into a string like so:

string filename = argv[1];

and then search it for an extension, starting from the end and working my way to the beginning:

int dot = filename.find_last_of('.');
string extension = filename.substr(dot);

This all works as expected, but afterwards, when I try to output filename, it is mysteriously empty? I tried debugging with cout. When I print out the string BEFORE I search it, it prints properly. After, nothing prints. Like so:

$ g++ test.cpp -o test.out; ./test.out foo.html
Filename: foo.html
Determined filetype: html
Filename:

I remembered something about iterators in the past, and tried using filename.begin() to reset it, but this did nothing. Can someone shed light onto this puzzling issue?

Upvotes: 0

Views: 1020

Answers (1)

Brian Neal
Brian Neal

Reputation: 32399

You are declaring a second variable called filename here, after the else:

string filename = argv[1];

This goes out of scope by the time you get here:

 cout << "Filename: " << filename << endl;

You are now printing the contents of the 1st variable you declared called filename, just under main.

Upvotes: 3

Related Questions