#include<iostream>
#include<fstream>
#include <cstring>
using namespace std;
int main(){
char a[14] ="haha";
cout<<"char[14]: "<<sizeof(a)<<" "<<strlen(a)<<endl;
ofstream fout;
fout.open("disk.txt");
fout<<a;
fout << "\n";
fout.close();
return 0;
}
我想输出14个字符到文本里去,而不是只有4个,请问怎么实现
#include<iostream>
#include<fstream>
#include <cstring>
void padright(char * s, int n, char c)
{
int b = 0;
for (int i = 0; i < n; i++)
{
if (!b && s[i] == '\0') b = 1;
if (b) s[i] = c;
}
s[n] = '\n';
}
int main(){
char a[15] ="haha"; //因为\0也需要一个字节,所以是15
cout<<"char[14]: "<<sizeof(a)<<" "<<strlen(a)<<endl;
ofstream fout;
fout.open("disk.txt");
padright(a, 14, ' '); //结尾用空格填充
fout<<a;
fout << "\n";
fout.close();
return 0;
}