[APORTE] Lector de Ficheros

(1/1)

Namida:
¡Muy Buenas!

Aqui os dejo mi pequeño aporte. Se trata de un código que se encarga de cargar 'palabras' de un fichero en una lista simple para que después pueda ser leida e interpretada por otro código. Espero os resulte útil, y cualquier error, duda o crítica que me comuniqueis será bienvenida. De antemano ¡Gracias!  :D

Nombre Original del Archivo: fword.h
Código:

#ifndef _FWORD_
#define _FWORD_
#endif

#ifndef _FSTREAM_
#include <fstream>
#endif

#ifndef NULL
#define NULL 0
#endif

struct L_word{
int wordc;
char* wordv;
struct L_word* next;
};
void L_word_add(struct L_word *&p,char* argv,int argc){
struct L_word* new_node;
struct L_word* aux_node;
new_node=new struct L_word;
new_node->next=NULL;
new_node->wordc=argc;
new_node->wordv=argv;
if(p==NULL){
p=new_node;
}else{
aux_node=p;
while(aux_node->next!=NULL){
aux_node=aux_node->next;
}
aux_node->next=new_node;
}
};
struct L_word* L_fword(char* file_name){
struct L_word* pointer=NULL;
fstream file(file_name,ios::in|ios::binary);
if(file.good()){
bool w=false;
while(!w){
int argc;
char aux;

do{
argc=0;
do{
file.read(reinterpret_cast<char*>(&aux),sizeof(char));
w=file.eof();
argc++;
}while((aux!=' ')&&(aux!='\n')&&(aux!='\t')&&(aux!='\0')&&(aux!=char(13))&&(!w));
}while((argc<2)&&(!w));

char* argv;
argv=new char[argc];

if(w){
file.clear();
file.seekg(-argc+1,ios::cur);
file.read(reinterpret_cast<char*>(argv),sizeof(char)*(argc-2));
file.read(reinterpret_cast<char*>(&aux),sizeof(char));
argv[argc-2]=aux;
}else{
file.seekg(-argc,ios::cur);
file.read(reinterpret_cast<char*>(argv),sizeof(char)*(argc-1));
}
argv[argc-1]='\0';
L_word_add(pointer,argv,argc);
file.seekg(1,ios::cur);
}
}
file.close();
return pointer;
};


Microsoft Visual Studio 2008

Navegación

[0] Índice de Mensajes