I'm trying to write a program that makes an data file, with records
(structs) and makes an index of them in a seperated file.
The first two function seem to work very well but the last function that
needs too read from the file and return a value from the first record
(dedicated for file info) then the whole progam hangs.
I've striped the function so it only try's to read the first record, and it
hangs on the funcion call read. Does anyone knows what is the error in this
code.
All function resides in unistd.h, it's the first time i'm programming under
Linux.
#include "idfile.h"
int init_indexFile (char* fName , size_t size){
rec newR;
size_t length = sizeof(rec);
int i;
int filedescriptor, indexfile ;
char datName[255], idxName[255] ;
strcpy(datName, fName );
strcat(datName, ".dat");
strcpy(idxName, fName );
strcat(idxName, ".idx");
newR.key = size;
newR.next = 1;
strncpy(&(newR.data),"", DATAL);
filedescriptor = open ( datName , O_WRONLY | O_CREAT , 0666);
if (filedescriptor == -1) { perror("create"); return 0; }
write(filedescriptor, &newR,length);
for(i=0; i<=size; i++){
if (i != size){ newR.next = i+1;} else newR.next=-1;
if (write(filedescriptor, &newR, length) != length) { perror("write
1"); };
}
indexfile = open( idxName, O_WRONLY | O_CREAT , 0666 );
close (filedescriptor);
close (indexfile);
indexFile* open_indexFile(char* fName){Quote:}
char idxName[255], datName[255];
indexFile *index = (indexFile*)malloc(sizeof(indexFile));
strcpy(idxName, fName);
strcat(idxName, ".idx");
strcpy(datName, fName);
strcat(datName, ".dat");
if (index->idxfile = open (idxName, O_RDWR ) == -1){ perror("open
idxfile"); return NULL;}
if (index->datfile = open (datName, O_RDWR ) == -1){ perror("open
datfile"); return NULL;}
return index;
void close_indexFile (indexFile* iFile){Quote:}
close(iFile->idxfile);
close(iFile->datfile);
unsigned sizeOf_indexFile (indexFile* iFile){Quote:}
rec *tmprec = (rec*)malloc(sizeof(rec));
size_t length = sizeof(rec);
//simple version only read first record no return to previous point
lseek(iFile->datfile, 0,SEEK_SET );
/************************************
the program keeps* on the next instruction
*************************************/
read(iFile->datfile, tmprec, length);
return 0;
/*************************************Quote:}
** and here a few lines from main.c **
**************************************/
int main(){
indexFile *idx;
init_indexFile ("hlp",10000);
idx = open_indexFile("hlp");
if (idx == NULL ) printf("Nok/n");
printf("%d\n", sizeOf_indexFile(idx));
close_indexFile(idx);
Quote:}