首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

请问fgetpos函数的几个小问题

2013-11-11 
请教fgetpos函数的几个问题#include stdio.h#include stdlib.hchar buf[132]int main(int argc, char

请教fgetpos函数的几个问题

#include <stdio.h>
#include <stdlib.h>
char buf[132];

int main(int argc, char *argv[])
{
FILE *fp;
fpos_t pos;
if(argc!=2)
{
printf("usage: %s mode \n",argv[0]);
return 1;
}
if(argv[1][0]!='a')
{
if((fp=fopen("testfile","w+"))==NULL)
{
printf("fopen failed!\n");
return 2;
}
}
else
{
if((fp=fopen("testfile","a+"))==NULL)
{
printf("fopen failed!\n");
return 3;
}
}
fputs("1234567890",fp);
fputs("abcdefghij",fp);
fseek(fp,0,SEEK_END);
fgetpos(fp,&pos);
printf("current file end position is %ld \n",pos);
fseek(fp,30,SEEK_END);
fgetpos(fp,&pos);
printf("call fseek(fp,30,SEEK_END)\n");
printf("current file position is %ld \n",pos);
fputs("abcdefg",fp);
printf("write %c %s %c \n",'"',"abcdfg",'"');
fgetpos(fp,&pos);
printf("current position of file is %ld \n",pos);
fclose(fp);
}

问题1:if(argv[1][0]!='a')这里,为什么多了一个[0]? argv[1]是参数名称,那么argv[0][1]代表什么呢?
问题2:fgetpos(fp,&pos);pos只是fpos_t类型的变量,这里&pos取其地址是什么意思呢?
问题3:printf("write %c %s %c \n",'"',"abcdfg",'"');这里的""","和""是不是多余的?

以上三个方面不是很懂,望大神指点。。。。
[解决办法]
问题1:argv[1][0]表示第一个参数的第一个字符
问题2:&pos的意思是传入pos地址,以便fgetpos(fp,&pos)可以通过地址改写pos的值
问题3:"号需要转译符\,否则编译器无法正确识别

引用:
#include <stdio.h>
#include <stdlib.h>
char buf[132];

int main(int argc, char *argv[])
{
FILE *fp;
fpos_t pos;
if(argc!=2)
{
printf("usage: %s mode \n",argv[0]);
return 1;
}
if(argv[1][0]!='a')
{
if((fp=fopen("testfile","w+"))==NULL)
{
printf("fopen failed!\n");
return 2;
}
}
else
{
if((fp=fopen("testfile","a+"))==NULL)
{
printf("fopen failed!\n");
return 3;
}
}
fputs("1234567890",fp);
fputs("abcdefghij",fp);
fseek(fp,0,SEEK_END);
fgetpos(fp,&pos);
printf("current file end position is %ld \n",pos);
fseek(fp,30,SEEK_END);
fgetpos(fp,&pos);
printf("call fseek(fp,30,SEEK_END)\n");
printf("current file position is %ld \n",pos);
fputs("abcdefg",fp);
printf("write %c %s %c \n",'"',"abcdfg",'"');
fgetpos(fp,&pos);
printf("current position of file is %ld \n",pos);
fclose(fp);
}

问题1:if(argv[1][0]!='a')这里,为什么多了一个[0]? argv[1]是参数名称,那么argv[0][1]代表什么呢?
问题2:fgetpos(fp,&pos);pos只是fpos_t类型的变量,这里&pos取其地址是什么意思呢?
问题3:printf("write %c %s %c \n",'"',"abcdfg",'"');这里的""","和""是不是多余的?

以上三个方面不是很懂,望大神指点。。。。

热点排行