the problem is a little more complex :(
i have to use it with a Client/Server application in C, where i'm using TCP concurrence for the server.
my txt file can change and can be updated with new records...
if i have this content in my file:
15 nick
25 marcus
18 sarah
The requests sended by the client are for example:
1 2 3 0
where 0 means that the command is termined and when the server receive this command it should have to send to the client:
15 25 18
and then to stop.
Or, if it receive:
2 1 3 0
it have to send back: 25 15 18
I use a function which load each integer and each string in a linked list, like this:
void load_file_in_list(){
llist = (struct NODE *)malloc(sizeof(struct NODE));
llist->code = 0;
llist->next = NULL;
llist->size = 0;
FILE * pFile;
char buffer [100];
int code;
char name[1000];
pFile = fopen (text_file , "r");
if (pFile == NULL) perror ("Error opening file");
else
{
while ( fgets (buffer , 100 , pFile) != NULL )
{
sscanf(buffer,"%d %s \n", &code, name);
append_node(llist, name, code); /* insert values in the list */
size++;
}
fclose (pFile);
}
}
and then, if the client sends me a string, i'm able to send back to it the value associated to that string with this function:
int search_value(struct NODE *llist, char *name, int fd) {
int retval = -1;
int i = 1;
int net;
while(llist != NULL) {
if(strcmp((char*)llist->name, (char*)name) == 0){ /* if there is a result for the request */
net = htons(llist->code);
Writeline(fd, &net, 2); /* send the value to the client */
llist = llist->next;
return i;
}
else i++;
llist = llist->next;
}
if (i != 1) /* else htons(0) si returned */
net = htons(0);
Writeline(fd, &net, 2);
return retval;
}
(i'm using net short to convert integer in net formats)
but, there is a way to use a function like the previous which can send back to the server the values associated to the line number stored in the txt file?
something like:
int search_value(struct NODE *llist, int text_line, int fd)
where the text_line is the line number where is stored the value which i want.
Because i'm able to find a string or a value when there are in the linked list, but what i have to do in order to use line number?
Thanks for all!