code block与VC++下相对路径的不同写法

最近想不借用超级宝典第五版的封装类自己完整写一个着色器,但是一直读取shader文件失败,原因在于相对路径的写法错误。

codeblock 下如下写即可读取


#include <stdio.h>
#include <stdlib.h>

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 <stdio.h>
#include <stdlib.h>

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

更进一步请看如下两篇帖子:

http://blog.csdn.net/sszgg2006/article/details/8447176
http://bbs.csdn.net/topics/80326375


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值