文件指针不能设置为全局变量?

[复制链接]
查看11 | 回复3 | 2010-7-29 11:56:06 | 显示全部楼层 |阅读模式
FILE *in,*out;
in = fopen("in.txt","r");
out = fopen("out.txt","w");
我将以上放在main() 外面
结果提示:
error C2040: 'in' : 'int ' differs in levels of indirection from 'struct _iobuf *'
error C2099: initializer is not a constant
error C2040: 'out' : 'int ' differs in levels of indirection from 'struct _iobuf *'
error C2099: initializer is not a constant
放在里面也试过了,还是错
以下是完整代码:
#define LEN sizeof(struct tree)
#include
struct tree
{
char name[30];
char attr[100];
struct tree *left,*right,*lup,*rup;
};

FILE *in,*out;
void write(struct tree *p,int n)
{
int i;
for (i=0; iname,p->attr);
if (p->right!=0) wirte(p->right,n+1);

else write(p->left,n);
}
main()
{

char s;
int k,j;

struct tree *q,*p,*p1,*root;

in = fopen("in.txt","r");

out = fopen("out.txt","w");
root = p = (struct tree*) malloc(LEN);
p->left=0; p->right=0; p->lup=0;p->rup=0;
s = fgetc(in);

while (s!=EOF)
{中间这段可以忽略 跟输出完全无关,字数有限就不发了}
write (root,0);
fclose(in);
fclose(out);
}
这是错误提示:lab2_2.obj : error LNK2001: unresolved external symbol _wirte
Debug/lab2_2.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.
用的VC6.0 编译,TURBO C也错

回复

使用道具 举报

千问 | 2010-7-29 11:56:06 | 显示全部楼层
wirte 函数你写错了吧 writemalloc 函数要求#include 头文件,你的代码也没有,代码我编译了一下,是没有问题的,与全局变量没有关系
回复

使用道具 举报

千问 | 2010-7-29 11:56:06 | 显示全部楼层
不会啊。除非你在main 里又重新声明 in,out, 把它们另做别用,否则它们是全局量 FILE *in,*out;#include#includeFILE *in,*out;void main(){// .....in = fopen("in.txt","r");out
回复

使用道具 举报

千问 | 2010-7-29 11:56:06 | 显示全部楼层
把in = fopen("in.txt","r"); out = fopen("out.txt","w");放在main()内部试试
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行