学生信息文件处理 [英] student info file handling

查看:97
本文介绍了学生信息文件处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您可以帮我提高我的code ......这是所有学生的信息......我有语法的麻烦...在编辑菜单...我尝试使用STRCMP但没有任何反应,我第一次使用与fgets并将其存储在一个数组,然后询问用户的输入,并在另一个阵列再存放起来..然后我会比较......但它没有工作..希望你能帮助我..这是我的code ..

can you help me improve my code... this is all about student information... i'm having trouble in syntax... In editing menu... i try using strcmp but nothing happens, i first use fgets and store it at an array and then ask the user for an input and store it again in another array.. and then i'll compare... but it didn't work.. hope you can help me... this is my code..

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

struct student{
       char name[30];
       char id[8];
       char course[5];
};

int main(void){
    int option =0;

    while(option!=6){
       system("cls");
       printf("Menu:\n");
       printf("[1] Add Student.\n");
       printf("[2] Display Student.\n");
       printf("[3] Delete Student.\n");
       printf("[4] Delete Student.\n");
       printf("[5] Exit.\n");
       scanf("%d",&option);

       switch(option)
       {
          case 1:
                addStudent();
                break;
          case 2:
                displayinfo();
                break;
          case 3:
                break;  
          case 4:
                break;
          default:
                printf("That is not in the options!\nPlease Try again!\n");
                break;
       }

    }
}

addStudent(){
    int i;
    FILE *stream = NULL;
    stream = fopen("studentinfo.txt", "a+");

    struct student s1;
    struct student array[3];//here i wnt 2 apply d malloc but, still didn't know how 2start
    for (i =0; i<1; i++){

       printf("Enter Student ID: ");
       scanf("%s", s1.id);
       fflush(stdin);
       printf("Enter Student Name: ");
       gets(s1.name);
       fflush(stdin);
       printf("Enter Student Course: ");
       scanf("%s", s1.course);

       fprintf(stream, "\n%s,\t%s,\t%s", s1.id, s1.name, s1.course);
    }
       fclose(stream);
    getch();
}
displayinfo(){
    FILE *stream = NULL;
     stream = fopen("studentinfo.txt", "rt");

     char arr[100];
     int i=0;

    while(!feof(stream)){  
     fgets(arr, 100, stream);
     printf("%s", arr);
     }

     fclose(stream);
     getch();
}

这是我在编辑菜单计划:

here's my plan in EDITING MENU:

     printf("enter details: ");
     gets(arr2);

     while(!feof(stream)){ 
        fgets(arr, 100, stream);
        if(strcmp(arr, arr2)==0){
           //code here
        }

     }

将这项工作?

谢谢你们希望你能帮助我^ _ ^

thanks guys hope you can help me ^_^

推荐答案

与fgets()保持换行符。获得()不。因此,字符串将永远不会匹配。

fgets() keeps the newline. gets() does not. Hence the strings will never match.

尝试阅读手册功能,如果你不完全确定它在做什么。

Try reading the manual for a function if you're not COMPLETELY sure what it is doing.

而不是的获取(ARR2)尝试做与fgets(ARR2,100,标准输入)

这篇关于学生信息文件处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆