0
#include <stdio.h>
#include <stdlib.h>
#define NULL 0

struct student
{
    int id;
    char name[20];
    float marks;
    struct student *next
};
typedef struct student node;
void main()
{
    node *head;
    void read(node *p);
    void create(node *p);
    int count(node *p);
    void print(node *p);
    head=(node *)malloc(sizeof(node));
    read(head);

}
void read(node *list)
{
    FILE *fp;
    char filename[30];
    int i;
    printf("input file name:");
    scanf("%s",filename);
    fp=fopen(filename,"r");

    while(!feof(fp))
    {
        create_f(list,fp);
    }

}
void create_f(node *list,FILE *fp )
{
    fscanf(fp,"%s %d %f",
               list->name,&list->id,&list->marks);
    printf("%s \t%d \t%f\n",
                        list->name,list->id,list->marks);

   list->next=(node*)malloc(sizeof(node));
    return;
}

The file that supposed to be read is this: image
but for some reasons it read the last line twice. Can anybody help me?

image

if you cannot open it here is the output:

input file name:input.txt.txt
student01 1 95.000000
student03 3 90.000000
student05 5 86.000000
student07 7 83.000000
student09 9 98.000000
student10 10 93.000000
student08 8 92.000000
student06 6 96.000000
student04 4 93.000000
student02 2 88.000000
student02 2 88.000000

Process returned 16 (0x10) execution time : 4.043 s Press any key to continue.

1:

Dor
  • 657
  • 1
  • 5
  • 11
ThisIsHard
  • 25
  • 1
  • 6
  • 1
    [Why is “while ( !feof (file) )” always wrong?](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong). It helps if you actually validate you read as many arguments as you think you did in your scanf calls, rather than assuming they always work. Assumption is the mother of all... – WhozCraig Dec 30 '19 at 13:12
  • @WhozCraig Ermmm.. so what should i use instead in my case? Sorry but im very new in programming. – ThisIsHard Dec 30 '19 at 13:31
  • Read the Q&A in the link. it spells out ample alternatives. – WhozCraig Dec 30 '19 at 13:32
  • Read [this question and it's high-voted answer on Meta](https://meta.stackoverflow.com/questions/284236/why-is-can-someone-help-me-not-an-actual-question) to learn why "can anybody help me is not a question". – dandan78 Dec 30 '19 at 19:17

1 Answers1

1

So the main error in your code is already well explained in few answers, take a look at the following links:

  1. feof
  2. fscanf

The following code snippet should be the solution for your question, notice that I have only one function.

Input (input.txt)


student01 1 95.000000
student03 3 90.000000
student05 5 86.000000
student07 7 83.000000
student09 9 98.000000
student10 10 93.000000
student08 8 92.000000
student06 6 96.000000
student04 4 93.000000
student02 2 88.000000


Solution

#include <stdio.h>
#include <stdlib.h>

struct student {
    int id;
    char name[20];
    float marks;
    struct student *next;
};
typedef struct student node;

void read(node *list) {
    FILE *fp;
    char filename[30];
    printf("input file name: ");
    scanf("%s",filename);
    fp=fopen(filename,"r");

    if(fp == NULL) return; // file doesn't exist

    while(fscanf(fp, "%s %d %f", list->name, &list->id, &list->marks) == 3) {
        printf("%s \t%d \t%f\n", list->name,list->id,list->marks);

        list->next=(node*)malloc(sizeof(node));
     }
    fclose(fp);
}

int main() {
    node *head; 
    head=(node *) malloc(sizeof(node));
    read(head); 
    return 0;
}

Output

input file name: input.txt
student01   1   95.000000
student03   3   90.000000
student05   5   86.000000
student07   7   83.000000
student09   9   98.000000
student10   10  93.000000
student08   8   92.000000
student06   6   96.000000
student04   4   93.000000
student02   2   88.000000
davidm
  • 1,570
  • 11
  • 24