1

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.

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.

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;
}
aviv.L
  • 39
  • 9
  • 1
    One obvious thing to fix: change `length+1 * sizeof(char)` to `(length+1) * sizeof(char)`. Remember, multiplication groups more tightly than addition. In this case, it doesn't really matter since `sizeof(char)` is 1, but as long as you're using it, use it correctly. – Tom Karzes Jan 18 '18 at 10:21
  • 1
    You are opening the file multiple times. Open it once outside the loop. Then write to it and close the file when you are done writing. What you did invoked implementation defined behavior and you used `while (!feof(q))`..wrong again. There are lots of thing to correctin your code - a book would help more than an answer. – user2736738 Jan 18 '18 at 10:21
  • @coderredoc thanks, after I took the opening of the file from the loop it worked out. – aviv.L Jan 18 '18 at 10:33
  • See [Why is “while ( !feof (file) )” always wrong?](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) – Andrew Henle Jan 18 '18 at 11:30
  • @aviv.L.: https://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list – user2736738 Jan 18 '18 at 11:50

1 Answers1

0

the following code is working. you have to take care to open and close the file not too often i.e. within loops : open the file, do the looping and the close the file behind the loop.

then there was a problem with 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. 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. reading files line-by-line with fscanf() is not considered to be good practice ( 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;
}
ralf htp
  • 9,149
  • 4
  • 22
  • 34