/*
istreamostream
| \ / |
ifstreamiostreamofstream
|
fstream
*/
#include <iostream>
#include <fstream>
using namespace std
int main()
{
char ch[6]
cout <<"Input a line: "
ch[5] = cin.peek()
cout <<"Peek first char: " <<ch[5] <<endl
cin.get(ch[4])
cout <<"Read first char: " <<ch[4] <<endl
cin.putback(ch[4])
cin.getline(ch, 5)
if(!cin)
{
cout <<"Input error!!!" <<endl
cin.clear()
cin.ignore(100, '\n')
}
else
cout <<ch <<endl
cout <<left <<hex <<showbase <唤旁纤<uppercase <<1234 <<endl
cout <<scientific <<uppercase <<left <<0.00123456 <<endl
cout.fill('*')
cout.width(8)
cout.unsetf(ios::dec | ios::oct)
cout.setf(ios::hex | ios::showbase | ios::uppercase | ios::left)
cout <<1234 <<endl
cout.setf(ios::dec)
ofstream fout("data.txt") //使用fstream fio("data.txt", ios::in | ios::out)既可读又可写。
if(!fout.fail())
{
fout <<"3.14159\t64I love C++" <<endl
fout.put('M')
fout.put('\n')
double d = 0.0123456789
fout.precision(4)
fout.setf(ios::fixed)
//fout.setf(ios::scientific | ios::uppercase)
fout <<d <<endl
fout.seekp(3, ios::cur) //读使用seekg(),与seekp()类似。
fout <<'*' <<flush
fout.seekp(3, ios::cur)
fout <<endl
fout.seekp(2, ios::cur)
fout <<'@' <<' ' <<启岁 '*' <<' ' <<'@' <<endl
fout.seekp(2, ios::cur)
streampos j = fout.tellp()
while((fout.tellp()-j) <5)
fout <<'*' <<flush
cout <<"文件写入成功!" <<endl
}
else
cout <<"文件打开失败!" <<endl
fout.close()
ifstream fin
fin.open("data.txt", ios::in) //类似可以如fout那样定义
if(!fin.fail())
{
double d
char ch[20] = {0}
string str
fin >>d
cout <<d <<endl
fin.get(ch[0])
for(int i=0i<2i++)
{
fin.get(ch[i])
}
cout <<ch <<endl
while(!fin.eof())
{
fin.getline(ch, 20, '\n')
cout <<ch <<endl
}
}
else
cout <<"文件打开失败!" <<endl
fin.close()
ofstream fo
fo.open("bin.bin", ios::binary|ios::app)
if(!fo.fail())
{
int a[10]
for(int i=0i<10i++)
a[i] = i
fo.write((char *)a, sizeof(a))
}
else
cout <<"文件打开失败!" <<endl
fo.close()
ifstream fi
fi.open("bin.bin", ios::binary|ios::in)
if(!fi.fail())
{
int a[10]
fi.read((char *)a, sizeof(a))
fi.close()
for(int i=0i<10i++)
cout <<a[i]
cout <<endl
}
else
cout <<"文件打开失败!" <<endl
fi.close()
return 0
}
用锋念system函数简单调用ping命令:#include "stdio.h"
void main()
{
char ip[15]
char command[50]
// printf("enter IP please: ")
scanf("%s"银笑困,&ip[0])//输入要ping的ip
sprintf(command,"升瞎ping %s",ip)
system(command)
exit(0)
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)