Need help about binary files

穿精又带淫゛_ 提交于 2019-12-08 13:29:22

问题


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.

My current code is:

#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");
}

Thanks in advance.


回答1:


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.

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); is improper code for 3 reasons.

  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 &)

  2. 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.

  3. 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.

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.

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.



来源:https://stackoverflow.com/questions/47342951/need-help-about-binary-files

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!