Read a particular line from a text file

Reply

Join Date: Jun 2008
Posts: 5
Reputation: marcusbarnet is an unknown quantity at this point 
Solved Threads: 0
marcusbarnet marcusbarnet is offline Offline
Newbie Poster

Read a particular line from a text file

 
0
  #1
Jun 20th, 2008
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!
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,396
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1466
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: Read a particular line from a text file

 
0
  #2
Jun 20th, 2008
sequentially read the file line-by-line until you get to the line number you want.
  1. intialize line counter to 0
  2. loop
  3. read a line
  4. increment line counter
  5. Is this the line you want
  6. Yes, then do something with it and exit the loop
  7. No, then back to top of loop
  8. end of loop
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Read a particular line from a text file

 
0
  #3
Jun 20th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,841
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 298
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Roasting Maven

Re: Read a particular line from a text file

 
0
  #4
Jun 20th, 2008
Originally Posted by Salem View Post
Or read the whole file into a std::vector of std::string.
But that wouldn't really be C right?
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 125
Reputation: bala24 is an unknown quantity at this point 
Solved Threads: 11
bala24's Avatar
bala24 bala24 is offline Offline
Junior Poster

Re: Read a particular line from a text file

 
0
  #5
Jun 20th, 2008
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.
Last edited by bala24; Jun 20th, 2008 at 9:10 am.
Michelangelo

"The best place to find a helping hand is at the end of your own arm"
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Read a particular line from a text file

 
-1
  #6
Jun 20th, 2008
I could have sworn this started in the C++ forum.
*shrug*
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Read a particular line from a text file

 
0
  #7
Jun 20th, 2008
>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
Last edited by iamthwee; Jun 20th, 2008 at 4:15 pm.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 5
Reputation: marcusbarnet is an unknown quantity at this point 
Solved Threads: 0
marcusbarnet marcusbarnet is offline Offline
Newbie Poster

Re: Read a particular line from a text file

 
0
  #8
Jun 21st, 2008
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:

  1. void load_file_in_list(){
  2.  
  3. llist = (struct NODE *)malloc(sizeof(struct NODE));
  4. llist->code = 0;
  5. llist->next = NULL;
  6. llist->size = 0;
  7.  
  8. FILE * pFile;
  9. char buffer [100];
  10. int code;
  11. char name[1000];
  12.  
  13. pFile = fopen (text_file , "r");
  14. if (pFile == NULL) perror ("Error opening file");
  15. else
  16. {
  17. while ( fgets (buffer , 100 , pFile) != NULL )
  18. {
  19.  
  20. sscanf(buffer,"%d %s \n", &code, name);
  21. append_node(llist, name, code); /* insert values in the list */
  22. size++;
  23. }
  24. fclose (pFile);
  25. }
  26.  
  27. }

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:

  1. int search_value(struct NODE *llist, char *name, int fd) {
  2. int retval = -1;
  3. int i = 1;
  4. int net;
  5.  
  6. while(llist != NULL) {
  7. if(strcmp((char*)llist->name, (char*)name) == 0){ /* if there is a result for the request */
  8. net = htons(llist->code);
  9. Writeline(fd, &net, 2); /* send the value to the client */
  10. llist = llist->next;
  11. return i;
  12. }
  13.  
  14. else i++;
  15. llist = llist->next;
  16.  
  17. }
  18. if (i != 1) /* else htons(0) si returned */
  19. net = htons(0);
  20. Writeline(fd, &net, 2);
  21. return retval;
  22. }

(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!
Last edited by marcusbarnet; Jun 21st, 2008 at 8:58 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,396
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1466
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: Read a particular line from a text file

 
0
  #9
Jun 21st, 2008
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 5
Reputation: marcusbarnet is an unknown quantity at this point 
Solved Threads: 0
marcusbarnet marcusbarnet is offline Offline
Newbie Poster

Re: Read a particular line from a text file

 
0
  #10
Jun 21st, 2008
Originally Posted by Ancient Dragon View Post
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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC