69,373
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
char* filename[100];
int len=0;
int readData(int fd,char *buf)
{
return read(fd,buf,676);
}
void getAllFileName()
{
int j;
for(j=0;j<100;j++)
{
filename[j]=(char*)malloc(sizeof(char)*100);
}
int i=0;
int fd=open("e:/file.txt",O_RDONLY);
if(fd<0){
printf("file open failed.\n");
return;
}
char *buf=(char*)malloc(sizeof(char)*1024);
readData(fd,buf);
printf("buf:\n%s\n--------\n",buf);
char *temp=NULL;
while((temp=strtok(buf,"\n"))!=NULL){
temp=filename[len++];
}
close(fd);
}
int main()
{
getAllFileName();
int i=0;
for(;i<len;i++){
printf("%s\n",filename[i]);
}
for(i=0;i<100;i++){
free(filename[i]);
}
system("pause");
return 0;
}
char *tmp=NULL;
tmp = strtok(buf, "\n");
while (tmp != NULL)
{
int k;
buf = NULL; // 要置为NULL
printf("file name: %s\n", tmp); // 可以正确打印文件名称
//filename[k++] = tmp;
//printf("%s\n", filename[k++]); //这里打印不了。
tmp = strtok(buf, "\n");
}
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#define NUM 60
void getAllFileName()
{
int fd=open("e:/file.txt",O_RDONLY);
if(fd<0){
printf("file open failed.\n");
return;
}
char *buf=(char*)malloc(sizeof(char)*100);
int count=0;
while(count!=-1&&(count=read(fd,buf,NUM)))
{
printf("count=%d\nbuf=\n%s\n",count,buf);
}
free(buf);
close(fd);
}
int main()
{
getAllFileName();
system("pause");
return 0;
}