简体   繁体   English

从文件读取数据,c

[英]Reading data from files, c

I practice the subject of files in c, and I am currently learning the basics, meaning I do not control all the functions built into the language, and in some commands, so if you can explain according to the basic commands and something complex I will thank you. 我在c中练习文件的主题,并且我目前正在学习基础知识,这意味着我不能控制语言中内置的所有功能以及某些命令,因此,如果您可以根据基本命令和一些复杂的内容进行解释,谢谢。

What the program does: Absorption of students' data (identity card, name, and telephone), and print each student's data in the file (each student information will be written in a new line) 该程序的作用:吸收学生的数据(身份证,姓名和电话),并在文件中打印每个学生的数据(每个学生的信息将写在新行中)

What's my problem with the code: I wrote a function that prints out all the data in the file, if the user inserted more than one student, the function prints only the first line in the file and that is it. 我的代码有什么问题:我编写了一个函数,可以打印出文件中的所有数据,如果用户插入了多个学生,则该函数仅打印文件的第一行。

When I used F10 I got to the loop while the first run printed out the details of the first student, and then the second run, the fscanf command returned a value (-1) and did not print out the details of the second student. 当我用F10进入循环时,第一次运行打印出第一个学生的详细信息,然后第二次运行,fscanf命令返回一个值(-1),而没有打印出第二个学生的详细信息。

I would be happy to guide, and to correct. 我很乐意指导和纠正。

#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;
}

the following code is working. 以下代码正在工作。 you have to take care to open and close the file not too often ie within loops : open the file, do the looping and the close the file behind the loop. 您必须注意不要太频繁地打开和关闭文件,即在循环内:打开文件,执行循环,然后关闭循环后的文件。

then there was a problem with fscanf() . 那么fscanf()就有问题了。 fscanf() does read until the next whitespace so when you specify in the format of fscanf() "%d %s %d" it reads until the third whitespace and then stops. fscanf()会一直读取到下一个空格,因此当您以fscanf()的格式指定"%d %s %d"它将读取直到第三个空格,然后停止。 you can workaround this by the method in the code below ( reading the return value of fscanf() ) because fscanf() returns EOF if it reads it ( http://www.cplusplus.com/reference/cstdio/fscanf/ ) with the code below fscanf() continues scanning the file line-by-line until it reads EOF . 您可以通过以下代码中的方法来解决此问题(读取fscanf()的返回值),因为fscanf()如果读取了( http://www.cplusplus.com/reference/cstdio/fscanf/ ),则会返回EOFfscanf()下面的代码继续逐行扫描文件,直到读取EOF为止。 reading files line-by-line with fscanf() is not considered to be good practice ( reading lines using fscanf ) fscanf()逐行读取文件被认为不是好习惯( 使用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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM