首页 > 解决方案 > 从二进制文件读取 LANG C

问题描述

我无法从 bin 文件中读取。我正在以二进制格式记录一些文本(我认为是正确的),但是我的代码无法读取我编写的文本并将其打印在控制台中。当我打开文件时,信息就在那里(该文档用于存储来自客户端和登录访问的信息)并且我可以登录,但是当我尝试在控制台中读取和打印时它不会显示我。我正在使用结构从文档中获取值,然后我打开文档“fopen()”但是当我尝试在“while”中使用“fread()”读取时,我没有进入,打印不给我正确的信息

我尽量减少代码。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <locale.h>
#include <dir.h>
#include <unistd.h>
#include <time.h>
#include <conio.h>
#define SIZE 1000

typedef struct
{
char id[10];
char nome[100];
char passe[100];
}Pacient;
Pacient p1;

FILE* filepaciente;

//variables
char tmpnome[100], tmppassword[100];

//variable for path
char nomedoc[15];
char pasta[250];
char sessionName[15];
char tmp_name[15];
char fullpath[200];
char tmp_nome[15];

void vedocumento(int* i, char* line, char pacienteData[])
{
    // DEFINE A VARIÁVEL USERDATA COMO UMA STRING VAZIA
    strcpy(pacienteData, "");
    // IF FINDS "|", GO TO THE NEXT CHARACTER
    if (line[*i] == '|')
    {
        (*i)++;
    }
    //WHEN THE CHARACTER IS DIFERENT THEN |, ADD THAT CHARACTER TO THE ARRAY USERDATA
    while (line[*i] != '|') // *i = 0
    {
        strncat(pacienteData, &line[*i], 1); // userData = "" ; userData = userData + carater
        (*i)++;
    }
}
findpath()
{
    //PROCURA O CAMINHO ONDE SE ENCONTRA O PROJETO

    if (getcwd(pasta, sizeof(pasta)) == NULL)
    {
        printf("ERROR!\n");
    }
    else
    {
        strcat(pasta, "\\file\\");
        //CRIA O FICHEIRO COM O NUMERO DE INTERNO
        strcpy(nomedoc, strcat(tmp_nome, ".bin"));
        //JUNTA O CAMINHO COMPLETO AO NOME DO FICHEIRO
        strcat(pasta, nomedoc);
        strcpy(fullpath, pasta);
    }
}

int main() //where i write in the bin file
{
    //GETTING REGISTRY DATA
    fflush(stdin); //para ir so buscar os dados
    printf("\nName pacient: ");
    scanf("%[^\n]", p1.nome);
    printf("\nPassword do pacient %s: ", p1.nome);
    scanf("%s", p1.passe);
    printf("\nID pacient %s: ", p1.nome);
    scanf(" %s", p1.id);
    strcpy(tmp_nome, p1.nome);
    strcpy(tmp_nome, strlwr(tmp_nome));

    findpath();
     if (access(fullpath, F_OK) == 0)
    {
        //IF EXISTS
        printf("Este nome de utilizador já foi registado! Por favor, tente novamente!\n");
    }
    else
    {
        // IF DOESN'T EXIST
        filepaciente = fopen(fullpath, "wb");
        if (filepaciente == NULL)
        {
            printf("\nerror\n");
        }
        
        //size_t elements_written = fwrite(&p1, sizeof(Paciente), 1, filepaciente);
        
        //PRINTING ON FILE
        
        fprintf(filepaciente, "%s|%s|%s\n", p1.nome, p1.passe, p1.id);
        fclose(filepaciente);
    }

  //WHERE I WANT TO PRINT ON THE CONSOLE
{
    printf("Name do pacient: ");
    scanf(" %[^\n]", tmpnome);

    strcpy(tmp_nome, tmpnome);
    strcpy(tmp_nome, strlwr(tmp_nome));//this is to get the name of the document
    
    findpath(); //Find the path to the document
    if (access(fullpath, F_OK) == 0)
    {
        filepaciente = fopen(fullpath, "rb"); //Open the document
        Pacient pacient[100];
        char line[SIZE];
        int nline = 0;
        fgets(line, SIZE, filepaciente);
        int i = 0;
        vedocumento(&i, line, pacient[nline].nome);
        vedocumento(&i, line, pacient[nline].passe);
        nline++;
        //LOOP TO CHECK
        for (i = 0; i < 1; i++)
        {   
            if (strcmp(tmpnome, pacient[i].nome) == 0)
            {   //WRITE INFO
                char buf[SIZE];
                if (filepaciente == NULL)
                { 
                    printf("\nERRO OPENING\n");
                }else printf("\nOPEN THE FILE\n");
                //IF I PUT "==1" DON'T GET IN THE WHILE
                while( fread(&p1, sizeof(p1), 1, filepaciente) == 1) 
                {
                    printf("%c", p1); //NOT SURE IF THIS IS CORRECT
                }
            }
        }
    }
}
}

标签: cwindowsprintingbinaryscanning

解决方案


推荐阅读