正确缓冲和验证字符串

时间:2016-04-16 02:20:49

标签: c

我的程序中出现了一些错误,它有些基本但是这是我的错误:我正在尝试将一系列字符串正确输入到结构中,似乎用户输入结束于错误的位置。

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

#define buffer 256
struct Fisher
 {
 char  SSN[8]; //9 digit max
 char First_Name[12];
 char Last_Name[15];
 char Phone[10];
 char Email[35];
 }typedef Fisher;

void getFisher(Fisher* pfisher, int i);
void dispFisher(Fisher* pfisher, int i);
void Fisherman_Menu(Fisher* pfisher, int i);
int main()
{
int i = 0;
   Fisher fisherarray[3];
   Fisher* pfisher = &fisherarray[0];
   Fisherman_Menu(pfisher, i);
   return 0;
}
void Fisherman_Menu(Fisher* pfisher, int i)
{

    for(;;)
    {
fflush(stdin);
        printf("-1-Register Fisherman\n");
        printf("-2-Search Fisherman\n");
        printf("-3-Go back to Main Menu\n");
fflush(stdin);
        int choice=0;
        scanf(" %d", &choice);
        if (choice == 1)
        {
         fflush(stdin);
            getFisher(pfisher, i);
            i++;
        }
        if (choice == 2)
            {
                fflush(stdin);
                dispFisher(pfisher, i);
            }
        else if (choice == 3)
        {
            break;  /* Break out of loop */
        }
        else
            printf("Anything else?\n");
    }

    /* When this function returns, you get back to the main menu */
}
void getFisher(Fisher* pfisher, int i)
{
    char input[buffer];
    char* pinput = NULL;
    //===============================
    printf("Enter Social Security Number: ");
    pinput = fgets(input, buffer, stdin);//validate
    strcpy((pfisher+i)->SSN, pinput);
    //(pfisher+i)->SSN = atoi(pinput);

    //==============================
    printf("Enter first name: ");
    pinput = fgets(input, buffer, stdin);//validates name
    strcpy((pfisher+i)->First_Name, pinput);

    //==============================
    printf("Enter last name: ");
    pinput = fgets(input, buffer, stdin);//validates name
            strcpy((pfisher+i)->Last_Name, pinput);

    //==============================
    printf("Enter phone number as a 10 digit number (without any    
        dashes     or spaces): ");
    pinput = fgets(input, buffer, stdin);//validate phone
        strcpy((pfisher+i)->Phone, pinput);
        //(pfisher+i)->Phone = atoi(pinput);

        //===============================
        printf("Enter email address: ");
        pinput = fgets(input, 20, stdin);//validates name
        strcpy((pfisher+i)->Email, pinput);
    }
    void dispFisher(Fisher* pfisher, int i)
    {
    int len;
    int pen;
     printf("ssn is equal to: %s\n", ((pfisher+0)->SSN));
     printf("First Name is equal to: %s\n", ((pfisher+0)->First_Name));
     printf("ssn is equal to: %s\n", ((pfisher+1)->SSN));
      printf("First Name is equal to: %s\n", ((pfisher+1)->First_Name));

    len = strlen(((pfisher+0)->SSN));
    printf("string length = %d\n", len);
    pen = strlen(((pfisher+1)->SSN));
    printf("string length = %d\n", len);
    /*
        int arr = 0;
        char searchSSN[9];
        printf("Enter SSN: ");
              scanf("%s", &searchSSN);

              for(arr = 0; arr < i; arr++)
                {
                    if(strcmp(&searchSSN, (pfisher+arr)->SSN) == 0)

                {

        //printf("I is equal to: %s\n", (pfisher+i)->SSN);
        printf("Fisher\n");
        printf("-------------------------------------------\n");
        printf("%d \n %s \n %s \n %s \n %s ", (pfisher + arr)->SSN, ((pfisher+arr)->First_Name), ((pfisher+arr)->Last_Name), ((pfisher+arr)->Phone), (pfisher+arr)->Email);
        printf("-------------------------------------------\n");

    }


      }

    */
    }

我的SSN输出搞砸了,变成了“123456789(FirstName字符串)
示例:123456789TurboTurkey 如何清理字符串的长度并正确进入SSN

1 个答案:

答案 0 :(得分:0)

没有足够的空间在SSN中存储9位数字。如果要在其中存储9位数,则其大小应为10(9位+ import openpyxl import pandas as pd import clipboard as clp #Copy dataframe to clipboard df.to_clipboard() #paste the clipboard to a valirable cells = clp.paste() #split text in varialble as rows and columns cells = [x.split() for x in cells.split('\n')] #Open the work book wb= openpyxl.load_workbook('H:/template.xlsx') #Get the Sheet sheet = wb.get_sheet_by_name('spam') sheet.title = 'df data' #Paste clipboard values to the sheet for i, r in zip(range(1,len(cells)), cells): for j, c in zip(range(1,len(r)), r): sheet.cell(row = i, column = j).value = c #Save the workbook wb.save('H:/df_out.xlsx') )。如果没有NULL字符,它也会在附近的位置读取..

相关问题