我有一个私有类变量char name[10],我想添加.txt扩展名,以便我可以打开目录中的文件.
我该怎么做?
最好创建一个包含连接字符串的新字符串变量.
我想写一个std::string我从用户接受的变量到一个文件.我尝试使用该write()方法,并写入该文件.但是当我打开文件时,我看到的是盒子而不是字符串.
该字符串只是一个可变长度的单个字.是否std::string适合这个还是应该使用字符数组或东西.
ofstream write;
std::string studentName, roll, studentPassword, filename;
public:
void studentRegister()
{
cout<<"Enter roll number"<<endl;
cin>>roll;
cout<<"Enter your name"<<endl;
cin>>studentName;
cout<<"Enter password"<<endl;
cin>>studentPassword;
filename = roll + ".txt";
write.open(filename.c_str(), ios::out | ios::binary);
write.put(ch);
write.seekp(3, ios::beg);
write.write((char *)&studentPassword, sizeof(std::string));
write.close();`
}
Run Code Online (Sandbox Code Playgroud) int main(void)
{
unsigned int y = 10;
int x = – 4;
if (x > y)
Printf("x is greater");
else
Printf("y is greater");
getch();
return (0);
}
Output: x is greater
Run Code Online (Sandbox Code Playgroud)
我认为输出y会更大,因为它是无符号的.这背后的原因是什么?