'fprintf is being ignored

I'm inserting number, if this number does not equals number that need to be - program will crash, that's what i need. It's some sort of security things

but... when i launch the program, fprintf is being skipped

What am i doing wrong?

#include <iostream>;
#include <fstream>;
#include <string>;
#pragma warning(disable: 4996);
using namespace std;
char record_book_number[8] = "";
struct cipher {
    char cipher_type[1];
    string source_info;
    string ciphered_info;
};

int main() {
    setlocale(LC_ALL, "Russian");
    FILE* config = fopen("D:/Docs/rec_book_number.txt", "w");
    cout << "Input your Record Book's number\n";
    fprintf(config, "%d\n", record_book_number);
    char buffer[10];
    int id_buffer;
    while (1)
    {
        if (!fgets(buffer, 10, config)) { break; }

        id_buffer = atoi(buffer);
        if (1848245 == id_buffer) { printf("yes"); continue; }
    }
    cipher type;
    cout << "Select cipher type:\n 1. Caesar\n 2. Vigenere\n\n\n ";
    cin >> type.cipher_type;
    ifstream file("source.txt");
    cout << "Input line to cipher: \n";
}
`
c++


Solution 1:[1]

You are not using fprintf correctly. You must pass the file, format string, then data to it. See the reference here.

Try replacing

fprintf(config, record_book_number);

with

fprintf(config, "%d\n", record_book_number);

-- Update --

Okay, there is a lot that needs fixing. record_book_number is a string, but its value is never set. So the format string should be "%s\n" however writing an uninitialised string is very dangerous and could crash your program. Also your file is created for writing so you cannot read from it using fgets.

Can you explain what you're expecting this code to do, because it isn't clear what you're trying to achieve.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1