C 正确打印输出
我在做这个C 正确打印输出,c,C,我在做这个 ls -l | myprogram 基本上我想以正确的方式打印输出。当我们单独使用ls-l时,可以有许多行。每行包含8个元素。因此,我想以一种方式打印它ls-lgive output,但跳过顶部的两个元素(总数)。例如,ls-l为我们提供 total 3 -rwx------ 1 cre university 8381 Sep 24 21:04 a.out drwx------ 5 cre university 4096 Sep 16 19:36 file2 -rwx------
ls -l | myprogram
基本上我想以正确的方式打印输出。当我们单独使用ls-l
时,可以有许多行。每行包含8个元素。因此,我想以一种方式打印它ls-l
give output,但跳过顶部的两个元素(总数)。例如,ls-l
为我们提供
total 3
-rwx------ 1 cre university 8381 Sep 24 21:04 a.out
drwx------ 5 cre university 4096 Sep 16 19:36 file2
-rwx------ 1 cre university 8499 Sep 25 00:50 file1
我需要ls-l|myprogram
:
-rwx------ 1 cre university 8381 Sep 24 21:04 a.out
drwx------ 5 cre university 4096 Sep 16 19:36 file2
-rwx------ 1 cre university 8499 Sep 25 00:50 file1
这就是我到目前为止所做的。它只需在一行中打印所有元素。
当零件与8个元素对齐时,不知道如何实现零件。之后,我想比较文件的大小。请帮助,这里的初学者 您可能可以在输入流中找到“\n\n”,即空行。从那里您将获得输入流的其余内容。在do while循环中,您可以尝试向printf语句添加“\n”,如下所示:
do {
result=scanf("%s",string);
printf("%s\n", string);
} while (result!=EOF);
但是,do/while不是很安全-请参阅下面的代码
如果需要跳过“总计X”行,可以采用以下两种方法之一:
知道它是第一行并跳过它,或者对“total X”进行字符串比较,然后跳过它。我可能会选择后面的选项,因为它将在那里——当然,除非ls很快改变。为了完全安全,正则表达式可能是最安全的,但实施时间更长
这里有一个例子。注意,我将它从do/while改为do。这是因为在使用之前需要检查result==EOF,而上面的do/while并没有给您这样做的机会。这段时间比较安全
while (EOF != (result = scanf("%s", string))) {
// skip line containing "total X"
if (strncmp(result, "total ", sizeof("total ")) == 0) {
continue;
}
printf("%s\n", string);
}
免责声明-我还没有编译这个,所以不太确定sizeof(“total”)在语法上是否正确,但这个想法应该是正确的
希望这对你有所帮助 虽然
“ls-l | tail-n+2”
这是个好答案。如果你想用你的代码来做这件事,只需检查行的开头
您可能希望跳过某些行,即与模式匹配的行。使用能够很好地处理正则表达式的语言(例如:awk、perl、ruby、python等)可以更好地实现这一点 您可以读取每一行,但可以匹配并跳过某些容易检测到模式的行,例如以“total”开头的行、空行以及“.”和“.”特殊目录
linebuffer[1024];
while( fgets(linbuffer,sizeof(linebuffer),stdin) ) {
if( strlen(linebuffer) < 2 ) continue; //short lines
if( strncmp(linebuffer,"total",strlen("total")==0 ) continue; //line begins with "total"
//do you want to skip ".", ".."?
if( strcmp(linebuffer+strlen(linebuffer)-2,".\n")==0 ) continue; //skip ".", ".."
printf("%s",linebuffer); //or use puts
}
您需要解码并打印统计(2)信息
int
print_fileinfo(stat* statbuf, char* name)
{
//decode rwx bits for owner, group, and other
//lookup owner and group names
//file size is part of statbuf
//file type (regular, directory, et al) is part of statbuf
}
您需要查看stat(2)手册页,但这里有一些有用的值、宏、
All of these system calls return a stat structure, which contains the following fields:
struct stat {
dev_t st_dev; /* ID of device containing file */
ino_t st_ino; /* inode number */
mode_t st_mode; /* protection */
nlink_t st_nlink; /* number of hard links */
uid_t st_uid; /* user ID of owner */
gid_t st_gid; /* group ID of owner */
dev_t st_rdev; /* device ID (if special file) */
off_t st_size; /* total size, in bytes */
blksize_t st_blksize; /* blocksize for file system I/O */
blkcnt_t st_blocks; /* number of 512B blocks allocated */
time_t st_atime; /* time of last access */
time_t st_mtime; /* time of last modification */
time_t st_ctime; /* time of last status change */
};
The following POSIX macros are defined to check the file type using the st_mode field:
S_ISREG(m) is it a regular file?
S_ISDIR(m) directory?
S_ISCHR(m) character device?
S_ISBLK(m) block device?
S_ISFIFO(m) FIFO (named pipe)?
S_ISLNK(m) symbolic link? (Not in POSIX.1-1996.)
S_ISSOCK(m) socket? (Not in POSIX.1-1996.)
The following flags are defined for the st_mode field:
S_IFMT 0170000 bit mask for the file type bit fields
S_IFSOCK 0140000 socket
S_IFLNK 0120000 symbolic link
S_IFREG 0100000 regular file
S_IFBLK 0060000 block device
S_IFDIR 0040000 directory
S_IFCHR 0020000 character device
S_IFIFO 0010000 FIFO
S_ISUID 0004000 set UID bit
S_ISGID 0002000 set-group-ID bit (see below)
S_ISVTX 0001000 sticky bit (see below)
S_IRWXU 00700 mask for file owner permissions
S_IRUSR 00400 owner has read permission
S_IWUSR 00200 owner has write permission
S_IXUSR 00100 owner has execute permission
S_IRWXG 00070 mask for group permissions
S_IRGRP 00040 group has read permission
S_IWGRP 00020 group has write permission
S_IXGRP 00010 group has execute permission
S_IRWXO 00007 mask for permissions for others (not in group)
S_IROTH 00004 others have read permission
S_IWOTH 00002 others have write permission
S_IXOTH 00001 others have execute permission
不是直接回答,但您是否尝试过
ls-l | tail-n+2
?您的预期输出是什么?你不是那样的clear@SukkoPerased'1d'
也做同样的事情。然而,在这种情况下,我会投票支持tail
Sure@starrify,我相信至少还有十几种方法可以做同样的事情:)。
int
print_fileinfo(stat* statbuf, char* name)
{
//decode rwx bits for owner, group, and other
//lookup owner and group names
//file size is part of statbuf
//file type (regular, directory, et al) is part of statbuf
}
All of these system calls return a stat structure, which contains the following fields:
struct stat {
dev_t st_dev; /* ID of device containing file */
ino_t st_ino; /* inode number */
mode_t st_mode; /* protection */
nlink_t st_nlink; /* number of hard links */
uid_t st_uid; /* user ID of owner */
gid_t st_gid; /* group ID of owner */
dev_t st_rdev; /* device ID (if special file) */
off_t st_size; /* total size, in bytes */
blksize_t st_blksize; /* blocksize for file system I/O */
blkcnt_t st_blocks; /* number of 512B blocks allocated */
time_t st_atime; /* time of last access */
time_t st_mtime; /* time of last modification */
time_t st_ctime; /* time of last status change */
};
The following POSIX macros are defined to check the file type using the st_mode field:
S_ISREG(m) is it a regular file?
S_ISDIR(m) directory?
S_ISCHR(m) character device?
S_ISBLK(m) block device?
S_ISFIFO(m) FIFO (named pipe)?
S_ISLNK(m) symbolic link? (Not in POSIX.1-1996.)
S_ISSOCK(m) socket? (Not in POSIX.1-1996.)
The following flags are defined for the st_mode field:
S_IFMT 0170000 bit mask for the file type bit fields
S_IFSOCK 0140000 socket
S_IFLNK 0120000 symbolic link
S_IFREG 0100000 regular file
S_IFBLK 0060000 block device
S_IFDIR 0040000 directory
S_IFCHR 0020000 character device
S_IFIFO 0010000 FIFO
S_ISUID 0004000 set UID bit
S_ISGID 0002000 set-group-ID bit (see below)
S_ISVTX 0001000 sticky bit (see below)
S_IRWXU 00700 mask for file owner permissions
S_IRUSR 00400 owner has read permission
S_IWUSR 00200 owner has write permission
S_IXUSR 00100 owner has execute permission
S_IRWXG 00070 mask for group permissions
S_IRGRP 00040 group has read permission
S_IWGRP 00020 group has write permission
S_IXGRP 00010 group has execute permission
S_IRWXO 00007 mask for permissions for others (not in group)
S_IROTH 00004 others have read permission
S_IWOTH 00002 others have write permission
S_IXOTH 00001 others have execute permission