###問題
下のコードはファイルのパスを受け取りそのパスをもとにファルの拡張子とファイル名を表示するプログラムを書いて動いていたんですが、急にエラーがでるようになりました。
発生箇所は、get_ext(fpath)
とget_filename(fpath)
で発生していると思います。
###エラー
により ハンドルされない例外が 0x753CA9F2 で発生しました (test.exe 内): Microsoft C++ の例外: std::out_of_range (メモリの場所 0x003EF600)。 が発生しました
###ソース
main.cpp
C++
1#include <iostream> 2#include <fstream> 3#include <string> 4 5#include "Fileinfo.hpp" 6 7using namespace std; 8 9 10int main() { 11 string fpath; // ファイルパス保管 12 string err; 13 14 cout << "ファイルをドロップしてください。\n"; 15 cin >> fpath; // パス入力 16 17 if (!fpath.empty()) // 開いたファイルの中身が空じゃなかったら 18 { 19 cout << "ファイルを受け取りました。\n"; 20 21 cout << "ファイルの拡張子:" << get_ext(fpath) << "\n"; 22 cout << "ファイルの名前:" << get_filename(fpath) << "\n"; 23 } 24}
Fileinfo.hpp
hpp
1#pragma once 2#include <string> 3 4int _path(std::string fpath) { 5 int path_i = fpath.find_last_of("\\") + 1; // ファイルパスから、最後のディレクトリ名までも文字数取得 6 return path_i; 7} 8int _ext(std::string fpath) { 9 int ext_i = fpath.find_last_of("."); // ファイルパスから、最後の.までの文字数取得 10 return ext_i; 11} 12 13std::string get_ext(std::string path) { 14 std::string extname = path.substr(_ext(path)); // 拡張子取得 15 return extname; 16} 17std::string get_filename(std::string path) { 18 std::string filename = path.substr(_path(path), _ext(path) - _path(path)); // ファイル名取得 19 return filename; 20} 21
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/07/16 06:28
2017/07/16 07:11
2017/07/16 07:17
2017/07/16 08:14
2017/07/16 13:14 編集
2017/07/16 15:21
2017/07/18 01:24