当前时区为 UTC + 8 小时



发表新帖 回复这个主题  [ 20 篇帖子 ]  前往页数 1, 2  下一页
作者 内容
1 楼 
 文章标题 : linux读取某一路径下的多个文件
帖子发表于 : 2008-12-27 20:59 

注册: 2008-07-10 15:08
帖子: 448
送出感谢: 0 次
接收感谢: 0 次
比如说我在"/home/weihua/"路径下有test1.c test2.ctest3.c...
我如何逐一读取这几个文件那?
因为我想通过socket发送这些文件中的信息,必须逐一读取,然后读一句发送一句,现在的问题是。
若是又一个文件的话我可以设置路径为:"/home.weihua/test.c"
FILE *fp=fopen("/home/weihua/test.c","w");
现在不知道该撒办了
求助


页首
 用户资料  
 
2 楼 
 文章标题 : Re: linux读取某一路径下的多个文件
帖子发表于 : 2008-12-27 23:19 
头像

注册: 2007-03-01 20:46
帖子: 2393
地址: 湖南长沙
送出感谢: 0 次
接收感谢: 0 次
不能用循环吗?


页首
 用户资料  
 
3 楼 
 文章标题 : Re: linux读取某一路径下的多个文件
帖子发表于 : 2008-12-29 9:11 

注册: 2008-07-10 15:08
帖子: 448
送出感谢: 0 次
接收感谢: 0 次
anticlockwise,
用循环是在你知道这些文件名的情况下或者说你知道要读取那些文件的情况下
现在假设我不知道该路径下又多少象.c的文件
我要把这些.c文件都读出来,该撒办?
即便是知道你又撒个循环法?


页首
 用户资料  
 
4 楼 
 文章标题 : Re: linux读取某一路径下的多个文件
帖子发表于 : 2008-12-29 13:08 
头像

注册: 2007-09-29 3:09
帖子: 5773
送出感谢: 0 次
接收感谢: 3
open打开标准输入...
用cat把那些需要的文件连一起传给程序...


_________________
HUNT Unfortunately No Talent...


页首
 用户资料  
 
5 楼 
 文章标题 : Re: linux读取某一路径下的多个文件
帖子发表于 : 2008-12-29 19:50 
头像

注册: 2007-03-01 20:46
帖子: 2393
地址: 湖南长沙
送出感谢: 0 次
接收感谢: 0 次
至少我知道在Python里边可以用:
代码:
for f in glob.glob("/directory/*.c"):
    process(f)


这样就遍历了所有directory目录下的.c文件,如果我没有记错的话,C的某个库里边也有相应的东西~~


页首
 用户资料  
 
6 楼 
 文章标题 : Re: linux读取某一路径下的多个文件
帖子发表于 : 2008-12-29 21:10 

注册: 2008-07-10 15:08
帖子: 448
送出感谢: 0 次
接收感谢: 0 次
HuntXu,
你说的有点模糊啊,
可能是我的能力有限
我想的是要通过函数知道某一路径下有几个文件,各个文件的名字是啥?
然后逐一再对其操作
如果用cat的话,你或者输出到终端,或者传到指定的文件中吧
cat是不是还用很高深的用法,不妨再提示提示


页首
 用户资料  
 
7 楼 
 文章标题 : Re: linux读取某一路径下的多个文件
帖子发表于 : 2008-12-29 22:41 
头像

注册: 2007-03-01 20:46
帖子: 2393
地址: 湖南长沙
送出感谢: 0 次
接收感谢: 0 次
HuntXu的意思是利用管道的方式将命令行上cat的输出导入到程序中去,所以可以用open()函数打开标准输入,用:
代码:
cat `ls /directory/*.c`


这样的命令可以将/directory/这个目录下的所有.c文件连成一个大文件,读到程序中去,这样就可以一行一行的发送了


页首
 用户资料  
 
8 楼 
 文章标题 : Re: linux读取某一路径下的多个文件
帖子发表于 : 2008-12-30 1:23 
头像

注册: 2005-12-28 1:16
帖子: 3916
地址: 火星
系统: Ubuntu 12.10 X64
送出感谢: 1
接收感谢: 0 次
APUE section 1.4


建议每一个UNIX/LINUX开发者都通读一遍APUE,否则寸步难行


_________________
目前负债150多万


页首
 用户资料  
 
9 楼 
 文章标题 : Re: linux读取某一路径下的多个文件
帖子发表于 : 2008-12-30 1:41 

注册: 2008-10-31 16:22
帖子: 75
系统: Ubuntu 12.10
送出感谢: 0 次
接收感谢: 0 次
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


页首
 用户资料  
 
10 楼 
 文章标题 : Re: linux读取某一路径下的多个文件
帖子发表于 : 2008-12-30 7:03 
头像

注册: 2007-03-01 20:46
帖子: 2393
地址: 湖南长沙
送出感谢: 0 次
接收感谢: 0 次
xhy 写道:
APUE section 1.4


建议每一个UNIX/LINUX开发者都通读一遍APUE,否则寸步难行


对,好书~~


页首
 用户资料  
 
11 楼 
 文章标题 : Re: linux读取某一路径下的多个文件
帖子发表于 : 2009-01-01 0:41 
头像

注册: 2006-09-10 8:19
帖子: 508
地址: Shanghai,China
系统: Gentoo/Mac OS X
送出感谢: 0 次
接收感谢: 0 次
用下面这个小程序可以对目录进行遍历。
有兴趣可以看一下。
代码:

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++; 
  } 
}



_________________
http://pjq.me


页首
 用户资料  
 
12 楼 
 文章标题 : Re: linux读取某一路径下的多个文件
帖子发表于 : 2009-01-01 7:05 

注册: 2008-07-10 15:08
帖子: 448
送出感谢: 0 次
接收感谢: 0 次
谢谢,正在拜读


页首
 用户资料  
 
13 楼 
 文章标题 : Re: linux读取某一路径下的多个文件
帖子发表于 : 2009-01-12 13:15 
头像

注册: 2006-09-21 14:28
帖子: 2376
送出感谢: 0 次
接收感谢: 0 次
用 shell 不行吗?


_________________
http://lee.youxu.info/


页首
 用户资料  
 
14 楼 
 文章标题 : Re: linux读取某一路径下的多个文件
帖子发表于 : 2009-04-08 16:09 

注册: 2008-07-10 15:08
帖子: 448
送出感谢: 0 次
接收感谢: 0 次
percy,


页首
 用户资料  
 
15 楼 
 文章标题 : Re: linux读取某一路径下的多个文件
帖子发表于 : 2009-04-08 16:11 

注册: 2008-07-10 15:08
帖子: 448
送出感谢: 0 次
接收感谢: 0 次
percy, 当初你给我的这个程序,只是遍历了所有的文件,而没有指出空文件,我现在想找空文件萨找


页首
 用户资料  
 
显示帖子 :  排序  
发表新帖 回复这个主题  [ 20 篇帖子 ]  前往页数 1, 2  下一页

当前时区为 UTC + 8 小时


在线用户

正在浏览此版面的用户:没有注册用户 和 4 位游客


不能 在这个版面发表主题
不能 在这个版面回复主题
不能 在这个版面编辑帖子
不能 在这个版面删除帖子
不能 在这个版面提交附件

前往 :  
本站点为公益性站点,用于推广开源自由软件,由 DiaHosting VPSBudgetVM VPS 提供服务。
我们认为:软件应可免费取得,软件工具在各种语言环境下皆可使用,且不会有任何功能上的差异;
人们应有定制和修改软件的自由,且方式不受限制,只要他们自认为合适。

Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
简体中文语系由 王笑宇 翻译