我正在尝试读取文本文件并将其内容输出到结构中。 (我正在尝试先输出内容,而不是立即使用结构。)
我尝试读取文件,但是即使它们相同并且文件存在,它似乎也无法识别名称。
文本文件(“ settings.txt”)
vida 3
vel_bola 1
vel_barreira 1
tamanho 15
quant_tijolos 30
tipo_tijolo 1
vel_brinde 1
tipo_brinde 1
tempo_brinde 60
prob_normal 70
prob_resist 20
prob_magico 10
sound 1
到目前为止我的代码
int _tmain(int argc, LPTSTR argv[]) {
#ifdef UNICODE
_setmode(_fileno(stdin), _O_WTEXT);
_setmode(_fileno(stdout), _O_WTEXT);
#endif
//jogo j;
HANDLE hFile;
byte buffer[BUFFERSIZE] = { 0 };
DWORD nIn = 0;
LPCWSTR fName = TEXT("settings.txt");
hFile = CreateFile(fName, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE) {
_tprintf(TEXT("Error opening the file %s!\n\n"), fName);
}else
_tprintf(TEXT("File %s Open!\n\n"), fName);
if (ReadFile(hFile, buffer, BUFFERSIZE - 2, &nIn, NULL) == FALSE) {
_tprintf(TEXT("It wasn't possible to read the file %s\n\n"), fName);
CloseHandle(hFile);
return -1;
}
if (nIn > 0 && nIn <= BUFFERSIZE - 2) {
buffer[nIn] = TEXT('\0');
}
_tprintf(TEXT("%s", buffer));
return 0;
}
编辑: 使用结构
typedef struct Jogo {
unsigned vidas;
unsigned vel_bola;
unsigned vel_barreira;
unsigned tamanho_barreira;
unsigned quant_tijolos;
unsigned tipo_tijolo;
unsigned vel_brinde;
unsigned tipo_brinde;
unsigned tempo_brinde;
unsigned prob_normal;
unsigned prob_resist;
unsigned probm_magico;
BOOL sound;
}jogo;
我希望内容像文本文件一样在屏幕上打印出来,但是它给我的结果是空的(来自调试)。
更新:现在,它读取文件并将所有内容放入缓冲区,但仍不会打印到屏幕上。
更新结束
UPDATE2:它可以按预期的方式工作,现在只需将其放入结构即可。
UPDATE2 END
答案 0 :(得分:1)
TEXT
宏可能不理解逗号
TEXT("%s", buffer)
所以我会用
_tprintf(TEXT("%s"), buffer);
或者只是
_tprintf("%s", buffer);
两者都将_tprintf
的参数分开。
答案 1 :(得分:0)
除了使用Windows File Api读取文件外,还可以使用流方法:
#include <windows.h>
#include <fstream>
#include <iostream>
#include <tchar.h>
using namespace std;
#define BUFFERSIZE 1024
typedef struct Jogo {
unsigned vidas;
unsigned vel_bola;
unsigned vel_barreira;
unsigned tamanho_barreira;
unsigned quant_tijolos;
unsigned tipo_tijolo;
unsigned vel_brinde;
unsigned tipo_brinde;
unsigned tempo_brinde;
unsigned prob_normal;
unsigned prob_resist;
unsigned probm_magico;
BOOL sound;//BOOL is defined as int;
}jogo;
int _tmain(int argc, LPTSTR argv[]) {
#ifdef UNICODE
_setmode(_fileno(stdin), _O_WTEXT);
_setmode(_fileno(stdout), _O_WTEXT);
#endif
jogo j;
unsigned int arr[13] = { 0 };
char memmber[BUFFERSIZE] = { 0 };
ifstream OpenFile("settings.txt");
for (int i = 0; i < 13; i++)
{
OpenFile >> member;
OpenFile >> arr[i];
_tprintf(TEXT("%s %d\n"), member,arr[i]);//Also need to ensure the order in .txt file is the same as the struct members.
}
memcpy(&j, arr,sizeof(arr));//Since the memory of struct members is the same as arry, so we can convert it with memory copy.
return 0;
}
更新:
使用C库函数sscanf
(从文件中获取缓冲区后):
...
_tprintf(TEXT("%s"), buffer);
char temp[20] = { 0 };
sscanf_s(buffer,"%s %d %s %d %s %d %s %d %s %d %s %d %s %d %s %d %s %d %s %d %s %d %s %d %s %d",temp,&j.vidas,temp,&j.vel_bola, temp, &j.vel_barreira, temp, &j.tamanho_barreira, temp, &j.quant_tijolos, temp, &j.tipo_tijolo, temp, &j.vel_brinde, temp, &j.tipo_brinde, temp, &j.tempo_brinde, temp, &j.prob_normal, temp, &j.prob_resist, temp, &j.probm_magico, temp, &j.sound);