标题:求助,关于文件写入的问题
只看楼主
爱之梦魇
Rank: 2
等 级:论坛游民
帖 子:21
专家分:30
注 册:2018-2-2
得分:0 
回复 10楼 炎天
好了,虽然文件里存储的信息有问题,但是在程序中输出结果是没问题的
2018-02-02 23:25
爱之梦魇
Rank: 2
等 级:论坛游民
帖 子:21
专家分:30
注 册:2018-2-2
得分:0 
回复 10楼 炎天
那如何让wb以二进制形式写入呢
2018-02-02 23:26
爱之梦魇
Rank: 2
等 级:论坛游民
帖 子:21
专家分:30
注 册:2018-2-2
得分:0 
回复 10楼 炎天
如何让wb以二进制形式写入
2018-02-02 23:27
炎天
Rank: 13Rank: 13Rank: 13Rank: 13
来 自:桃花岛
等 级:贵宾
威 望:29
帖 子:1218
专家分:4986
注 册:2016-9-15
得分:0 
表述可能不清楚, fp=fopen("E:\\cdata.txt","wb")  //建立一个新的二进制文件, 那就用fwrite(),fread()写读
        fp=fopen("E:\\cdata.txt","w")   //建立一个新的文本文件, 用fputs() , fgets() 写读

早知做人那么辛苦!  当初不应该下凡
2018-02-02 23:34
Jeyyion_han
Rank: 5Rank: 5
等 级:职业侠客
威 望:4
帖 子:207
专家分:301
注 册:2018-1-22
得分:10 
我也遇到过这种乱码的问题,我后来改了一下,就可以了,我把那一块的函数给你贴上
int input_stu_xinxi()

{    /*************

    int flag = 1;

    while (flag)

    {

    struct stu students;

    FILE *fp;

    errno_t err= (fopen_s(&fp, "F:\stuinfo.txt", "w"));

    if (err!= 0)

    {

    printf("文件打开错误\n");

    exit(0);

    }

    if (fwrite(&students, sizeof(struct stu), 1, fp)!=1)

    {

    printf("\n file write error \n");

    }



    文件的写还需要完善

    printf("\t学号:");

    scanf("%s", students.stu_no);

    fputs(students.stu_no,fp);

    printf("\t姓名:");

    scanf("%s", students.name);

    fputs(students.name, fp);

    printf("\t年龄:");

    scanf("%s", students.age);

    fputs(students.age, fp);

    printf("\t性别:");

    scanf("%s", students.sex);

    fputs(students.sex, fp);

    printf("\t出生年月:");

    scanf("%s", students.birth_date);

    fputs(students.birth_date, fp);

    printf("\t地址:");

    scanf("%s", students.address);

    fputs(students.address, fp);

    printf("\t电话:");

    scanf("%s", students.phone);

    fputs(students.phone, fp);

    printf("\t邮件:");

    scanf("%s", students.email);

    fputs(students.email, fp);

    printf("\n");

    fclose(fp);

    memset(&students,0x00,sizeof(struct stu));

    printf("继续输入请按1,返回上级菜单请按2,退出请安0\n");

    scanf_s("%d", &flag);

    if (flag == 0)

    {

    exit(0);

    }

    if (flag == 1)

    {

    continue;

    }

    if (flag == 2)

    {

    menu();

    break;

    }

    }

    return 0;

    ******/

    FILE *fp;

    int i, m, flag = 1;

    printf("请输入等级学生信息的总人数:");

    scanf_s("%d", &m);

    while (flag)

    {

        fp = fopen("F:\stuinfo.txt", "w");

        //errno_t err = (fopen_s(&fp, "F:\stuinfo.txt", "w"));

        if (fp == 0)

        {

            printf("文件打开错误\n");

            exit(0);

        }

        /****

        if (fwrite(&students[i], sizeof(struct stu), 1, fp) != 1)

        {

        printf("\n file write error \n");s

        }

        ****/

        for (i = 0; i < m; i++)

        {

            printf("学号:");

            scanf("%s", &students[i].stu_no);

            fputs(students[i].stu_no, fp);

            fputs("\t", fp);



            printf("姓名:");

            scanf("%s", &students[i].name);

            fputs(students[i].name, fp);

            fputs("\t", fp);



            printf("年龄:");

            scanf("%s", &students[i].age);

            fputs(students[i].age, fp);

            fputs("\t", fp);



            printf("性别:");

            scanf("%s", &students[i].sex);

            fputs(students[i].sex, fp);

            fputs("\t", fp);



            printf("出生年月:");

            scanf("%s", &students[i].birth_date);

            fputs(students[i].birth_date, fp);

            fputs("\t", fp);



            printf("地址:");

            scanf("%s", &students[i].address);

            fputs(students[i].address, fp);

            fputs("\t", fp);



            printf("电话:");

            scanf("%s", &students[i].phone);

            fputs(students[i].phone, fp);

            fputs("\t", fp);



            printf("邮件:");

            scanf("%s", &students[i].email);

            fputs(students[i].email, fp);

            fputs("\n", fp);

        }

        fclose(fp);

        printf("继续输入请按1,返回上级菜单请按2,退出请安0\n");

        scanf_s("%d", &flag);

        if (flag == 0)

        {

            exit(0);

        }

        if (flag == 1)

        {

            continue;

        }

        if (flag == 2)

        {

            menu();

            break;

        }

    }

    return 0;

}
2018-02-09 18:39
Jeyyion_han
Rank: 5Rank: 5
等 级:职业侠客
威 望:4
帖 子:207
专家分:301
注 册:2018-1-22
得分:0 
你看到fclose(fp)就可以了
2018-02-09 18:40
Jeyyion_han
Rank: 5Rank: 5
等 级:职业侠客
威 望:4
帖 子:207
专家分:301
注 册:2018-1-22
得分:0 
你看到fclose(fp)就可以了
2018-02-09 18:40



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-484604-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.250698 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved