小编bla*_*axc的帖子

C++如何将整数存储到二进制文件中?

我有一个包含2个整数的结构,我想将它们存储在二进制文件中并再次读取.

这是我的代码:

static const char *ADMIN_FILE = "admin.bin";
struct pw {  
  int a;  
  int b;
};

void main(){  
  pw* p = new pw();  
  pw* q = new pw();  
  std::ofstream fout(ADMIN_FILE, ios_base::out | ios_base::binary | ios_base::trunc);  
  std::ifstream fin(ADMIN_FILE, ios_base::in | ios_base::binary);  
  p->a=123;  
  p->b=321;  
  fout.write((const char*)p, sizeof(pw));  
  fin.read((char*)q, sizeof(pw));  
  fin.close();  
  cout << q->a << endl;
}  
Run Code Online (Sandbox Code Playgroud)

我得到的输出是0.谁能告诉我这是什么问题?

c++ binary file-io

4
推荐指数
1
解决办法
3396
查看次数

C++密码屏蔽

我正在编写一个代码来接收密码输入.下面是我的代码...程序运行良好,但问题是除了数字和字母字符之外的其他键也被读取,例如删除,插入等等我能否知道如何避免它?TQ ...

string pw="";
char c=' ';

while(c != 13) //Loop until 'Enter' is pressed
{
    c = _getch();
    if(c==13)
        break;

    if(c==8)
    {
        if(pw.size()!=0)   //delete only if there is input 
        {
            cout<<"\b \b";
            pw.erase(pw.size()-1);
        }
    }

    if((c>47&&c<58)||(c>64&&c<91)||(c>96&&c<123))  //ASCii code for integer and alphabet
    {
        pw += c;
        cout << "*";
    }
}
Run Code Online (Sandbox Code Playgroud)

c++ passwords input masking

3
推荐指数
1
解决办法
4012
查看次数

标签 统计

c++ ×2

binary ×1

file-io ×1

input ×1

masking ×1

passwords ×1