啊哈磊_编程从这里起步

标题: 移动文件指针发生错误 [打印本页]

作者: ahgjg    时间: 2014-4-12 17:30
标题: 移动文件指针发生错误
unsigned long int getfilelen(char filename[])
{
        FILE *fp;
    unsigned long fl;
    if (fp=fopen(filename,"rb+")==NULL)
                printf("打开文件失败!");
    system("pause");
    printf("%d",fseek(fp,1L,0));      //移动文件指针
    system("pause");
    fl=ftell(fp);
    system("pause");
    fclose(fp);
    return fl;
}
执行到printf("%d",fseek(fp,1L,0));   这一句时就发生错误


文件打开是成功的,文件内容大约有20多个字节,这是为什么?


作者: rosynirvana    时间: 2014-4-12 17:30
if (fp=fopen(filename,"rb+")==NULL)

问题在这一句, ==的优先级比=要高
作者: rosynirvana    时间: 2014-4-12 17:53
贴完整的代码
作者: ahgjg    时间: 2014-4-12 20:10
rosynirvana 发表于 2014-4-12 17:53
贴完整的代码

//学生信息管理系统┌ └ ┐ ┘ ─ │ ├ ┤ ┬ ┴ ┼
//分五大模块:档案录入 档案修改 档案删除 查看学生信息 统计总数
#include <stdio.h>
#include <stdlib.h>
#include "show.h"
unsigned long int getfilelen(char filename[]);
struct inf
{
        char name[10];                        //姓名
    char age;                                //年龄
    char sex[2];                        //性别
    char father[10];                //父亲姓名
    char mather[10];                //乡亲姓名
    char lastschool[20];        //毕业学校
    char homeaddress[50];        //家庭住址
    char phone[20];                        //联系电话
    char others[100];                //备注
};
int main()
{
        unsigned int choosing;
    struct inf *head;
    while(1)
    {
                system("cls");
                show("学生信息管理系统","请输入相应选项编号,按回车");
                printf("┌───────────────────────────┐\n");
                printf("│\t\t\t\t\t\t\t│\n");
                printf("│\t\t\t\t\t\t\t│\n");
                printf("│\t\t\t1.档案录入\t\t\t│\n");
                printf("│\t\t\t2.档案修改\t\t\t│\n");
                printf("│\t\t\t3.档案删除\t\t\t│\n");
                printf("│\t\t\t4.查看信息\t\t\t│\n");
                printf("│\t\t\t5.统计总数\t\t\t│\n");
        printf("│\t\t\t0.退出系统\t\t\t│\n");
                printf("│\t\t\t\t\t\t\t│\n");
                printf("│\t\t\t\t\t\t\t│\n");
                printf("│\t\t\t\t\t\t\t│\n");
                printf("└───────────────────────────┘\n");
                printf("请输入选项:");
                scanf("%d",&choosing);  
        printf("%d",sizeof(struct inf));   
        switch(choosing)
        {
                        case 0:
                               
                                return 0;
            case 1:
                                printf("%ld",getfilelen("d:\\123.txt"));
                break;
            case 2:
                               
                break;
            case 3:
                               
                break;
            case 4:
                               
                break;
            case 5:
                               
                break;
        }
        system("pause");
    }       
       
}

struct inf* creatlist(unsigned int n,unsigned int size)
{
        struct inf *p;
    p=(struct inf*)calloc(n,size);
}   

unsigned long int getfilelen(char filename[])
{
        FILE *fp;
    unsigned long fl;
    if (fp=fopen(filename,"rb+")==NULL)
                printf("打开文件失败!");
    system("pause");
    printf("%d",fseek(fp,1L,0));      //移动文件指针
    system("pause");
    fl=ftell(fp);
    system("pause");
    fclose(fp);
    return fl;
}

int getfiledata(char filename[],struct inf *p,int size,int n)
{
        FILE *fp;
    fp=fopen(filename,"rb");
    fread(p,size,n,fp);
    fclose(fp);
}

作者: ahgjg    时间: 2014-4-12 23:31
rosynirvana 发表于 2014-4-12 20:43
if (fp=fopen(filename,"rb+")==NULL)

问题在这一句, ==的优先级比=要高

非常感谢
加了一层括号就好了




欢迎光临 啊哈磊_编程从这里起步 (https://bbs.codeaha.com/) Powered by Discuz! X3.2