问题描述
我在读取 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,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),filepaciente) == 1)
{
printf("%c",p1); //NOT SURE IF THIS IS CORRECT
}
}
}
}
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)