【解決方法】Cでscanfとfgetsの問題を解決するには?


このコードでは、使用しようとするたびに直面している問題があります スキャン

printf("Enter your choice: ");

最初の部分と

printf("Enter your name: ");

第二部では、それは私がすることを許可しません

fputs("Select your choice to update: ", stdout);

このコードをさらに実行します。 そこで止まりますが、使用するたびに fgets それはのメッセージを与える

printf("Access Denied. Please enter correct Details.\n");

この問題を解決する方法。

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define MAX 15
    #define STRING_LEN 50
    
    int update_info(FILE * fr1, FILE * fr2, FILE * fr3, FILE * fw1, char *name, int *dob);
    /* Global Variables */
    char one='1', two='2', three='3', four='4', five='5', six='6', seven='7', eight='8', choice[MAX];
    static const char * listing[] = {"Name", "Date of birth","ID card number","Phone number","Address","Account","Fixing year","Deposit amount"};
    
    int main(){
    	FILE * fw1 = fopen("new.csv","w");
        FILE * fr1 = fopen("file.csv", "r");
        FILE * fr2 = fopen("file.csv", "r");
    	FILE * fr3 = fopen("file.csv","r");
    
    	int dob[11];
    	char name[15] = "";
    
    	while(one != choice[0]){
    		printf("%c. Create new account\n",one);
    		printf("%c. Update information\n",two);
    
    		printf("Enter your choice: ");
    		scanf("%7s", &choice); 
            // fgets(choice, 7, stdin);         
    
    		if (choice[0] == one){
    			printf("Selected One");
    			break;
    		}
    		else if (choice[0] == two){
    			update_info(fr1, fr2, fr3, fw1, name, dob);
    			break;
    		}
    		else{
    			printf("Please enter the correct information\n");
    			break;
    		}
      }
    	return 0;
    }
   
Next part 

    int update_info(FILE * fr1, FILE * fr2, FILE * fr3, FILE * fw1, char *name, int *dob){
    	int option, i=0, one_by_one;
    	char file_text[100], updated_name[50], str[100], string[STRING_LEN], input[STRING_LEN], saved_word[STRING_LEN];
    	char* word = NULL;
    	short int FLAG = 0;
      
    	printf("Enter your name: ");
    	scanf("%s", &str);
    	// fgets(str, 100, stdin);
    
    	while(fscanf(fr1, "%s", file_text) != EOF){
    		if(!strcmp(file_text, str)){
    			printf("Found. Here are your details. \n");
    			FLAG = 1;
    		}
    	}
    
    	if (!(FLAG == 1)){
    		printf("Access Denied. Please enter correct Details.\n");
    		return -1;
    	}
    	fclose(fr1);
    
    	int c = fgetc(fr2);
    	while(c != EOF){
    		file_text[one_by_one] = c;
    		if(file_text[one_by_one] == ','){
    			file_text[one_by_one] = '\0';
    			printf("Here is your %s: %s\n",listing[i],file_text);
    			one_by_one = 0;
    			i++;
    		}
    		else
    			one_by_one++;
    		c = fgetc(fr2);
       }
      fclose(fr2);
    
      for (option = 1; option <= sizeof(listing)/sizeof(char *); ++option)
    	  printf("%d. Your %s\n", option, listing[option-1]);  
    
      // printf("Select your choice to update: ");
    	fputs("Select your choice to update: ", stdout);
    	scanf("%d", &option);
    
    	if (option == 1){
    		while(fgets(string, STRING_LEN-1, fr3)){
    			printf("Scanned now.\n");
    		}
    	}
     	return 0;
    }

What I have tried:

You can check it and its working fine but two problems I am not understanding.

解決策 1

問題は、.cvs ファイルへのデータの入力方法にあると思います。 テキストファイルだと思います。

文字列が dbl 引用符「fred」で入力された場合、fscanf がデータを読み取ると、次のようになります。

“\”フレッド\”\000\000…\000”

したがって、scanf に dbl 引用符を付けずに fred という名前を付けると、メモリは次のようになります。

“フレッド\000…\000”.

これは、csv ファイルから得たものと比較するために fscanf に与えるものです。 それらは strcmp() に一致しません。

もちろん、その逆も当てはまります。データ ファイルには dbl 引用符はありませんが、scanf には dbl 引用符が指定されています。

コメント

タイトルとURLをコピーしました