0

Sorry for the language difference, thank you

In first I input data ( program to input data student , the teacher asked to use .txt )

#include"stdio.h"
#include"stdlib.h"
#include"string.h"
void delete_n(char a[]){
    size_t x=strlen(a);
        if(a[strlen(a)-1]=='\n'){
            a[strlen(a)-1]='\0';
        }
}
struct age{
    char id[10];
    char name[50];
    int numb;
    int score;
};
void sx(age dnu[], int n){
    age a;
    for(int i=0;i<n;i++){
        for(int j=i+1;j<n;j++){
            if(dnu[i].numb<dnu[j].numb){
                a=dnu[i];
                dnu[i]=dnu[j];
                dnu[j]=a;
            }
        }
    }
}
void file(age dnu[],int n){
    FILE *f;
    f = fopen("ex3","w");
    if(f==NULL){
        printf("Error");
        exit(0);
    }
    fprintf(f,"%d\n",n);    
    for(int i=0;i<n;i++){
        fprintf(f," %s\n",dnu[i].id);
        fprintf(f,"%s\n",dnu[i].name);
        fprintf(f," %d \n",dnu[i].numb);
        fprintf(f," %d \n",dnu[i].score);
    }

    fclose(f);
}
void out(age dnu[],int n){
    printf("%10s    %20s    %20s    %20s","Ma nganh","Ten nganh","So sinh vien ","Diem trung tuyen\n");
    for(int i=0;i<n;i++){
        printf("%10s    %20s    %20d    %20d",dnu[i].id,dnu[i].name,dnu[i].numb,dnu[i].score);
        printf("\n");
    }
}
int main(){

    age *dnu;
    int n;
    printf("Nhap so nganh hoc can quan ly  : ");
    scanf("%d",&n);
    dnu = (age*)malloc(n*sizeof(age));
    for(int i=0;i<n;i++){
        fflush(stdin);
        printf("Nhap ma nganh hoc : ");
        fgets(dnu[i].id,sizeof(dnu[i].id),stdin);
        delete_n(dnu[i].id);
        fflush(stdin);
        printf("Nhap ten nganh hoc : ");
        fgets(dnu[i].name,sizeof(dnu[i].name),stdin);
        delete_n(dnu[i].name);
        fflush(stdin);
        printf("Nhap so sinh vien : ");
        scanf("%d",&dnu[i].numb);
        printf("Nhap diem trung tuyen : ");
        scanf("%d",&dnu[i].score);
    }
    sx(dnu,n);
    out(dnu,n);
    file(dnu,n);
    free(dnu);
    
    
}

Then second program , i want output data student , but loop for only run 1 time then error i thinks , my mistakes is fscanf(f,"%d",....) , I think fscanf is not a good choice , sorry again for the language difference , you don't have to pay attention to anything else , I just think the error is in the FILE section

#include"stdio.h"
#include"stdlib.h"
#include"string.h"

struct age{
    char id[10];
    char name[50];
    int numb;
    int score;
};
int main(){
    age dnu[20];
    char x[50];
    int d;
    int n;
    FILE *f;
    f = fopen("ex3","r");
    if(f==NULL){
        printf("Error");
        exit(0);
    }
//  if( fgets(x,60,f)!=NULL){
//      puts(x);
//  }
    
    fscanf(f,"%d",&n);  
    for(int i=0;i<1;i++){
        fscanf(f," %s ",&dnu[i].id);
        fscanf(f," %s ",&dnu[i].name);
        fscanf(f,"%d",&dnu[i].numb);
        fscanf(f,"%d",&dnu[i].score);
    }
    printf("%10s    %20s    %20s    %20s","Ma nganh","Ten nganh","So sinh vien ","Diem trung tuyen\n");
    for(int i=0;i<n;i++){
        printf("%10s    %20s    %20d    %20d",dnu[i].id,dnu[i].name,dnu[i].numb,dnu[i].score);
        printf("\n");
    }
    fclose(f);
}

you can see images enter image description here

Ngoc Anh
  • 21
  • 5
  • 1
    Sorry but the code posted doesn't compile as C - there is no type `age`. I notice in the screen shot that you are using `.cpp` files. – Weather Vane Nov 24 '21 at 17:16
  • i want program 2 to get data from program 1 – Ngoc Anh Nov 24 '21 at 17:21
  • in the screenshot is the data I entered from program 1 , and the data that program 2 prints out . In the middle is the txt file – Ngoc Anh Nov 24 '21 at 17:24
  • you can see program 2 ,at the first time of the for loop, program 2 runs true, then it fails – Ngoc Anh Nov 24 '21 at 17:26
  • Questions seeking debugging help should always provide a [mre] of the problem. If I understand correctly, the first program is supposed to create the `.txt` file from user input, and the second program is supposed to read that `.txt` file. You should be able to determine by looking at the created `.txt` file whether the first program is working or not. If the first program is working and the problem is the second program, then there is no need to include the first program in the question. Instead, all that is required is the second program and the contents of the `.txt` file. – Andreas Wenzel Nov 24 '21 at 17:40
  • Are you supposed to write C or C++ code? You are using the `.cpp` file extension, which probably causes the C++ compiler to be invoked, instead of the C compiler. You should be using `.c` files if you want your code to be compiled as C. Some of the code you posted will not compile with a C compiler, only with a C++ compiler. So you will have to make a few changes if you want your code to compile with a C compiler. – Andreas Wenzel Nov 24 '21 at 17:43
  • thanks you @AndreasWenzel but , I'm afraid that my program 1 is also wrong , i hope you can help me , it is 00:44 am ( in my country ) and i will have to submit this in the morning – Ngoc Anh Nov 24 '21 at 17:45
  • 2
    You are mixing the use of `fgets(..., stdin)` with `scanf()` will will usually lead to problems, which you seem to be using `fflush(stdin)` to get round. But the behaviour is undefined. Get every input with `fgets()` and apply `sscanf()` to the input string if needed. Remove every `fflush(stdin)`. Rename the .cpp files as .c. Replace every use of `age` with `struct age`. – Weather Vane Nov 24 '21 at 17:47
  • You may want to read [this](https://stackoverflow.com/q/2979209/12149471) for the reasons why you should generally not use `fflush(stdin)`. – Andreas Wenzel Nov 24 '21 at 17:52
  • You stated that you suspect that the first program also is not working. Does that mean that the contents of the created `.txt` file is not what you want? If so, then please specify exactly the input, intended output and the actual output of the first program. – Andreas Wenzel Nov 24 '21 at 17:56
  • @Andreas Wenzel thanks you – Ngoc Anh Nov 24 '21 at 17:58
  • I am afraid that the way I write the file in program 1 is wrong, but as far as I can see it still works properly, the txt file also produces the results I want. , but the program that reads the file ( program 2 ) crashes , I described it in the article – Ngoc Anh Nov 24 '21 at 18:02
  • The first line of the `.txt` file consists of `z2`. When your second program does `fscanf(f,"%d",&n);`, it will attempt to convert that string to a number, which will fail, because `z` is not a digit. What is supposed to be in that first line? Are you sure that the `.txt` file in the screenshot was created by the posted code of your first program? I am asking because I see no way how the line `fprintf(f,"%d\n",n);` in the first program could write `z2` into the input file. – Andreas Wenzel Nov 24 '21 at 18:23
  • If the `.txt` file was indeed created by the first program with the code posted in the question, then please specify the exact input of the first program that creates that output file. – Andreas Wenzel Nov 24 '21 at 18:28

0 Answers0