在此程序中,我无法使用fgets()或gets()在字符串c [](在add_diary()中)中获取输入,该程序只是跳过了输入过程,请告诉我我在做什么错。我只是无法开始。在日记中,最重要的是写东西,而这是“您不能写,因为cuz fgets()无法正常工作”。

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

void access();
void diary_main(char user[],char pass[]);
void diary_login();
void add_diary(unsigned dd, unsigned mm, unsigned yy);
void add_entry(unsigned choice);
void del_entry(unsigned dd, unsigned mm, unsigned yy);
void update_entry(unsigned dd, unsigned mm, unsigned yy);
void logout();

void main() {
    access();
}

void access() {
    char password[10],username[20], ch;
    int i;
    printf("Enter User name: ");
    gets(username);
    printf("Enter the password (8 characters):");
    for(i=0;i<8;i++) {
            ch = getch();
            password[i] = ch;
            ch = '*' ;
            printf("%c", ch);
     }
     password[i] = '\0';
     /*printf("\nYour password is :");
     for(i=0;i<8;i++) {
    printf("%c", password[i]);*/
    diary_main(username, password);
}

void diary_main(char user[],char pass[]) {
    /*FILE *fptr;
    //char file_name[10];
    //sprintf(file_name, "user%s.txt", user);
    fptr = fopen("login.txt", "w");
    fprintf(fptr,"\n%s",user);
    fclose(fptr);*/
    diary_login();
}

void diary_login() {
    unsigned choice;
    printf("\nEnter your choice\n");
    printf("1. Add Diary Entry\n");
    printf("2. Delete Diary Entry\n");
    printf("3. Update Diary Entry\n");
    printf("4. Logout\n");
    printf("5. Exit\n");
    scanf("%d", &choice);
    if (choice == 1) {
        add_entry(choice);
    }
    else if (choice == 2) {
        add_entry(choice);
    }
    else if (choice == 3) {
        add_entry(choice);
    }
    else if (choice == 4) {
        logout();
    }
    else if (choice == 5) {
        exit(1);
    }
    else {
        //printf("%d", choice);
        printf("Enter correct choice");
        diary_login();
    }
}

void add_entry(unsigned choice) {
    unsigned dd, mm, yy;
    printf("Enter date (DD/MM/YY):");
    scanf("%u/%u/%u", &dd, &mm, &yy);
    //printf("%u/%u/%u", dd, mm, yy);
    if ((dd>31 || mm>12) || (dd>29 && mm == 2) || (yy!=17)) {
        printf("\nEnter correct date\n");
        add_entry(choice);
    }
    else {
        //printf("else");
        switch(choice) {
            case 1: add_diary(dd, mm, yy);
                    break;
            case 2: del_entry(dd, mm, yy);
                    break;
            case 3: update_entry(dd, mm, yy);
                    break;
        }
    }
}

void add_diary(unsigned dd, unsigned mm, unsigned yy) {
    //printf("add_diary");
    long file_extra;
    char file_name[50], c[5000];
    FILE *fptr;
    file_extra = (((dd*100)+mm)*100)+yy;
    sprintf(file_name, "diary%ld.txt", file_extra);
    fptr = fopen(file_name, "w");
    /*if(fptr == NULL){
       printf("Error");
       exit(1);
    }*/
    printf("\nStart writing diary..\n");
    scanf("%s", c);
    //fgets(c, sizeof(c), stdin);
    //gets(c);
    fprintf(fptr,"%s",c);
    fclose(fptr);
}

void del_entry(unsigned dd, unsigned mm, unsigned yy) {
     long file_extra;
     char file_name[50];
     file_extra = (((dd*100)+mm)*100)+yy;
     sprintf(file_name, "diary%ld.txt", file_extra);
     int status;
     status = remove(file_name);
     if( status == 0 ) {
        printf("Entry deleted successfully.\n");
     }
     else {
        printf("Unable to delete the file\n");
    }
}
void update_entry(unsigned dd, unsigned mm, unsigned yy) {
    long file_extra;
    char file_name[50], c[5000];
    file_extra = (((dd*100)+mm)*100)+yy;
    sprintf(file_name, "diary%ld.txt", file_extra);
    FILE *fptr;
    fptr = fopen(file_name, "w");
    printf("\nStart editing diary..\n");
    scanf("%s", c);
    //fgets(c, sizeof(c), stdin);
    //gets(c);
    fprintf(fptr,"%s",c);
    fclose(fptr);
}
void logout(){

}

最佳答案

据我了解,这是scanf的工作方式:

假设您让他们输入“ Hello World”,实际上输入的是“ Hello World \ n”。您可以通过在数组('\ n')中选择n-1并将其替换为终止符('\ o')来解决此问题。

关于c - 无法接受字符串输入,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40259687/

10-11 21:21
查看更多