需要有关二进制文件的帮助 [英] Need help about binary files

查看:91
本文介绍了需要有关二进制文件的帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在创建代码时遇到了麻烦,该代码创建了一个包含多行的bin文件,每行每行包含6个值.例如: 我正在做一个小项目,每行将是一个密码",因此我的系统将加载此文件并从中读取所有密码,并在更新时以此类推. 我尚未实现读取文件的方式,因为我正在努力操作文件. 该项目将在具有Raspberry pi的GPIO功能的Linux上运行 所以我正在使用Visual Studio创建主要代码.

I'm having trouble creating a code that creates a bin file with multiple lines, each one containing 6 values per line. for example: I'm doing a little project where each line will be a 'password', so my system will load this file and read all passwords from it, when it gets updated and etc. I havent implemented the way to read the file yet because i'm struggling to manipulate the file. This project will be running on linux with GPIO features from Raspberry pi so i'm using Visual Studio for creating the main code.

我当前的代码是:

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char keys[4] = { '1', '2', '3', '4' }; // NOT USING
char temp_key[6];

int incl_key() {
    int i;
    FILE *fp;
    char code = "0";
    int control = 0;
    int var_internal = 0;

    fp = fopen("keys.bin", "wb");
    if (fp == NULL) {
        printf("\n Error opening file. \n");
    }

    printf("write your sequence <6 digits>:\n");

    do{
        scanf("%s", &temp_key);
        fputs(temp_key, fp);
        fputs("\n", fp);
        control++;
    } while (control < 5);
    fclose(fp);
}

int keys_db() {
    return 0;
}

int main() {
    incl_key();

    system("PAUSE");
}

谢谢.

推荐答案

数组对于用户输入的 string 而言太小.

Array is too small for user input as a string.

char temp_key[6];
printf("write your sequence <6 digits>:\n");
scanf("%s", &temp_key);   // poor code


字符串是连续的字符序列,以第一个空字符结尾并包含第一个空字符.

A string is a contiguous sequence of characters terminated by and including the first null character.

当一种类型 1 2 3 4 5 6 Enter ,将'1''2''3''4''5''6''\n'放入stdin. scanf("%s", temp_key);查找前导空白,不查看任何内容,然后读取6个非空白字符,并将其保存在temp_key[0]temp_key[1],.... temp_key[5]中.读取'\n'(空白)后,"%s"指示停止扫描更多内容,将'\n'放回stdin中供以后输入,然后然后尝试附加空字符放入temp_key[6]中,使其成为 string .但是temp_key[]太小了,因此可能发生任何事情-这是未定义的行为.剩下的代码无关紧要.

When one types 1, 2, 3, 4, 5, 6, Enter, that puts '1', '2', '3', '4', '5', '6', '\n' into stdin. scanf("%s", temp_key); looks for leading white-space, sees none and then reads the 6 non-white-space chracters and saves those in temp_key[0], temp_key[1], .... temp_key[5]. Reading the '\n' (a white-space), the "%s" directs to stop scanning for more, puts the '\n' back into stdin for later input and then attempts to append a null character into temp_key[6] to make it a string. But temp_key[] is too small for that and so anything may happens - it is undefined behavior. Remaining code is irrelevant.

scanf("%s", &temp_key);是不正确的代码,原因有3个.

scanf("%s", &temp_key); is improper code for 3 reasons.

    当需要数组的第一个元素的地址来匹配"%s"时,
  1. &temp_key传递数组的地址.这些地址都具有相等的值,但类型不同.这本身就是未定义的行为.然而,在大多数情况下,它像正确的scanf("%s", temp_key);(不是&)

  1. &temp_key passes the address of an array when the address of the first element of the array is needed to match "%s". Both of those addresses have equivalent values, yet different types. This itself is undefined behavior. Yet the vast majority of the time, it "works" like the correct scanf("%s", temp_key); (no &)

scanf("%s", temp_key);对读取多少数据没有限制,如上所述,输入6位或更多数字会导致缓冲区溢出和未定义的行为.

scanf("%s", temp_key); offers no limitation on how much data is read and as outlined above, entering 6 or more digits results in a buffer overrun and undefined behavior.

不检查返回值,因此代码不知道事情是否成功. stdin可能已经被关闭(不再输入)或其他问题.

The return value is not checked, so code does not know if things are successful. stdin could have been closed (no more input) or other concerns.

代替使用fgets()来读取用户输入的并将该输入隐蔽为 string .

Instead use fgets() to read a line of user input and covert that input into a string.

#define KEY_N 6
//            key    \n   \0  extra - why be stingy? 
char temp_key[KEY_N + 1 + 1 + 10];

//scanf("%s", &temp_key);
if (fgets(temp_key, sizeof temp_key, stdin)) {
  // user input successfully read!

  temp_key[strcspn(temp_key), "\n"] = '\0'; // lop off potential trailing \n
  if (strlen(temp_key) != KEY_N) Handle_Invalid_Input();
  else GoodToGo();
}


代码也可能有其他问题.


Code may have other issues too.

例如:fp在二进制vs文本模式下以及在linux上运行...所以我正在使用Visual Studio"与担心阅读"keys.bin"(看起来像文本文件)的期望有关.目前,只要OP被视为二进制文件,OP所做的事情就可以了.

E.g.: The fp in binary vs text mode along with "running on linux ... so i'm using Visual Studio" has concerns about the expectations of reading "keys.bin" which appears like a text file. For now, what OP is doing concerning that looks OK as long as it is treated a a binary file.

这篇关于需要有关二进制文件的帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆