retrieve whole line of string from file

Reply

Join Date: Jan 2007
Posts: 58
Reputation: jobs is an unknown quantity at this point 
Solved Threads: 0
jobs jobs is offline Offline
Junior Poster in Training

retrieve whole line of string from file

 
0
  #1
Dec 24th, 2007
Line by line I need to get the whole content in a line from a file. I have looked at fgetc, fgets. According to fgets, I need to specify max number of characters to read.

If I have file content like this:

1235690,9087657788888770000,89977553223456789\n

I wouldn't know how max characters in the file, is there a way to tell C to just read until \n?
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 290
Reputation: kv79 is an unknown quantity at this point 
Solved Threads: 7
kv79 kv79 is offline Offline
Posting Whiz in Training

Re: retrieve whole line of string from file

 
0
  #2
Dec 24th, 2007
Originally Posted by jobs View Post
Line by line I need to get the whole content in a line from a file. I have looked at fgetc, fgets. According to fgets, I need to specify max number of characters to read.

If I have file content like this:

1235690,9087657788888770000,89977553223456789\n

I wouldn't know how max characters in the file, is there a way to tell C to just read until \n?
I do not meet that command .
You can use for small files < 3.2MB;
char *t;
FILE *fp;
fgets( t, 32767, fp);
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,031
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 177
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: retrieve whole line of string from file

 
0
  #3
Dec 24th, 2007
Originally Posted by jobs View Post

I wouldn't know how max characters in the file, is there a way to tell C to just read until \n?
Yes there's a way in C to just read until the '\n'. Nevertheless, that will not help you in what you want to do.
Create a buffer with an extra long capability; say buffer[255]. Use fgets() to read the line.
Then, remove the '\n' from buffer[ strlen ( buffer) - 1 ] if it is there.
Last edited by Aia; Dec 24th, 2007 at 8:19 pm.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 290
Reputation: kv79 is an unknown quantity at this point 
Solved Threads: 7
kv79 kv79 is offline Offline
Posting Whiz in Training

Re: retrieve whole line of string from file

 
0
  #4
Dec 25th, 2007
I rather listen the tips.

remove it with
  1. len = strlen( buffer);
  2. if( buffer[len-1] == '\n')
  3. buffer[len-1] = '\0';
Last edited by kv79; Dec 25th, 2007 at 6:00 am.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,630
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 718
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: retrieve whole line of string from file

 
0
  #5
Dec 25th, 2007
Originally Posted by elite1986 View Post
I do not meet that command .
You can use for small files < 3.2MB;
char *t;
FILE *fp;
fgets( t, 32767, fp);
How did you come up with 32767? If your files are smaller than 3.2MB, you still have to account for up to 3.2MB, which is 3355443, not 32767. And this still doesn't solve the problem because you're wasting oodles of memory[1].

>I wouldn't know how max characters in the file, is there a way to tell C to just read until \n?
You can guess at the size or use a "big enough" size for your array, but both of those are wasteful assumptions that can fail. So there's no way to do it safely without writing your own function:
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. /**
  6.   <summary>Read an arbitrarily long line from input</summary>
  7.   <param name="in">in The input stream to read from</param>
  8.   <returns>
  9.   On success, a pointer to the new line is returned. If memory
  10.   allocation fails, end-of-file is reached, or a read error occurs
  11.   before storing any characters, a null pointer is returned.
  12.   </returns>
  13.   <remarks>
  14.   jsw_getline returns a pointer that must be freed
  15.   </remarks>
  16. */
  17. char *jsw_getline ( FILE *in )
  18. {
  19. char buffer[BUFSIZ]; /* Buffer for partial lines */
  20. char *result = NULL; /* Buffer for the complete line */
  21. size_t size = 0; /* Amount of memory assigned to result */
  22.  
  23. /* In the average case, this should only run once */
  24. while ( fgets ( buffer, sizeof buffer, in ) != NULL ) {
  25. size_t len = strlen ( buffer );
  26. char *save = realloc ( result, size + len + 1 );
  27.  
  28. /* If allocation fails, take what we've got */
  29. if ( save == NULL )
  30. break;
  31.  
  32. /* Update the result buffer */
  33. strcpy ( save + size, buffer );
  34. result = save;
  35. size += len;
  36.  
  37. /* Finish up if we found a newline */
  38. if ( result[size - 1] == '\n' ) {
  39. result[size - 1] = '\0';
  40. break;
  41. }
  42. }
  43.  
  44. return result;
  45. }
[1] That's assuming you simply omitted t's memory allocation for brevity. If you think that you can use a pointer without pointing it to memory that you own then your entire example is severely broken.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 290
Reputation: kv79 is an unknown quantity at this point 
Solved Threads: 7
kv79 kv79 is offline Offline
Posting Whiz in Training

Re: retrieve whole line of string from file

 
0
  #6
Dec 25th, 2007
Narue ,can you be so nice and explain me why do you think that this code read will until '\0'.
I am a noob .
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,630
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 718
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: retrieve whole line of string from file

 
0
  #7
Dec 26th, 2007
>Narue ,can you be so nice
You must be new here.

>explain me why do you think that this code read will until '\0'
I don't think it reads until '\0' because that's not what it does. If you bothered to read the comments I so nicely placed in the code then you would know exactly what I think it does. It reads an arbitrarily long line (ie. up to '\n' or end-of-file if there isn't a newline) and returns a pointer to the dynamically allocated memory where the line is stored, or a null pointer if an error occurs.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,031
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 177
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: retrieve whole line of string from file

 
0
  #8
Dec 26th, 2007
Concerning:
  1. /* jsw_getline returns a pointer that must be freed */

If the pointer returned by jsw_getline function is used as
  1. printf( "%s\n", jsw_getline( fp ) );
the ability of freeing that memory is lost. Correct? Is that what constitute a memory leak?

If a pointer is initialized to the function return
  1. fjpt = jsw_getline ( fp );
does
  1. free( fjpt );
take care of freeing that originally allocated dynamic memory?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,630
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 718
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: retrieve whole line of string from file

 
0
  #9
Dec 26th, 2007
Originally Posted by Aia View Post
Concerning:
  1. /* jsw_getline returns a pointer that must be freed */

If the pointer returned by jsw_getline function is used as
  1. printf( "%s\n", jsw_getline( fp ) );
the ability of freeing that memory is lost. Correct? Is that what constitute a memory leak?
Correct.
Originally Posted by Aia View Post
If a pointer is initialized to the function return
  1. fjpt = jsw_getline ( fp );
does
  1. free( fjpt );
take care of freeing that originally allocated dynamic memory?
Yes, that's it exactly. Personally I hate that kind of interface because it's not obvious that the memory needs to be freed, and because of that the function is more likely to be used in such a way that memory leaks show up. This is where garbage collection or destructors are ideal. You can keep the interface simple but still avoid resource leaks.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 2
Reputation: srikanth1212k is an unknown quantity at this point 
Solved Threads: 0
srikanth1212k srikanth1212k is offline Offline
Newbie Poster

Re: retrieve whole line of string from file

 
0
  #10
Aug 21st, 2008
I think you can try this:
I tried this in gcc compilers, since 'a' optional argument will be present in gcc.

main()
{
char *no;
printf ("Enter a really large number: ");
fscanf (stdin, "%s[0-9,]", &no);
printf ("The number you typed: %s", no);
free (no);
}

This program will also take ',' in between. you can remove then and use other library functions to convert from string format to integer format. Note this will not take floating point, you need to makeup the string such that it takes the floating point number.
Reply With Quote Quick reply to this message  
Reply

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



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC