Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7800005
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T00:29:32+00:00 2026-06-02T00:29:32+00:00

Self-teaching myself C++, and I know I’m missing something critical, but I can’t for

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T00:29:34+00:00Added an answer on June 2, 2026 at 12:29 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've taught myself Obj-C, and have been self-teaching Cocoa, but adding Core Data to
I know this is a stupid question, but I have been teaching myself c#,
I'm teaching myself .NET c# through books and self-assigned projects for fun. I thought
i'm self-teaching c++ and i get how pointers work. but the doc i'm using
I am teaching my self hibernate, and am quite confused of why i can
I am self teaching myself some python OOP, and I have created a simple
Without any mentor, self-teaching myself with books and references on the web, how do
self.close() is working fine in IE but not in Mozilla. Does anyone know what
I'm teaching my self Qt; in the same time I don't want to lose
I've been teaching myself python recently an came across and example where str.endswith took

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.