求助:使用C语言读取文本文件并创建新文件,能创建但文件没有内容。请求大神帮忙!谢谢!

sinat_41822458 2018-03-11 04:00:45
题目:
现有两个文本文件db1.txt和db2.txt。db1.txt中第一列为姓名,第二列为英语成绩;db2.txt中第一列为姓名,第二列为数学成绩。通过姓名字段将db1.txt关联到db2.txt文件生成db3.txt文件,使db3.txt文件的第一列为姓名,第二列为英语成绩,第三列为数学成绩,第四列为平均成绩。
我的代码如下:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct student /*包含学生姓名和成绩*/
{
char *name;
int score;
};

int main()
{
FILE *fp1;
FILE *fp2;
FILE *fp3;

fp1 = fopen("db1.txt", "r");
fp2 = fopen("db2.txt", "r");
fp3 = fopen("db3.txt", "w"); /*db3.txt将被创建*/

struct student eng[100];
struct student math[100];
int num1 = 0;
int num2 = 0;
int i = 0;
int j = 0;

while (!feof(fp1))
{
fscanf(fp1, "%s %d\n", eng[num1].name, eng[num1].score);
num1++;
}
fclose(fp1);

while (!feof(fp1))
{
fscanf(fp1, "%s %d\n", math[num2].name, math[num2].score);
num2++;
}
fclose(fp2);

for (i = 0; i < num1; i++)
{
for(j = 0; j < num2; j++)
{
if (strcmp(eng[i].name, math[j].name) == 0)
{
fprintf(fp3, "%s %d %f\n", eng[i].name, eng[i].score, math[j].score, (float)(eng[i].score + math[j].score) / 2);
}
}
}
fclose(fp3);
return 0;
}

问题:运行时出现的问题如下:

...全文
904 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2018-03-14
  • 打赏
  • 举报
回复
崩溃的时候在弹出的对话框按相应按钮进入调试,按Alt+7键查看Call Stack即“调用堆栈”里面从上到下列出的对应从里层到外层的函数调用历史。双击某一行可将光标定位到此次调用的源代码或汇编指令处,看不懂时双击下一行,直到能看懂为止
自信男孩 2018-03-14
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct student /*包含学生姓名和成绩*/
{
    char name[16];
    int english;
    int math;
};

#define MAX_SIZE    100

int main()
{
    FILE *fp1 = fopen("db1.txt", "r");
    if (!fp1)
        exit(0);
    FILE *fp2 = fopen("db2.txt", "r");
    if (!fp2)
        exit(0);
    FILE *fp3 = fopen("db3.txt", "w");
    if (!fp3)
        exit(0);

    struct student info[MAX_SIZE] = { 0 };
    char name[32];
    int ret, score;
    int i = 0, num;

    while (!feof(fp1))
    {
        ret = fscanf(fp1, "%s %d\n", name, &score);
        if (ret < 2)
            break;
        //printf("1. name = %s, score = %d\n", name, score);
        strcpy(info[i].name, name);
        info[i].english = score;
        i++;
        if (i >= MAX_SIZE)
            break;
    }
    fclose(fp1);

    num = i;
    i = 0;
    while (!feof(fp2))
    {
        ret = fscanf(fp2, "%s %d\n", name, &score);
        if (ret < 2)
            break;
        //printf("2. name = %s, score = %d\n", name, score);

        while (i < num) {
            if (strcmp(info[i].name, name) == 0) {
                info[i].math = score;
                break;
            }
            i++;
        }
        if (i >= num && i < MAX_SIZE) {
            strcpy(info[i].name, name);
            info[i].math = score;
            num++;
        }
        if (num >= MAX_SIZE)
            break;
        i = 0;
    }
    fclose(fp2);

    //printf("Num = %d\n", num);
    i = 0;
    while (i < num) {
        fprintf(fp3, "%s %d %d %.1lf\n",
                info[i].name,
                info[i].english,
                info[i].math,
                (info[i].english + info[i].math)*1.0 / 2);
        i++;
    }
    fclose(fp3);
    return 0;
}
上面的代码确实有点问题,这个是测试过的。参考一下吧 遇到问题不要慌,不要马上就问,自己先试试找问题,不然自己怎么能进步呢?!
sinat_41822458 2018-03-14
  • 打赏
  • 举报
回复
引用 7 楼 cfjtaishan 的回复:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct student /*包含学生姓名和成绩*/
{
    char name[16];
    int english;
    int math;
};

#define MAX_SIZE    100

int main()
{
    FILE *fp1 = fopen("db1.txt", "r");
    if (!fp1)
        exit(0);
    FILE *fp2 = fopen("db2.txt", "r");
    if (!fp2)
        exit(0);
    FILE *fp3 = fopen("db3.txt", "w");
    if (!fp3)
        exit(0);

    struct student info[MAX_SIZE] = { 0 };
    char name[32];
    int ret, score;
    int i = 0, num;

    while (!feof(fp1))
    {
        ret = fscanf(fp1, "%s %d\n", name, &score);
        if (ret < 2)
            break;
        //printf("1. name = %s, score = %d\n", name, score);
        strcpy(info[i].name, name);
        info[i].english = score;
        i++;
        if (i >= MAX_SIZE)
            break;
    }
    fclose(fp1);

    num = i;
    i = 0;
    while (!feof(fp2))
    {
        ret = fscanf(fp2, "%s %d\n", name, &score);
        if (ret < 2)
            break;
        //printf("2. name = %s, score = %d\n", name, score);

        while (i < num) {
            if (strcmp(info[i].name, name) == 0) {
                info[i].math = score;
                break;
            }
            i++;
        }
        if (i >= num && i < MAX_SIZE) {
            strcpy(info[i].name, name);
            info[i].math = score;
            num++;
        }
        if (num >= MAX_SIZE)
            break;
        i = 0;
    }
    fclose(fp2);

    //printf("Num = %d\n", num);
    i = 0;
    while (i < num) {
        fprintf(fp3, "%s %d %d %.1lf\n",
                info[i].name,
                info[i].english,
                info[i].math,
                (info[i].english + info[i].math)*1.0 / 2);
        i++;
    }
    fclose(fp3);
    return 0;
}
上面的代码确实有点问题,这个是测试过的。参考一下吧 遇到问题不要慌,不要马上就问,自己先试试找问题,不然自己怎么能进步呢?!
抱歉,C语言小白,看到问题就头大。想解决又不知道从何下手。是我太心急了。
sinat_41822458 2018-03-13
  • 打赏
  • 举报
回复
引用 5 楼 cfjtaishan 的回复:
[quote=引用 4 楼 sinat_41822458 的回复:] [quote=引用 3 楼 cfjtaishan 的回复:]
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct student /*包含学生姓名和成绩*/
{
    char name[16];
    int english;
    int math;
};

#define MAX_SIZE    100

int main()
{
    FILE *fp1 = fopen("db1.txt", "r");
    if (!fp1)
        exit(0);
    FILE *fp2 = fopen("db2.txt", "r");
    if (!fp2)
        exit(0);
    FILE *fp3 = fopen("db3.txt", "w"); /*db3.txt将被创建*/
    if (!fp3)
        exit(0);

    struct student info[MAX_SIZE] = { 0 };
    char name[32];
    int ret, score;
    int i = 0, num;

    while (!feof(fp1))
    {
        ret = fscanf(fp1, "%s %d\n", name, &score);
        if (ret < 2)
            break;
        strcpy(info[i].name, name);
        info[i].english = score;
        i++;
        if (i >= MAX_SIZE)
            break;
    }
    fclose(fp1);

    num = i;
    i = 0;
    while (!feof(fp2))
    {
        ret = fscanf(fp2, "%s %d\n", name, &score);
        if (ret < 2)
            break;

        while (i < num)
            if (strcmp(info[i].name, name) == 0)
                info[i].math = score;
        if (i >= num && i < MAX_SIZE) {
            strcpy(info[i].name, name);
            info[i].math = score;
            num++;
        }
        if (num >= MAX_SIZE)
            break;
        i = 0;
    }
    fclose(fp2);

    i = 0;
    while (i < num) {
        fprintf(fp3, "%s %d %d %.1lf\n",
                info[i].name,
                info[i].english,
                info[i].math,
                (info[i].english + info[i].math)*1.0 / 2);
        i++;
    }
    fclose(fp3);
    return 0;
}
参考一下吧 问题较多,建议对比找一下;比如两个while循环都是从文件1里读取,这个错误不应该呢,不够仔细。 程序异常退出的直接原因是name是野指针,没有给其分配空间,建议直接用数组即可。
为什么运行了一下您的代码“db3”还是个空文档呢? [/quote] 程序异常退出了,没有正常运行。 贴一下你的db1,db2两个文件的内容[/quote]
自信男孩 2018-03-12
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct student /*包含学生姓名和成绩*/
{
    char name[16];
    int english;
    int math;
};

#define MAX_SIZE    100

int main()
{
    FILE *fp1 = fopen("db1.txt", "r");
    if (!fp1)
        exit(0);
    FILE *fp2 = fopen("db2.txt", "r");
    if (!fp2)
        exit(0);
    FILE *fp3 = fopen("db3.txt", "w"); /*db3.txt将被创建*/
    if (!fp3)
        exit(0);

    struct student info[MAX_SIZE] = { 0 };
    char name[32];
    int ret, score;
    int i = 0, num;

    while (!feof(fp1))
    {
        ret = fscanf(fp1, "%s %d\n", name, &score);
        if (ret < 2)
            break;
        strcpy(info[i].name, name);
        info[i].english = score;
        i++;
        if (i >= MAX_SIZE)
            break;
    }
    fclose(fp1);

    num = i;
    i = 0;
    while (!feof(fp2))
    {
        ret = fscanf(fp2, "%s %d\n", name, &score);
        if (ret < 2)
            break;

        while (i < num)
            if (strcmp(info[i].name, name) == 0)
                info[i].math = score;
        if (i >= num && i < MAX_SIZE) {
            strcpy(info[i].name, name);
            info[i].math = score;
            num++;
        }
        if (num >= MAX_SIZE)
            break;
        i = 0;
    }
    fclose(fp2);

    i = 0;
    while (i < num) {
        fprintf(fp3, "%s %d %d %.1lf\n",
                info[i].name,
                info[i].english,
                info[i].math,
                (info[i].english + info[i].math)*1.0 / 2);
        i++;
    }
    fclose(fp3);
    return 0;
}
参考一下吧 问题较多,建议对比找一下;比如两个while循环都是从文件1里读取,这个错误不应该呢,不够仔细。 程序异常退出的直接原因是name是野指针,没有给其分配空间,建议直接用数组即可。
自信男孩 2018-03-12
  • 打赏
  • 举报
回复
引用 4 楼 sinat_41822458 的回复:
[quote=引用 3 楼 cfjtaishan 的回复:]
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct student /*包含学生姓名和成绩*/
{
    char name[16];
    int english;
    int math;
};

#define MAX_SIZE    100

int main()
{
    FILE *fp1 = fopen("db1.txt", "r");
    if (!fp1)
        exit(0);
    FILE *fp2 = fopen("db2.txt", "r");
    if (!fp2)
        exit(0);
    FILE *fp3 = fopen("db3.txt", "w"); /*db3.txt将被创建*/
    if (!fp3)
        exit(0);

    struct student info[MAX_SIZE] = { 0 };
    char name[32];
    int ret, score;
    int i = 0, num;

    while (!feof(fp1))
    {
        ret = fscanf(fp1, "%s %d\n", name, &score);
        if (ret < 2)
            break;
        strcpy(info[i].name, name);
        info[i].english = score;
        i++;
        if (i >= MAX_SIZE)
            break;
    }
    fclose(fp1);

    num = i;
    i = 0;
    while (!feof(fp2))
    {
        ret = fscanf(fp2, "%s %d\n", name, &score);
        if (ret < 2)
            break;

        while (i < num)
            if (strcmp(info[i].name, name) == 0)
                info[i].math = score;
        if (i >= num && i < MAX_SIZE) {
            strcpy(info[i].name, name);
            info[i].math = score;
            num++;
        }
        if (num >= MAX_SIZE)
            break;
        i = 0;
    }
    fclose(fp2);

    i = 0;
    while (i < num) {
        fprintf(fp3, "%s %d %d %.1lf\n",
                info[i].name,
                info[i].english,
                info[i].math,
                (info[i].english + info[i].math)*1.0 / 2);
        i++;
    }
    fclose(fp3);
    return 0;
}
参考一下吧 问题较多,建议对比找一下;比如两个while循环都是从文件1里读取,这个错误不应该呢,不够仔细。 程序异常退出的直接原因是name是野指针,没有给其分配空间,建议直接用数组即可。
为什么运行了一下您的代码“db3”还是个空文档呢? [/quote] 程序异常退出了,没有正常运行。 贴一下你的db1,db2两个文件的内容
sinat_41822458 2018-03-12
  • 打赏
  • 举报
回复
引用 3 楼 cfjtaishan 的回复:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct student /*包含学生姓名和成绩*/
{
    char name[16];
    int english;
    int math;
};

#define MAX_SIZE    100

int main()
{
    FILE *fp1 = fopen("db1.txt", "r");
    if (!fp1)
        exit(0);
    FILE *fp2 = fopen("db2.txt", "r");
    if (!fp2)
        exit(0);
    FILE *fp3 = fopen("db3.txt", "w"); /*db3.txt将被创建*/
    if (!fp3)
        exit(0);

    struct student info[MAX_SIZE] = { 0 };
    char name[32];
    int ret, score;
    int i = 0, num;

    while (!feof(fp1))
    {
        ret = fscanf(fp1, "%s %d\n", name, &score);
        if (ret < 2)
            break;
        strcpy(info[i].name, name);
        info[i].english = score;
        i++;
        if (i >= MAX_SIZE)
            break;
    }
    fclose(fp1);

    num = i;
    i = 0;
    while (!feof(fp2))
    {
        ret = fscanf(fp2, "%s %d\n", name, &score);
        if (ret < 2)
            break;

        while (i < num)
            if (strcmp(info[i].name, name) == 0)
                info[i].math = score;
        if (i >= num && i < MAX_SIZE) {
            strcpy(info[i].name, name);
            info[i].math = score;
            num++;
        }
        if (num >= MAX_SIZE)
            break;
        i = 0;
    }
    fclose(fp2);

    i = 0;
    while (i < num) {
        fprintf(fp3, "%s %d %d %.1lf\n",
                info[i].name,
                info[i].english,
                info[i].math,
                (info[i].english + info[i].math)*1.0 / 2);
        i++;
    }
    fclose(fp3);
    return 0;
}
参考一下吧 问题较多,建议对比找一下;比如两个while循环都是从文件1里读取,这个错误不应该呢,不够仔细。 程序异常退出的直接原因是name是野指针,没有给其分配空间,建议直接用数组即可。
为什么运行了一下您的代码“db3”还是个空文档呢?
zhangyiant 2018-03-11
  • 打赏
  • 举报
回复
好像从来就没给name分配空间。
DarkChampion 2018-03-11
  • 打赏
  • 举报
回复
问题太多了,指针没空间的, struct student /*包含学生姓名和成绩*/ { char name[32]; int score; }; while (!feof(fp1))里面全是fp1 fscanf也用错了

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧