After initializing the c language string, I wrote down the tap in more detail when I input/output the rest of the file.

Asked 2 years ago, Updated 2 years ago, 123 views

int main(void){

int c, i;
//Implement as a link list 
FILE *cities, *members, *reser;// departure city, member list, reservation list 
char ID[20];
char PW[20];
char *ID_B;
char *PW_B; 
char *NEW_ID; 
char *NEW_PW;
char *NEW_NAME;
char *NEW_E_MAIL;
char *buffer; 

INIT: //Initial screen label

printf("welcome to KORENA AIR\n");

printf("If you are member of KOREAN AIR, Please press '1'\n");
printf("If you are to be member of KOREAN AIR, Please press '2'\n");
printf("If you want to find ID, Please press '3'\n");
printf("If you want to find PW, Please press '4'\n");


scanf("%d", &c);
getchar();

buffer = (char*)malloc(20*(sizeof(char)));
ID_B = (char*)malloc(20*(sizeof(char)));
PW_B = (char*)malloc(20*(sizeof(char)));
NEW_ID = (char*)malloc(20*(sizeof(char)));
NEW_PW = (char*)malloc(20*(sizeof(char)));
NEW_NAME = (char*)malloc(20*(sizeof(char)));
NEW_E_MAIL = (char*)malloc(20*(sizeof(char)));  

switch(c) {//Initialize the read position on each initial read of the file( members, 0L, SEEK_SET);
    case 1: // login 

        members = fopen("members.txt", "r"); 


        if(members == NULL){
            printf("Divie connection failed.");
            return 0;
        }


        printf("please enter your ID : ");
        gets(ID);
        printf("please enter your PASSWORD : ");
        gets(PW);
        Read //members.txt file and compare // set file pointer to initial position 
        buffer[0] = '\0';
        ID_B[0] = '\0';
        PW_B[0] = '\0';

        while((fgets(buffer, sizeof(ID, members)) &&(c == 1)){//// having difficulty comparing strings to each other 3/4 

            for(i = 0; i < 20; i++){
                if((buffer[i] == '\t') || (buffer[i] == '\0') || (buffer[i] == ' ') || (buffer[i] == '\n')){
                    break;
                }
            }

            strncpy(ID_B, buffer, i);

            if(strncmp(ID_B, ID, i) == 0){

                fgets(buffer, sizeof(PW), members);

                for(i = 0; i < 20; i++){
                    if((buffer[i] == '\t') || (buffer[i] == '\0') || (buffer[i] == ' ') || (buffer[i] == '\n')){
                        break;
                    }
                }

                strncat(PW_B, buffer, i);

                if(strncmp(PW_B, PW, i) == 0){
                    c = 0;
                }        
            }
            buffer[0] = '\0';
        } 
    if( c == 0){
        printf("LOG - IN SUCCESS.\n");
        break;
    }
    else{
        printf("LOG - IN FAILED. Return to Log-in page.\n");
        goto INIT;
    }

    fclose(members);

Question 1

We're initializing all the strings here, but if we actually print out a string of 20 characters, we can see that there are meaningless characters in the string, so why does this happen? for(i = 0; i < 20; i++){ if((buffer[i] == '\t') || (buffer[i] == '\0') || (buffer[i] == ' ') || (buffer[i] == '\n')){ break; } }

            strncat(PW_B, buffer, i);

            if(strncmp(PW_B, PW, i) == 0){
                c = 0;
            }        
        }

When I received a string from the file input/output, I saw that the end of the meaningful part appeared as a tab rather than a \0, so I processed it in this way, but why does it appear as a tap in the file input/output? I wonder why the difference occurs. Question 2

c string fileinputstream

2022-09-22 10:33

1 Answers

The following initialization clears all zeros: Originally, malloc allocates memory, not initializes it.

memset(PW_B, 0, sizeof(char)*20);

Also, PW_B[0] = '\0' does not initialize the entire PW_B; it only adds '\0' to the 0th.

if((buffer[i] == '\t') || (buffer[i] == '\0') || (buffer[i] == ' ') || (buffer[i] == '\n')){ break; }


2022-09-22 10:33

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.