关于c语言文件操作

wzy43 2013-08-01 04:27:23
对于一个目录,之下有20个文件,我要用这个*fp【20】,来分别指向这些文件,例如fp【0】指向1.txt,fp【1】指向2.txt,这个可以实现吗?怎么实现,急求
...全文
97 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
wzy43 2013-08-01
  • 打赏
  • 举报
回复
我的想法是可以任意选择*fp【n】来指向需要操作的文件,6楼用链表保存的方法固然很好,然而做不到任意选取两个文件进行操作,所以只能使用文件数组指针来做吧。 例如我需要读入8.txt和15.txt两个文件则用*fp【8】和*fp【15】来指向这两个文件,当然这里只是举个例子
  • 打赏
  • 举报
回复
遍历目录下的所有文件 findfirst findnext 具体写法可以自己研究。
赵4老师 2013-08-01
  • 打赏
  • 举报
回复
仅供参考
//将c:\\tmp文件夹下的所有文件的内容全部放到用malloc分配的内存中
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <io.h>
struct FB {
    char fn[256];
    size_t fl;
    char *b;
    struct FB *next;
    struct FB *prev;
} *fh,*fb,*ft;
char ln[256];
char fpn[256];
FILE *af;
FILE *f;
int L,n;
int main() {
    system("dir /b /a-d c:\\tmp\\*.* >c:\\allfn.txt");
    af=fopen("c:\\allfn.txt","r");
    if (NULL==af) {
        printf("Can not open file c:\\allfn.txt!\n");
        return 1;
    }
    fh=NULL;
    fb=NULL;
    n=0;
    while (1) {
        if (NULL==fgets(ln,256,af)) break;
        L=strlen(ln);
        if ('\n'==ln[L-1]) ln[L-1]=0;
        printf("read %s\n",ln);
        strcpy(fpn,"c:\\tmp\\");
        strcat(fpn,ln);
        ft=(struct FB *)malloc(sizeof(struct FB));
        if (NULL==ft) {
            printf("Can not malloc ft!\n");
            fclose(af);
            return 2;//之前的malloc在main退出后由操作系统自动free
        }
        printf("ft[%d]==%p\n",n,ft);
        strcpy(ft->fn,fpn);
        f=fopen(fpn,"rb");
        if (NULL==f) {
            printf("Can not open file %s!\n",fpn);
            fclose(af);
            return 3;//之前的malloc在main退出后由操作系统自动free
        }
        ft->fl=_filelength(fileno(f));
        ft->b=malloc(ft->fl);
        if (NULL==ft->b) {
            printf("Can not malloc ft->b!\n");
            fclose(f);
            fclose(af);
            return 4;//之前的malloc在main退出后由操作系统自动free
        }
        printf("ft[%d]->b==%p\n",n,ft->b);
        if (ft->fl!=fread(ft->b,1,ft->fl,f)) {
            printf("fread error!\n");
            fclose(f);
            fclose(af);
            return 5;//之前的malloc在main退出后由操作系统自动free
        }
        fclose(f);
        ft->next=NULL;

        if (NULL==fh) {
            ft->prev=NULL;
            fh=ft;
        } else {
            fb->next=ft;
            ft->prev=fb;
        }
        fb=ft;
        n++;
    }
    fclose(af);
    printf("-----list-----\n");
    for (ft=fh;NULL!=ft;ft=ft->next) {
        printf("%8d %s\n",ft->fl,ft->fn);
        if (NULL!=ft) fb=ft;
    }
    printf("-----free-----\n");
    n--;
    if (NULL!=fh) {
        for (ft=fb->prev;NULL!=ft;ft=ft->prev) {
            if (NULL!=ft->next->b) {
                printf("ft[%d]->b==%p\n",n,ft->next->b);
                free(ft->next->b);
            }
            if (NULL!=ft->next) {
                printf("ft[%d]==%p\n",n,ft->next);
                free(ft->next);
            }
            n--;
        }
        if (NULL!=fh->b) {
            printf("ft[0]->b==%p\n",fh->b);
            free(fh->b);
        }
        printf("ft[0]==%p\n",fh);
        free(fh);
    }
    return 0;
}
//C:\tmp\tmp\Debug>dir /a-d c:\tmp
// 驱动器 C 中的卷是 C_HD5_1
// 卷的序列号是 1817-D526
//
// c:\tmp 的目录
//
//找不到文件
//
//C:\tmp\tmp\Debug>tmp
//找不到文件
//-----list-----
//-----free-----
//
//C:\tmp\tmp\Debug>dir /a-d c:\tmp
// 驱动器 C 中的卷是 C_HD5_1
// 卷的序列号是 1817-D526
//
// c:\tmp 的目录
//
//2011-06-30  18:04            44,840 my_c.rar
//2011-06-30  17:18             1,036 err.frm
//2011-06-30  14:32            14,243 出租.txt
//2011-06-28  12:08            23,681 MSDN98书签.txt
//             4 个文件         83,800 字节
//             0 个目录 17,041,870,848 可用字节
//
//C:\tmp\tmp\Debug>tmp
//read my_c.rar
//ft[0]==00421800
//ft[0]->b==00520068
//read err.frm
//ft[1]==00421670
//ft[1]->b==0052AFC0
//read 出租.txt
//ft[2]==00421530
//ft[2]->b==00378F28
//read MSDN98书签.txt
//ft[3]==004213F0
//ft[3]->b==0052B3F8
//-----list-----
// 44840 c:\tmp\my_c.rar
//  1036 c:\tmp\err.frm
// 14243 c:\tmp\出租.txt
// 23681 c:\tmp\MSDN98书签.txt
//-----free-----
//ft[3]->b==0052B3F8
//ft[3]==004213F0
//ft[2]->b==00378F28
//ft[2]==00421530
//ft[1]->b==0052AFC0
//ft[1]==00421670
//ft[0]->b==00520068
//ft[0]==00421800
//
//C:\tmp\tmp\Debug>
赵4老师 2013-08-01
  • 打赏
  • 举报
回复
system("dir /b /a-d c:\\*.* >d:\\allfiles.txt"); //读文件d:\\allfiles.txt的内容即C:\\下所有文件的名字 system("dir /b /a-d /s c:\\*.* >d:\\allfilesinsub.txt"); //读文件d:\\allfilesinsub.txt的内容即C:\\下所有文件的名字包含子目录 system("dir /b /ad c:\\*.* >d:\\alldirs.txt"); //读文件d:\\alldirs.txt的内容即C:\\下所有子目录的名字 请记住,能用shell命令获取文件、文件夹信息或者操作文件、文件夹最好用shell命令获取或者操作,而不要用各种API获取或者操作,因为当遇到非法文件夹名或非法文件名或非法文件长度、非法文件日期、压缩文件、链接文件、稀疏文件……等各种意料之外的情况时,API会处理的不全面或陷入死循环,而shell命令不会。
wzy43 2013-08-01
  • 打赏
  • 举报
回复
我用了readdir,然后不知道怎么办了
wzy43 2013-08-01
  • 打赏
  • 举报
回复
哦,不是这个意思,是要先读入一个目录下的文件,然后再用fp指向
max_min_ 2013-08-01
  • 打赏
  • 举报
回复

完全可以的。


引用 1 楼 turingo 的回复:
完全可以的啊:

FILE* fp[20];
fp[0] = fopen("0.txt", "rt");
fp[1] = fopen("1.txt", "rt");
...
图灵狗 2013-08-01
  • 打赏
  • 举报
回复
完全可以的啊:

FILE* fp[20];
fp[0] = fopen("0.txt", "rt");
fp[1] = fopen("1.txt", "rt");
...

64,637

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧