linux读取某一路径下的多个文件

软件和网站开发以及相关技术探讨
weihua2008
帖子: 448
注册时间: 2008-07-10 15:08

linux读取某一路径下的多个文件

#1

帖子 weihua2008 » 2008-12-27 20:59

比如说我在"/home/weihua/"路径下有test1.c test2.ctest3.c...
我如何逐一读取这几个文件那?
因为我想通过socket发送这些文件中的信息,必须逐一读取,然后读一句发送一句,现在的问题是。
若是又一个文件的话我可以设置路径为:"/home.weihua/test.c"
FILE *fp=fopen("/home/weihua/test.c","w");
现在不知道该撒办了
求助
头像
anticlockwise
帖子: 2394
注册时间: 2007-03-01 20:46
来自: 湖南长沙

Re: linux读取某一路径下的多个文件

#2

帖子 anticlockwise » 2008-12-27 23:19

不能用循环吗?
weihua2008
帖子: 448
注册时间: 2008-07-10 15:08

Re: linux读取某一路径下的多个文件

#3

帖子 weihua2008 » 2008-12-29 9:11

anticlockwise,
用循环是在你知道这些文件名的情况下或者说你知道要读取那些文件的情况下
现在假设我不知道该路径下又多少象.c的文件
我要把这些.c文件都读出来,该撒办?
即便是知道你又撒个循环法?
头像
HuntXu
帖子: 5776
注册时间: 2007-09-29 3:09

Re: linux读取某一路径下的多个文件

#4

帖子 HuntXu » 2008-12-29 13:08

open打开标准输入...
用cat把那些需要的文件连一起传给程序...
HUNT Unfortunately No Talent...
头像
anticlockwise
帖子: 2394
注册时间: 2007-03-01 20:46
来自: 湖南长沙

Re: linux读取某一路径下的多个文件

#5

帖子 anticlockwise » 2008-12-29 19:50

至少我知道在Python里边可以用:

代码: 全选

for f in glob.glob("/directory/*.c"):
    process(f)
这样就遍历了所有directory目录下的.c文件,如果我没有记错的话,C的某个库里边也有相应的东西~~
weihua2008
帖子: 448
注册时间: 2008-07-10 15:08

Re: linux读取某一路径下的多个文件

#6

帖子 weihua2008 » 2008-12-29 21:10

HuntXu,
你说的有点模糊啊,
可能是我的能力有限
我想的是要通过函数知道某一路径下有几个文件,各个文件的名字是啥?
然后逐一再对其操作
如果用cat的话,你或者输出到终端,或者传到指定的文件中吧
cat是不是还用很高深的用法,不妨再提示提示
头像
anticlockwise
帖子: 2394
注册时间: 2007-03-01 20:46
来自: 湖南长沙

Re: linux读取某一路径下的多个文件

#7

帖子 anticlockwise » 2008-12-29 22:41

HuntXu的意思是利用管道的方式将命令行上cat的输出导入到程序中去,所以可以用open()函数打开标准输入,用:

代码: 全选

cat `ls /directory/*.c`
这样的命令可以将/directory/这个目录下的所有.c文件连成一个大文件,读到程序中去,这样就可以一行一行的发送了
头像
xhy
帖子: 3916
注册时间: 2005-12-28 1:16
系统: Ubuntu 12.10 X64
来自: 火星

Re: linux读取某一路径下的多个文件

#8

帖子 xhy » 2008-12-30 1:23

APUE section 1.4


建议每一个UNIX/LINUX开发者都通读一遍APUE,否则寸步难行
目前负债150多万
babysoul
帖子: 75
注册时间: 2008-10-31 16:22
系统: Ubuntu 12.10

Re: linux读取某一路径下的多个文件

#9

帖子 babysoul » 2008-12-30 1:41

ls强,我来灌水。。
深切地缅怀Steven,这样算侵犯copyright不?
1.4. Files and Directories
File System

The UNIX file system is a hierarchical arrangement of directories and files. Everything starts in the directory called root whose name is the single character /.

A directory is a file that contains directory entries. Logically, we can think of each directory entry as containing a filename along with a structure of information describing the attributes of the file. The attributes of a file are such things as type of fileregular file, directorythe size of the file, the owner of the file, permissions for the filewhether other users may access this fileand when the file was last modified. The stat and fstat functions return a structure of information containing all the attributes of a file. In Chapter 4, we'll examine all the attributes of a file in great detail.

We make a distinction between the logical view of a directory entry and the way it is actually stored on disk. Most implementations of UNIX file systems don't store attributes in the directory entries themselves, because of the difficulty of keeping them in synch when a file has multiple hard links. This will become clear when we discuss hard links in Chapter 4.

Filename

The names in a directory are called filenames. The only two characters that cannot appear in a filename are the slash character (/) and the null character. The slash separates the filenames that form a pathname (described next) and the null character terminates a pathname. Nevertheless, it's good practice to restrict the characters in a filename to a subset of the normal printing characters. (We restrict the characters because if we use some of the shell's special characters in the filename, we have to use the shell's quoting mechanism to reference the filename, and this can get complicated.)

Two filenames are automatically created whenever a new directory is created: . (called dot) and .. (called dot-dot). Dot refers to the current directory, and dot-dot refers to the parent directory. In the root directory, dot-dot is the same as dot.

The Research UNIX System and some older UNIX System V file systems restricted a filename to 14 characters. BSD versions extended this limit to 255 characters. Today, almost all commercial UNIX file systems support at least 255-character filenames.
Pathname

A sequence of one or more filenames, separated by slashes and optionally starting with a slash, forms a pathname. A pathname that begins with a slash is called an absolute pathname; otherwise, it's called a relative pathname. Relative pathnames refer to files relative to the current directory. The name for the root of the file system (/) is a special-case absolute pathname that has no filename component.
Example

Listing the names of all the files in a directory is not difficult. Figure 1.3 shows a bare-bones implementation of the ls(1) command.

The notation ls(1) is the normal way to reference a particular entry in the UNIX system manuals. It refers to the entry for ls in Section 1. The sections are normally numbered 1 through 8, and all the entries within each section are arranged alphabetically. Throughout this text, we assume that you have a copy of the manuals for your UNIX system.

Historically, UNIX systems lumped all eight sections together into what was called the UNIX Programmer's Manual. As the page count increased, the trend changed to distributing the sections among separate manuals: one for users, one for programmers, and one for system administrators, for example.

Some UNIX systems further divide the manual pages within a given section, using an uppercase letter. For example, all the standard input/output (I/O) functions in AT&T [1990e] are indicated as being in Section 3S, as in fopen(3S). Other systems have replaced the numeric sections with alphabetic ones, such as C for commands.

Today, most manuals are distributed in electronic form. If your manuals are online, the way to see the manual pages for the ls command would be something like

man 1 ls


or

man -s1 ls


Figure 1.3 is a program that just prints the name of every file in a directory, and nothing else. If the source file is named myls.c, we compile it into the default a.out executable file by

cc myls.c


Historically, cc(1) is the C compiler. On systems with the GNU C compilation system, the C compiler is gcc(1). Here, cc is often linked to gcc.

Some sample output is

$ ./a.out /dev
.
..
console
tty
mem
kmem
null
mouse
stdin
stdout
stderr
zero
many more lines that aren't shown
cdrom
$ ./a.out /var/spool/cron
can't open /var/spool/cron: Permission denied
$ ./a.out /dev/tty
can't open /dev/tty: Not a directory


Throughout this text, we'll show commands that we run and the resulting output in this fashion: Characters that we type are shown in this font, whereas output from programs is shown like this. If we need to add comments to this output, we'll show the comments in italics. The dollar sign that precedes our input is the prompt that is printed by the shell. We'll always show the shell prompt as a dollar sign.

Note that the directory listing is not in alphabetical order. The ls command sorts the names before printing them.

There are many details to consider in this 20-line program.

*

First, we include a header of our own: apue.h. We include this header in almost every program in this text. This header includes some standard system headers and defines numerous constants and function prototypes that we use throughout the examples in the text. A listing of this header is in Appendix B.
*

The declaration of the main function uses the style supported by the ISO C standard. (We'll have more to say about the ISO C standard in the next chapter.)
*

We take an argument from the command line, argv[1], as the name of the directory to list. In Chapter 7, we'll look at how the main function is called and how the command-line arguments and environment variables are accessible to the program.
*

Because the actual format of directory entries varies from one UNIX system to another, we use the functions opendir, readdir, and closedir to manipulate the directory.
*

The opendir function returns a pointer to a DIR structure, and we pass this pointer to the readdir function. We don't care what's in the DIR structure. We then call readdir in a loop, to read each directory entry. The readdir function returns a pointer to a dirent structure or, when it's finished with the directory, a null pointer. All we examine in the dirent structure is the name of each directory entry (d_name). Using this name, we could then call the stat function (Section 4.2) to determine all the attributes of the file.
*

We call two functions of our own to handle the errors: err_sys and err_quit. We can see from the preceding output that the err_sys function prints an informative message describing what type of error was encountered ("Permission denied" or "Not a directory"). These two error functions are shown and described in Appendix B. We also talk more about error handling in Section 1.7.
*

When the program is done, it calls the function exit with an argument of 0. The function exit terminates a program. By convention, an argument of 0 means OK, and an argument between 1 and 255 means that an error occurred. In Section 8.5, we show how any program, such as a shell or a program that we write, can obtain the exit status of a program that it executes.

Figure 1.3. List all the files in a directory

#include "apue.h"
#include <dirent.h>

int
main(int argc, char *argv[])
{
DIR *dp;
struct dirent *dirp;

if (argc != 2)
err_quit("usage: ls directory_name");

if ((dp = opendir(argv[1])) == NULL)
err_sys("can't open %s", argv[1]);
while ((dirp = readdir(dp)) != NULL)
printf("%s\n", dirp->d_name);

closedir(dp);
exit(0);
}


Working Directory

Every process has a working directory, sometimes called the current working directory. This is the directory from which all relative pathnames are interpreted. A process can change its working directory with the chdir function.

For example, the relative pathname doc/memo/joe refers to the file or directory joe, in the directory memo, in the directory doc, which must be a directory within the working directory. From looking just at this pathname, we know that both doc and memo have to be directories, but we can't tell whether joe is a file or a directory. The pathname /usr/lib/lint is an absolute pathname that refers to the file or directory lint in the directory lib, in the directory usr, which is in the root directory.
Home Directory

When we log in, the working directory is set to our home directory. Our home directory is obtained from our entry in the password file (Section 1.3).
Hasee F545T: Core2 Duo T5450, 2G RAM, 320G HD, ATI HD2400 14.4, Archlinux
Lenovo ideapad Y580: i7 3630M, 8G RAM, 1T HD, nVidia GTX660M, Ubuntu 13.04
头像
anticlockwise
帖子: 2394
注册时间: 2007-03-01 20:46
来自: 湖南长沙

Re: linux读取某一路径下的多个文件

#10

帖子 anticlockwise » 2008-12-30 7:03

xhy 写了:APUE section 1.4


建议每一个UNIX/LINUX开发者都通读一遍APUE,否则寸步难行
对,好书~~
头像
percy
帖子: 508
注册时间: 2006-09-10 8:19
系统: Gentoo/Mac OS X
来自: Shanghai,China
联系:

Re: linux读取某一路径下的多个文件

#11

帖子 percy » 2009-01-01 0:41

用下面这个小程序可以对目录进行遍历。
有兴趣可以看一下。

代码: 全选


pjq@Gentoo-PJQ ~/workspace/c/list/src $ cat dir.c
/*************************************************************************
Author: pengjianqing@sina.com
Created Time: Sun 21 Dec 2008 11:26:07 PM CST
File Name: dir.c
Description: 
 ************************************************************************/
#include   <stdio.h>  
#include   <dirent.h>  
#include   <sys/types.h>  
#include   <sys/stat.h>  

void   dir_scan(char   *path,   char   *file);  
int   count   =   0;  

int   main(int argc, char *argv[])  
{  
  struct   stat   s;  
  char *path = argv[1];
  //char *path = "/home/pjq/workspace/c";
  if(argc   !=   2){  
    printf("one   direction   requried\n");  
    	exit(1);  
  }  
  if(lstat(path, &s) < 0)
  {  
    printf("lstat   error\n");  
    	exit(2);  
  }  
  if(!S_ISDIR(s.st_mode))
  {  
    printf("%s   is   not   a   direction   name\n",   argv[1]);  
  //  exit(3);  
  }  

  dir_scan("", path);  

  printf("total:   %d   files\n",   count);  

  exit(0);  
}  

void   dir_scan(char   *path,   char   *file)  
{  
  struct   stat   s;  
  DIR           *dir;  
  struct   dirent   *dt;  
  char   dirname[50];  

  memset(dirname,   0,   50*sizeof(char));  
  strcpy(dirname,   path);  

  if(lstat(file,   &s)   <   0)
  {  
    printf("lstat   error\n");  
  }  

  if(S_ISDIR(s.st_mode))
  {  
    strcpy(dirname+strlen(dirname),   file);  
    strcpy(dirname+strlen(dirname),   "/");  
    if((dir = opendir(file)) == NULL)
    {  
      printf("opendir   %s/%s   error\n");  
      exit(4);  
    }  
    if(chdir(file) < 0)   
    {  
      printf("chdir   error\n");  
      exit(5);  
    }  
    while((dt   =   readdir(dir))   !=   NULL)
    {  
      if(dt->d_name[0]   ==   '.')
      {  
	continue;  
      }  

      dir_scan(dirname,   dt->d_name);  
    }  
    if(chdir("..")   <   0)
    {  
      printf("chdir   error\n");  
      exit(6);  
    }  
  }
  else
  {  
    if(S_ISREG(s.st_mode))
    {
	printf("%s%s size:%ld bytes\tmodified at %s", dirname,file,s.st_size,ctime(&s.st_mtime)); 
    }
    //printf("%s%s\n",   dirname,   file);  
    count++;  
  }  
}

weihua2008
帖子: 448
注册时间: 2008-07-10 15:08

Re: linux读取某一路径下的多个文件

#12

帖子 weihua2008 » 2009-01-01 7:05

谢谢,正在拜读
flyinflash
帖子: 2376
注册时间: 2006-09-21 14:28

Re: linux读取某一路径下的多个文件

#13

帖子 flyinflash » 2009-01-12 13:15

用 shell 不行吗?
weihua2008
帖子: 448
注册时间: 2008-07-10 15:08

Re: linux读取某一路径下的多个文件

#14

帖子 weihua2008 » 2009-04-08 16:09

percy,
weihua2008
帖子: 448
注册时间: 2008-07-10 15:08

Re: linux读取某一路径下的多个文件

#15

帖子 weihua2008 » 2009-04-08 16:11

percy, 当初你给我的这个程序,只是遍历了所有的文件,而没有指出空文件,我现在想找空文件萨找
回复