【问题标题】:Reading data from files, c从文件中读取数据,c
【发布时间】:2018-01-18 20:22:37
【问题描述】:

我在c中练习文件的主题,目前正在学习基础知识,也就是说我并不控制语言内置的所有功能,并且在一些命令中,所以如果你能根据基本命令解释一下复杂的我会感谢你的。

程序的作用: 吸收学生资料(身份证、姓名、电话),并在文件中打印每个学生的资料(每个学生的资料都会写在新的一行)

我的代码有什么问题: 我写了一个打印文件中所有数据的函数,如果用户插入了多个学生,该函数只打印文件中的第一行,就是这样。

当我使用 F10 时,我进入了循环,第一次运行打印出第一个学生的详细信息,然后第二次运行,fscanf 命令返回一个值 (-1),并且没有打印出学生的详细信息第二个学生。

我很乐意指导和纠正。

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct studnt
{
    int id;
    int number;
    char *name;
}typedef s_studnt;
void print_studnt(FILE *q,char name_file[])
{
    int id, number;
    char str_name[20];
    q=fopen(name_file, "r");
    if (q == NULL)
    {
        printf("eror fac\n");
        return;
    }
    printf("\n");
    while (!feof(q))
    {

        fscanf(q, "%d %s %d", &id, str_name, &number); //at the next loop, fscanf return -1
        printf("studnt name:%s\nthe id studnt:%d\ntelephon number:%d\n", str_name, id, number);
        printf("\n");
    }
    fclose(q);
    return;
}
int main()
{
    int size, i, length;
    char str[20], name_file[20];
    FILE *q = NULL;
    s_studnt *studnt = NULL;
    printf("how much studnt you have:\n");
    scanf("%d", &size);
    studnt = (s_studnt*)malloc(size * sizeof(s_studnt));
    if (studnt == NULL)
    {
        printf("eror 1\n");
        return 0;
    }
    printf("enter file name:\n");
    scanf("%s", name_file);
    q = fopen(name_file, "w");
    if (q == NULL)
    {
        printf("eror file\n");
        return 0;
    }
    fclose(q);
    for (i = 0; i < size; i++)
    {
        printf("enter studnt name:\n");
        scanf("%s", str);
        length = strlen(str);
        studnt[i].name = (char*)malloc(length+1 * sizeof(char));
        if (studnt[i].name == NULL)
        {
            printf("eror2\n");
            return 0;
        }
        strcpy(studnt[i].name, str);
        printf("enter the id studnt:\n");
        scanf("%d", &studnt[i].id);
        printf("enter your telephon number:\n");
        scanf("%d", &studnt[i].number);
        q = fopen(name_file, "a");
        if (q == NULL)
        {
            printf("eror write\n");
            return 0;
        }
        fprintf(q, "%d %s %d\n", studnt[i].id, studnt[i].name, studnt[i].number);

    }
    fclose(q);
    print_studnt(q,name_file);
    return 0;
}

【问题讨论】:

  • 一个显而易见的问题需要解决:将length+1 * sizeof(char) 更改为(length+1) * sizeof(char)。请记住,乘法组比加法组更紧密。在这种情况下,sizeof(char) 为 1 并不重要,但只要你在使用它,就可以正确使用它。
  • 您多次打开该文件。在循环外打开一次。然后写入它并在完成写入后关闭文件。你所做的调用了实现定义的行为,你又使用了while (!feof(q))..wrong。您的代码有很多需要纠正的地方——一本书的帮助不仅仅是答案。
  • @coderredoc 谢谢,在我从循环中打开文件后,它解决了。

标签: c file


【解决方案1】:

以下代码正在运行。您必须注意不要经常打开和关闭文件,即在循环内:打开文件,执行循环并关闭循环后面的文件。

那么fscanf() 有问题。 fscanf() 确实会读取到下一个空格,因此当您以fscanf() "%d %s %d" 的格式指定时,它会读取到第三个空格然后停止。您可以通过以下代码中的方法解决此问题(读取fscanf() 的返回值),因为fscanf() 如果读取它(http://www.cplusplus.com/reference/cstdio/fscanf/)返回EOF,下面的代码fscanf() 继续扫描文件行-逐行直到它读取EOF。使用 fscanf() 逐行读取文件不被认为是一种好的做法(reading lines using fscanf

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct studnt
{
    int id;
    int number;
    char *name;
}typedef s_studnt;

void print_studnt(FILE *q,char name_file[])
{
    int id, number;
    char str_name[20];

    q=fopen(name_file, "r");
    if (q == NULL)
    {
        printf("eror fac\n");
        return;
    }
    printf("\n");

    int r; 
    r=0;

    do
    {
        r = fscanf(q, "%d %s %d", &id, str_name, &number); 
        if(r==EOF) break;
        printf("studnt name:%s\nthe id studnt:%d\ntelephon number:%d\n", str_name, id, number);
        printf("\n");

    } while (r != EOF);
    fclose(q);
    return;
}


int main()
{
    int size, i, length;
    char str[20], name_file[20];
    FILE *q = NULL;
    s_studnt *studnt = NULL;
    printf("how much studnt you have:\n");
    scanf("%d", &size);
    studnt = (s_studnt*)malloc(size * sizeof(s_studnt));
    if (studnt == NULL)
    {
        printf("eror 1\n");
        return 0;
    }
    printf("enter file name:\n");
    scanf("%s", name_file);
    q = fopen(name_file, "w");
    if (q == NULL)
    {
        printf("eror file\n");
        return 0;
    }


    for (i = 0; i < size; i++)
    {

        printf("enter studnt name:\n");
        scanf("%s", str);
        length = strlen(str);
        studnt[i].name = (char*)malloc(length+1 * sizeof(char));
        if (studnt[i].name == NULL)
           {
            printf("eror2\n");
            return 0;
           }
        strcpy(studnt[i].name, str);
        printf("enter the id studnt:\n");
        scanf("%d", &studnt[i].id);
        printf("enter your telephon number:\n");
        scanf("%d", &studnt[i].number);

        fprintf(q, "%d %s %d\n", studnt[i].id, studnt[i].name, studnt[i].number);

    }
    fclose(q);
    print_studnt(q,name_file);
    return 0;
}

【讨论】:

    猜你喜欢
    • 2013-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-21
    • 2023-03-06
    • 2016-04-24
    相关资源
    最近更新 更多