c - 了解 sscanf 格式化

标签 c formatting scanf

首先,是的,这是作业,是的,我一直在尝试自己解决,是的,我在 SO 上阅读了类似的问题,但没有找到我需要的帮助。

我有一个正在读入结构的 .txt 文件,我真的很难理解 sscanf 的格式。我现在正处于尝试任何事情的地步,所以我担心如果我做对了,那将是因为我很幸运,而不是因为我真正理解我在做什么,也就是在哪里,希望你们这些好人能帮忙。

这是来自 .txt 的示例数据

4, Ben Appleseed, 1587 Apple Street, Salt Lake City, UT, 80514
2, Terri Lynn Smith, 1234 Slate Street, Cincinnati, OH, 45242

注意:每个“字段”由空格、逗号或制表符分隔。有些条目没有中间名,否则每个条目都遵循相同的模式。 (如果有人对如何处理没有所有 8 个字段的行有建议,我愿意提供帮助)

这是我的结构:

typedef struct
{
  long lngRecordID;
  char strFirstName[50];
  char strMiddleName[50];
  char strLastName[50];
  char strStreet[100];
  char strCity[50];
  char strState[50];
  char strZipCode[50];
} udtAddressType;

这是我填充结构的例程

void AddAddressToArray(char strBuffer[], udtAddressType audtAddressList[])
{
  int intIndex = 0;

  for (intIndex = 0; intIndex < strBuffer[intIndex]; intIndex += 1)
  {

    if(sscanf(strBuffer, "%d, %s %s %s %s %s %s %s ",
        &audtAddressList[intIndex].lngRecordID,
        &audtAddressList[intIndex].strFirstName,
        &audtAddressList[intIndex].strMiddleName,
        &audtAddressList[intIndex].strLastName,
        &audtAddressList[intIndex].strStreet,
        &audtAddressList[intIndex].strCity,
        &audtAddressList[intIndex].strState,
        &audtAddressList[intIndex].strZipCode) != 8)
       {
          break;
       }
  }

}

这给了我一个输出:

  Address #50 -------------------------
     Address ID:            4
     First Name:            Ben
     Middle Name:           Appleseed,
     Last Name:             1587
     Street Address:        Apple
     City:                  Street,
     State:                 Salt
     Zip Code:              Lake

那是不对的。

我不明白如何指定我希望三个地址字段在一行上。 我一直在阅读的很多内容只会让我更加困惑。

将文件加载到数组中的函数:

void PopulateAddressList( udtAddressType audtAddressList[])
{
  // Declare a file pointer
  FILE* pfilInput = 0;
  int intResultFlag = 0;
  char strBuffer[50] = "";
  char chrLetter = 0;
  int intIndex = 0;



  // Try to open the file for reading
  intResultFlag = OpenInputFile("c:\\temp\\Addresses1.txt", &pfilInput);

  // Was the file opened?
  if (intResultFlag == 1)
  {

      // Yes, read in records until end of file( EOF )
      while (feof(pfilInput) == 0)
      {
        // Read next line from file
        fgets(strBuffer, sizeof(strBuffer), pfilInput);

        AddAddressToArray(strBuffer, &audtAddressList[intIndex]);

        intIndex += 1;      
      }

    // Clean up
   fclose(pfilInput);
  }


}

非常感谢任何帮助!

最佳答案

使用扫描集来捕获子串。 %200[^,], 将扫描逗号以外的所有内容,最多 200 个字符到 char strSub[201];。根据需要,sscanf strSub 来捕获字段。

if(sscanf(strBuffer, "%d, %200[^,], %99[^,], %49[^,], %49[^,],%49s",
    &audtAddressList[intIndex].lngRecordID,
    strSub,
    audtAddressList[intIndex].strStreet,
    audtAddressList[intIndex].strCity,
    audtAddressList[intIndex].strState,
    audtAddressList[intIndex].strZipCode) == 6)
{
    //sscan the fields
    fields = sscanf ( strSub, "%s%s%s",
        audtAddressList[intIndex].strFirstName,
        audtAddressList[intIndex].strMiddleName,
        audtAddressList[intIndex].strLastName);
    if ( fields == 2) {//only two fields
        //assume that the second field was for last name so copy middle to last
        strcpy (
            audtAddressList[intIndex].strLastName,
            audtAddressList[intIndex].strMiddleName);
        //set middle as blank
        audtAddressList[intIndex].strMiddleName[0] = '\0';
    }
}
else {
    break;
}

关于c - 了解 sscanf 格式化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45402761/

相关文章:

C语言: why get 1s padded to the byte when byte starts with 1?

c - 外部变量 C

c - 如何将字符串拆分为 C 中的标记?

date - 使用单元格格式值的 IF 语句

c# - 如何判断格式化字符串中的替换次数?

c - 如何在C中搜索文件中的单词,将其放入数组中然后将其删除

c - 如何使用 strtok 打印二进制数?

C printf 格式化

c++ - 由于 istream 中的杂散\n 而不是 scanf() 或 cin,gets () 的结果不正确?

C编程-从文本文件中读取数字