首页 > 解决方案 > 我的程序的 fstream 部分无法编译。并且程序的 ReplaceString 部分可以更快地执行吗?C++

问题描述

#include <stdio.h>
#include <tchar.h>
#include <locale.h>
#include <iostream>
#include <windows.h>
#include <fstream>



/*
    string中の特定文字列をstringで置換する
*/
std::string ReplaceString
(
      std::string String1   // 置き換え対象
    , std::string String2   // 検索対象
    , std::string String3   // 置き換える内容
)
{
    std::string::size_type  Pos( String1.find( String2 ) );

    while( Pos != std::string::npos )
    {
        String1.replace( Pos, String2.length(), String3 );
        Pos = String1.find( String2, Pos + String3.length() );
    }

    return String1;
}

int _tmain
(
      int argc
    , _TCHAR* argv[]
)
{
    FreeConsole();
    std::char data[100];
    std::cout << "filename you want; ";
    std::cin >> data;
    // 標準出力にユニコード出力する
    setlocale( LC_ALL, "Japanese" );
    std::string str, str1 ,str2, str3, str4, str5, str6, str7, str8, str9, str10, str11, str12, str13, str14, str15, str16, str17, str18, str19, str20, str21, str22, str23, str24, str25, str26, str27, str28, str29, str30, str31, str32, str33, str34, str35, str36, str37, str38, str39, str40, str41, str42, str43, str44, str45, str46, str47, str48, str49, str50, str51, str52, str53, str54, str55, str56, str57, str58, str59, str60, str61, str62, str63, str64, str65, str66, str67, str68, str69, str70, str71, str72, str73, str74, str75, str76, str77, str78, str79, str80, str81, str82, str83, str84, str85, str86, str87, str88, str89, str90, str91, str92, str93, str94;
    std::cout << "string you want encrypted:  ";
    std::getline(std::cin , str );
    // stringをstringで置換する
    str1 = ReplaceString(
              str
            , "v"
            , "-9H4VTkhW4-Nnx4"
        );


    // 標準出力へ出力する
    std::ofstream outfile;
    outfile.open(data + ".dat");
    outfile << str94 ;
    outfile.close();

    // 正常終了
    return( 0 );
}

我得到的错误是

E2272 an identifier is needed "std::char data[100]"

此外,键盘上的所有数字、字母和符号都有替换字符串代码,为 94 个字符。

加密一个简单的单词需要 3 多分钟,比如 hello。

如果有什么方法可以让这个程序运行得更快,请告诉我。

标签: c++stringfstream

解决方案


char是 C++ 语言的关键字,而不是在std. 因此,您不能char以. 为前缀std::。只需使用char而不是std::char.

但是,您打算使用数组从用户接收文件名,因此根本没有理由使用固定大小的字符数组。而是data使用 type定义std::string

std::string data;

这也避免了您在使用字符数组时遇到的一些问题。例如,如果is 的类型data + ".dat"是无效的,因为字符数组和字符串文字之间的加法根本没有定义。但是,使用as string 可以按预期工作,将字符串文字附加到 string 的副本。datachar[]datadata


推荐阅读