Hi to all!

Is there a way to go and read a particular line in a text file?

For example, i have a text file like this:

15 nick
25 marcus
18 sarah

where each line contains an integer (age) and a string (a name).

I'd like to write a function which takes the line number and return the age.
For example, if a call the function with 2 so it has to return 25, because at the line 2 there is marcus who is 25.

Is it possible to find the value by using line numbers?
I have to use it in a client/server application.

The client can sends commands like FIND231 and the server has to send to it 25 18 15

Thanks a lot!

sequentially read the file line-by-line until you get to the line number you want.

intialize line counter to 0
loop
   read a line
   increment line counter
   Is this the line you want
       Yes, then do something with it and exit the loop
       No, then back to top of loop
end of loop

Well if the file is static (doesn't change), then it might be worth creating an index of where each line starts. You can then seek to the index position and read a line of your choice.

Or read the whole file into a std::vector of std::string

But no, there is no standard API to goto a specific line, without reading all the lines before it.

Or read the whole file into a std::vector of std::string.

But that wouldn't really be C right? ;)

there is no standard API to goto a specific line

Just curious, If we have the index, can't we use seek to get to that line.

I could have sworn this started in the C++ forum.
*shrug*

commented: For not reading the forum names properly. -3
Member Avatar for iamthwee

>The client can sends commands like FIND231 and the server has to send to it 25 18 15

Out of interest, how does it know to look for the lines 2,3 and 1 and not line #231?

Have a look at the link below:

http://www.daniweb.com/tutorials/tutorial45806.html

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!

are you asking if you can send the linked list from client to server? Why would you want to do that? Just have the server create the linked list, unless of course the server doesn't have the file to generate the list.

But if the client already has the linked list then it doesn't need the server.

are you asking if you can send the linked list from client to server? Why would you want to do that? Just have the server create the linked list, unless of course the server doesn't have the file to generate the list.

But if the client already has the linked list then it doesn't need the server.

i'm asking how to do in order to let client send a command like this:

FIND1230

and let the server sends back to the client:

15 25 18

where in command FIND1230
FIND is the command
1 is the first line in the text file
2 is the second line
3 is the this line
and 0 specify the end of the command

and
15 is the age stored in the first line
25 is the age stored in the second line
28 in the age stored in the third line

>>FIND1230
What will the server do if you want to find the 123d line in the text file? obviously that command syntax is lacking. Maybe you could put a comma between the line numbers
FIND1,2,50,123,0
Now the server can process a file that has an unlimited number of lines

also if i send the number in network byte order on 16 bit, i can read from 16 to 16 bit... and convert it in the host format...

mmh...

how can i read a string composed by different number coded in 16 bit and exctrat an int each time?

for example, if i have FIND1230

(i use FIND to trap which command is sended, if the server receives FIND then it have to read next numbers in the string)

where 1 2 3 0 are number coded in 16 bit on network byte order and 0 indicates that the commands is ended?

thanks a lot :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.