F.Dinis
F.Dinis

Reputation: 35

How to read a multi-line text file

I'm trying to read a text file and output its content into a struct. (I'm trying to output the contents first, instead of going for the structs right away).

I've tried to read a file, but it doesn't seem to recognize the name, even though they are the same and the file exists.

Text file ("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

My code so far

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;

}

EDIT: Structure to use

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;

I expected the contents to print out in the screen like the text file, but it just gives me a null result (from the debug).

UPDATE: Now it reads the file and puts everything in the buffer, but it still doesn't print out to the screen.

UPDATE END

UPDATE2: It works the way intended, now it's a matter of putting it to a structure.

UPDATE2 END

Upvotes: 0

Views: 121

Answers (2)

Drake Wu
Drake Wu

Reputation: 7170

Instead of reading file with Windows File Api, you could also use the stream method:

#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;

}

UPDATE:

Use C Library Function sscanf(after you've got buffer from the file):

...
_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);

Upvotes: 0

Weather Vane
Weather Vane

Reputation: 34585

The TEXT macro probably does not understand the comma in

TEXT("%s", buffer)

so I would use

_tprintf(TEXT("%s"), buffer); 

or just

_tprintf("%s", buffer); 

both of which keep the arguments for _tprintf separate.

Upvotes: 1

Related Questions