设为首页 加入收藏

TOP

code block与VC++下相对路径写法
2014-02-08 13:36:26 来源: 作者: 【 】 浏览:140
Tags:code  block 相对 路径 写法
    最近想不借用超级宝典第五版的封装类自己完整写一个着色器,但是一直读取shader文件失败,原因在于相对路径的写法错误。
    codeblock 下如下写即可读取
    #include
    #include
    char *ReadText(char *fn);
    int main()
    {
    char *ff;
    ff=ReadText("first.vert");
    printf("%s\n",ff);
    return 0;
    }
    char *ReadText(char *fn)
    {
    FILE *fp;
    char *content = NULL;
    int count=0;
    if (fn != NULL)
    {
    fp = fopen(fn,"rt");
    if (fp != NULL)
    {
    fseek(fp, 0, SEEK_END);  // 重定位流(数据流/文件)上的文件内部位置指针
    // int fseek(FILE *stream, long offset, int fromwhere);
    count = ftell(fp);       // long ftell(FILE *stream); 返回当前文件指针,是int类型
    rewind(fp);  // void rewind(FILE *stream); 将文件内部的位置指针重新指向一个流(数据流/文件)的开头
    if (count > 0)
    {
    content = (char *)malloc(sizeof(char) * (count+1));  // extern void *malloc(unsigned int num_bytes);
    count = fread(content,sizeof(char),count,fp);  // 从一个流中读数据
    //函数原型: size_t fread( void *buffer, size_t size, size_t count, FILE *stream );
    //     buffer
    // Storage location for data.
    //
    // size
    // Item size in bytes.
    //
    // count
    // Maximum number of items to be read.
    //
    // stream
    // Pointer to FILE structure.
    content[count] = '\0';
    }
    fclose(fp);
    }
    }
    return content;
    }
    当前目录的写法为。
    ff=ReadText("first.vert");
    VC++2008则不同。
    当前目录的写法为
    ff=ReadText("/first.vert");
    源代码如下:
    #include
    #include
    char *ReadText(char *fn);
    int main()
    {
    char *ff;
    ff=ReadText("/first.vert");
    printf("%s\n",ff);
    getchar();
    return 0;
    }
    char *ReadText(char *fn)
    {
    FILE *fp;
    char *content = NULL;
    int count=0;
    if (fn != NULL)
    {
    fp = fopen(fn,"rt");
    if (fp != NULL)
    {
    fseek(fp, 0, SEEK_END);  // 重定位流(数据流/文件)上的文件内部位置指针
    // int fseek(FILE *stream, long offset, int fromwhere);
    count = ftell(fp);       // long ftell(FILE *stream); 返回当前文件指针,是int类型
    rewind(fp);  // void rewind(FILE *stream); 将文件内部的位置指针重新指向一个流(数据流/文件)的开头
    if (count > 0)
    {
    content = (char *)malloc(sizeof(char) * (count+1));  // extern void *malloc(unsigned int num_bytes);
    count = fread(content,sizeof(char),count,fp);  // 从一个流中读数据
    //函数原型: size_t fread( void *buffer, size_t size, size_t count, FILE *stream );
    //     buffer
    // Storage location for data.
    //
    // size
    // Item size in bytes.
    //
    // count
    // Maximum number of items to be read.
    //
    // stream
    // Pointer to FILE structure.
    content[count] = '\0';
    }
    fclose(fp);
    }
    }
    return content;
    }
    如果访问上层文件则一次后退即可。
    /name1/*.txt

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇C++使用libxml解析XML文件 下一篇C/C++大文件/数据网络传输方法

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·PostgreSQL 索引 - (2025-12-25 22:20:43)
·MySQL Node.js 连接 (2025-12-25 22:20:41)
·SQL 撤销索引、表以 (2025-12-25 22:20:38)
·Linux系统简介 (2025-12-25 21:55:25)
·Linux安装MySQL过程 (2025-12-25 21:55:22)