#include <bits/stdc++.h>
#include <windows.h>
using namespace std;
int x;
void color(short x){
if(x>=0 && x<=15)SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), x);
else SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
}
void compile(string cppfile){
string rfile="g++ -o "+(cppfile.substr(0,cppfile.size()-4)+".exe")+" "+cppfile;
cout<<rfile<<endl;
const char* system_code=rfile.data();
color(12);
system(system_code);
color(100);
cout<<endl;
}
int main() {
cout<<"Welcome to SuperG++!\n";
while(cout<<"Which one do you want?\n\t1.create a new file.\n\t2.open a file.\n\t3.online c++.\n\t4.compile a file.\n\t5.close all file.\n\t(1 or 2 or 3 or 4 or 5)\n>>>",cin>>x){
if(x==1){
string a;
cout<<"File:";
cin>>a;
a+=".cpp";
const char* str=a.data();
freopen(str,"w",stdout);
cout<<"/*SuperG++:\n\tversion:1.0.0\n*/";
cin.clear();
freopen("CON","w",stdout);
cout<<"The file has been created.\nDo you want to edit?(Yes or No)\n>>>";
string opt;
while(cin>>opt){
if(opt=="No"){
system("CLS");
break;
}else if(opt=="Yes"){
string code;
string linecode;
while(getline(cin,linecode)){
code+=linecode;
code+='\n';
}
freopen(str,"w",stdout);
cout<<code;
fclose(stdout);
fflush(stdout);
cin.clear();
freopen("CON","w",stdout);
compile(str);
break;
}else{
color(12);
cout<<"Error.There is no such option.\n";
color(100);
continue;
}
}
}else if(x==2){
}else if(x==3){
}else if(x==4){
}else if(x==5){
system("CLS");
}
else{
color(12);
cout<<"Error.There is no such option.\n";
color(100);
}
}
return 0;
}
c++ 文件换行♪◙○乱码
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
4条回答 默认 最新
- 阿里嘎多学长 2024-07-11 15:06关注
以下内容由CHATGPT及阿里嘎多学长共同生成、有用望采纳:
您好!看起来您遇到了一个编码问题。在您的 C++ 程序中,编译后的文件换行字符变成乱码,这通常是因为文件的编码格式与您的终端或编辑器的编码格式不匹配导致的。
为什么会出现乱码?
- 编码不一致:如果您的源代码文件使用的是 UTF-8 编码,而终端或编译器默认使用的是其他编码(如 ANSI 或 ISO-8859-1),这可能导致显示乱码。
- 终端设置:Windows 命令提示符默认使用 OEM 编码,而不是 UTF-8,这可能导致显示问题。
- 文件写入方式:在您的代码中,使用
freopen
函数重定向stdout
到文件时,如果freopen
没有正确设置文件的编码方式,也可能导致乱码。
解决方案
- 确保编码一致性:确保您的源代码文件、终端和编译器都使用相同的编码格式,推荐使用 UTF-8。
- 更改终端编码:在 Windows 命令提示符中,可以通过以下命令设置为 UTF-8 编码:
chcp 65001
- 设置文件编码:在使用
freopen
写入文件时,确保设置正确的编码。例如,使用_wfreopen
函数(宽字符版本)并指定std::ios::out
与std::locale()
来设置正确的编码。 - 检查
system
调用:system
调用执行的命令可能会受到当前环境的影响,确保执行的命令在您的系统中是有效的。
参考链接
- UTF-8 Everywhere
- Stack Overflow: How to change console encoding in Windows?
- Microsoft Docs: freopen function
希望这些信息能帮助您解决问题。如果还有其他问题或需要进一步的帮助,请随时告诉我!
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决评论 打赏 举报 编辑记录无用 1
悬赏问题
- ¥15 无法输出helloworld
- ¥15 高通uboot 打印ubi init err 22
- ¥20 PDF元数据中的XMP媒体管理属性
- ¥15 R语言中lasso回归报错
- ¥15 网站突然不能访问了,上午还好好的
- ¥15 有没有dl可以帮弄”我去图书馆”秒选道具和积分
- ¥15 semrush,SEO,内嵌网站,api
- ¥15 Stata:为什么reghdfe后的因变量没有被发现识别啊
- ¥15 振荡电路,ADS仿真
- ¥15 关于#c语言#的问题,请各位专家解答!