C++中使用<

在C++中,>>operator可以读取不同的基本类型,但是对于endl其不能识别,这会影响到如下文件的读取。

#systat stream tcp nowait nobody /usr/sbin/tcpd /bin/ps -auwwx
netstat stream tcp nowait root /bin/netstat /bin/netstat -a
#ident stream tcp nowait root /usr/sbin/in.identd in.identd

from busybox inetd.conf.

使用ws manupulator 不能识别endl因为空格('')、定位字符('/t')、CR('/r')、换行('/n')、垂直定位字符('/v')或翻页('/f')都被过滤。

使用noskipws 会导致文件读取出错。

考虑编写自己的ignoreSpace 和 ignoreline.  其中ignoreline拷贝自 C++ Standard Library, The: A Tutorial and Reference 用于忽略注释行。

#include
#include
#include
using namespace std;

istream&
ignoreSpace (istream& strm)
{
 // skip until end-of-line
 istream::int_type a;
 a = strm.peek();
 while ( ! strm.eof()) {
  if ( a == 0x0a ) {
  break;
  }
  if ( a == 0x20 || a == 0x0d || a == 0x09 || a == 0x08) {
  strm.ignore();
  }
  else {
  break;
  }
  a = strm.peek(); 
 }
 
 
 // return stream for concatenation
 return strm;
}

template
std::basic_istream&
ignoreLine (std::basic_istream& strm)
{
  // skip until end-of-line
  strm.ignore(std::numeric_limits::max(),strm.widen('/n'));

  // return stream for concatenation
  return strm;
}

int main (int argc, char* argv[])
{
  ifstream file;
  string dump;
  ifstream::int_type a;

  // for all command-line arguments
  for (int i=1; i
  // open file
  file.open(argv[i],std::ios::binary);
// file >> noskipws;
  while (file.good()){
// file >> noskipws;
  file >> dump;
// if ( !file ) break;
  if ( dump[0] == '#' ) {
  file >> ignoreLine;
  a = file.peek();
// if ( !file ) break;
  continue;
  }
  cout << dump << " " << flush;
  file >> ignoreSpace;
// if ( !file ) break;
  a = file.peek();
// if ( !file ) break;
  if (a == 0x0a) 
  {
// file.ignore();
  cout << endl;
  }
   
 }
  file.clear();

  file.close();
  }
 cout << "file end " << endl;
}
执行

cat1 inetd.conf

可以获得如下结果:

echo stream tcp nowait root internal
echo dgram udp wait root internal
daytime stream tcp nowait root internal
daytime dgram udp wait root internal
time stream tcp nowait root internal
time dgram udp wait root internal
netstat stream tcp nowait root /bin/netstat /bin/netstat -a
file end

inetd.conf 内容如下:

# /etc/inetd.conf:  
#see inetd(8) for further informations.
#
# Internet server configuration database
#
#
# If you want to disable an entry so it isn't touched during
# package updates just comment it out with a single '#' character.
#
# If you make changes to this file, either reboot your machine or
# send the inetd process a HUP signal:
# Do a "ps x" as root and look up the pid of inetd. Then do a
# kill -HUP
# inetd will re-read this file whenever it gets that signal.
#
#
#:INTERNAL: Internal services
# It is generally considered safer to keep these off.
 echo stream tcp nowait root internal 
echo dgram udp wait root internal
#discard stream tcp nowait root internal  
#discard dgram udp wait root internal
daytime stream tcp nowait root internal
daytime dgram udp wait root internal
#chargen stream tcp nowait root internal
#chargen dgram udp wait root internal
time stream tcp nowait root internal
time dgram udp wait root internal

# These are standard services.
#
#ftp stream tcp nowait root /usr/sbin/tcpd in.ftpd
#telnet stream tcp nowait root /sbin/telnetd /sbin/telnetd
#nntp stream tcp nowait root tcpd in.nntpd
#smtp stream tcp nowait root tcpd sendmail -v
#
# Shell, login, exec and talk are BSD protocols.
#
# If you run an ntalk daemon (such as netkit-ntalk) on the old talk
# port, that is, "talk" as opposed to "ntalk", it won't work and may
# cause certain broken talk clients to malfunction.
#
# The talkd from netkit-ntalk 0.12 and higher, however, can speak the
# old talk protocol and can be used safely.
#
#shell stream tcp nowait root /usr/sbin/tcpd in.rshd -L
#login stream tcp nowait root /usr/sbin/tcpd in.rlogind -L
#exec stream tcp nowait root /usr/sbin/tcpd in.rexecd
#talk dgram udp wait root /usr/sbin/tcpd in.talkd
#ntalk dgram udp wait root /usr/sbin/tcpd in.talkd
#
# Pop et al
# Leave these off unless you're using them.
#pop2 stream tcp nowait root /usr/sbin/tcpd in.pop2d
#pop3 stream tcp nowait root /usr/sbin/tcpd in.pop3d
#
# The Internet UUCP service.
# uucp stream tcp nowait uucp /usr/sbin/tcpd /usr/lib/uucp/uucico -l
#
# Tftp service is provided primarily for booting. Most sites
# run this only on machines acting as "boot servers." If you don't
# need it, don't use it.
#
#tftp dgram udp wait nobody /usr/sbin/tcpd in.tftpd
#bootps dgram udp wait root /usr/sbin/in.bootpd in.bootpd
#
# Finger, systat and netstat give out user information which may be
# valuable to potential "system crackers." Many sites choose to disable
# some or all of these services to improve security.
#
#finger stream tcp nowait nobody /usr/sbin/tcpd in.fingerd -w
#systat stream tcp nowait nobody /usr/sbin/tcpd /bin/ps -auwwx
netstat stream tcp nowait root /bin/netstat /bin/netstat -a
#ident stream tcp nowait root /usr/sbin/in.identd in.identd




你可能感兴趣的:(CPlusPlus)