C++ 无法在visual studio中使用指针和流运行程序

C++ 无法在visual studio中使用指针和流运行程序,c++,visual-studio-2017,fstream,C++,Visual Studio 2017,Fstream,我可以在代码块或visual studio 2015中运行我的程序,但它在visual studio 2017中不起作用 #include <iostream> #include <fstream> #include <cstdlib> #include <string> using namespace std; void replacechar(char *filenguon, char ktc, char ktm) { fstream

我可以在代码块或visual studio 2015中运行我的程序,但它在visual studio 2017中不起作用

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;
void replacechar(char *filenguon, char ktc, char ktm)
{
    fstream fs(filenguon, ios::in | ios::out);
    if (!fs)
        cout << "khong the tim thay" << endl;
    else
    {
        char ch;
        while (fs.get(ch))
        {
            if (ch == ktc)
            {
                int pos = fs.tellg();
                pos--;
                fs.seekp(pos);
                fs.put(ktm);
                fs.seekg(pos + 1);
            }
        }
    }
}

int main()
{
    replacechar("caua.txt", 'r', 'R');
    return 0;
}

我可以在codeblock或visual studio 2015中运行我的程序,但它在visual studio 2017中不起作用

您不允许将
常量字符*
(在您的情况下,字符串literal
“caua.txt”
传递给接受非常量
字符*
的函数

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;
void replacechar(char *filenguon, char ktc, char ktm)
{
    fstream fs(filenguon, ios::in | ios::out);
    if (!fs)
        cout << "khong the tim thay" << endl;
    else
    {
        char ch;
        while (fs.get(ch))
        {
            if (ch == ktc)
            {
                int pos = fs.tellg();
                pos--;
                fs.seekp(pos);
                fs.put(ktm);
                fs.seekg(pos + 1);
            }
        }
    }
}

int main()
{
    replacechar("caua.txt", 'r', 'R');
    return 0;
}
将签名更改为
void replacechar(const char*filengoon、char ktc、char ktm)

更改

void replacechar(char *filenguon, char ktc, char ktm)

关于字符串文字的规则在C++11中发生了变化(我认为)。它们是常量数据,因此任何传递字符串文字的函数参数都应该用
const
声明

并且,如评论中所述,变更

int pos = fs.tellg();

tellg
返回的不是
int
,通过使用
auto
您要求编译器使用正确的类型,不管是什么类型。

两种方法:
一,

void replacechar(常量字符*filengoun,字符ktc,字符ktm)
{
//待办事项
}
2.
charstr[]={“caua.txt”;};
replacechar(str,'r','r');

这应该是工作,“caua.txt”是
const char*
,它通过逐个复制变为
char*
,还是
const\u cast

什么不起作用?有错误吗?是错误的结果吗?对不起。我刚刚更新了。你可以通过将所有内容翻译成英语来提高理解问题的难度。另外,尝试使用更多自解释的标识符。那么你写“run”是指“build”?因为这些都是编译器错误。更改它们也有助于减少混淆。
char*filengoon
=>
const char*filengoon
int pos=fs.tellg();
=>
auto pos=fs.tellg();
int pos = fs.tellg();
auto pos = fs.tellg();