简体   繁体   English

fprintf仅将部分数据写入文件

[英]fprintf only write part of the data into file

I am trying to get the program to write to a text file using fprintf. 我正在尝试使程序使用fprintf写入文本文件。

Problem 问题

When I use fprintf(stdout,"%x",pbtData[szPos]); 当我使用fprintf(stdout,"%x",pbtData[szPos]); it will successfully print out 12345678. However when I use fprintf(f,"%x",pbtData[szPos]); 但是,当我使用fprintf(f,"%x",pbtData[szPos]);时,它将成功打印出12345678 fprintf(f,"%x",pbtData[szPos]); to write to the file, it will just write the last two digit 78, why is that? 写入文件,它将只写入最后两位78。这是为什么? Shouldn't it write the same result when I use stdout? 使用stdout?时,它不应该写入相同的结果stdout?

fprintf part fprintf部分

static void
print_hex(const uint8_t *pbtData ,const size_t szBytes)
{
size_t  szPos;

  for (szPos = 0; szPos < szBytes; szPos++) {
// printf("%x",pbtData[szPos]);

 FILE *f = fopen("file.txt", "w");
if (f == NULL)
{
  printf("Error opening file!\n");
  exit(1);
}
 fprintf(f,"%x",pbtData[szPos]);

fclose(f);
  }
  printf("\n");
}

FULL CODE 完整代码

#include <stdlib.h>
#include <nfc/nfc.h>



static void
print_hex(const uint8_t *pbtData ,const size_t szBytes)
{
size_t  szPos;

  for (szPos = 0; szPos < szBytes; szPos++) {
// printf("%x",pbtData[szPos]);

 FILE *f = fopen("file.txt", "w");
if (f == NULL)
{
  printf("Error opening file!\n");
  exit(1);
}
 fprintf(f,"%x",pbtData[szPos]);

fclose(f);
  }
  printf("\n");
}


int
main(int argc, const char *argv[])
{
  nfc_device *pnd;
  nfc_target nt;

  // Allocate only a pointer to nfc_context
  nfc_context *context;

  // Initialize libnfc and set the nfc_context
  nfc_init(&context);
  if (context == NULL) {
    printf("Unable to init libnfc (malloc)\n");
    exit(EXIT_FAILURE);
  }

  // Display libnfc version
  const char *acLibnfcVersion = nfc_version();
  (void)argc;
  printf("%s uses libnfc %s\n", argv[0], acLibnfcVersion);

  // Open, using the first available NFC device which can be in order of selection:
  //   - default device specified using environment variable or
  //   - first specified device in libnfc.conf (/etc/nfc) or
  //   - first specified device in device-configuration directory (/etc/nfc/devices.d) or
  //   - first auto-detected (if feature is not disabled in libnfc.conf) device
  pnd = nfc_open(context, NULL);

  if (pnd == NULL) {
    printf("ERROR: %s\n", "Unable to open NFC device.");
    exit(EXIT_FAILURE);
  }
  // Set opened NFC device to initiator mode
  if (nfc_initiator_init(pnd) < 0) {
    nfc_perror(pnd, "nfc_initiator_init");
    exit(EXIT_FAILURE);
  }

  printf("NFC reader: %s opened\n", nfc_device_get_name(pnd));

  // Poll for a ISO14443A (MIFARE) tag
  const nfc_modulation nmMifare = {
    .nmt = NMT_ISO14443A,
    .nbr = NBR_106,
  };
  if (nfc_initiator_select_passive_target(pnd, nmMifare, NULL, 0, &nt) > 0) {

    print_hex(nt.nti.nai.abtUid,nt.nti.nai.szUidLen);

    }






  // Close NFC device
  nfc_close(pnd);
  // Release the context
  nfc_exit(context);
  exit(EXIT_SUCCESS);
}

You need to move the opening/closing of the file out of the loop. 您需要将文件的打开/关闭移出循环。

FILE *f = fopen("file.txt", "w");
if (f == NULL)
{
   printf("Error opening file!\n");
   exit(1);
}

for (szPos = 0; szPos < szBytes; szPos++) {
   // printf("%x",pbtData[szPos]);
   fprintf(f,"%x",pbtData[szPos]);
}
printf("\n");

fclose(f);

You may want to add a space to the output to clearly see the output. 您可能想在输出中添加一个空格以清楚地看到输出。

   fprintf(f, "%x ", pbtData[szPos]);
   //            ^

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM