| | |
Read a particular line from a text file
![]() |
•
•
Join Date: Jun 2008
Posts: 5
Reputation:
Solved Threads: 0
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!
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.
C Syntax (Toggle Plain Text)
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
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.
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 no, there is no standard API to goto a specific line, without reading all the lines before it.
>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
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*
•
•
Join Date: Jun 2008
Posts: 5
Reputation:
Solved Threads: 0
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:
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:
(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!

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:
C Syntax (Toggle Plain Text)
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:
C Syntax (Toggle Plain Text)
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!
Last edited by marcusbarnet; Jun 21st, 2008 at 8:58 am.
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.
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.
•
•
Join Date: Jun 2008
Posts: 5
Reputation:
Solved Threads: 0
•
•
•
•
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
![]() |
Similar Threads
- how to read a new line in my text file (C#)
- Read part of text file (PHP)
- Read a specific line from a text file - how to ? (Java)
- delete first line from a text file (C++)
- Read in a string from a text file (C)
- how do i read the last line of a text file? (Python)
- reading a file into code (Java)
- problem with convert jumbled text file to unjumbled text file (C)
Other Threads in the C Forum
- Previous Thread: error: incompatible types in assignment
- Next Thread: What goes wrong?
| Thread Tools | Search this Thread |
#include adobe ansi api array asterisks binarysearch changingto char character cm copyimagefile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax database directory dynamic execv feet fgets file fork forloop frequency function getlasterror givemetehcodez global grade graphics gtkgcurlcompiling hacking hardware highest histogram i/o include incrementoperators infiniteloop input interest kernel keyboard kilometer license linked linkedlist linux linuxsegmentationfault list locate logical_drives looping loopinsideloop. lowest match matrix meter microsoft motherboard mqqueue mysql number odf opensource owf pattern pdf performance pointer posix probleminc process program programming radix recursion recv repetition research reversing scanf segmentationfault sequential shape socket socketprograming standard string systemcall threads turboc unix user voidmain() wab windows.h windowsapi






