| | |
retrieve whole line of string from file
![]() |
•
•
Join Date: Jan 2007
Posts: 58
Reputation:
Solved Threads: 0
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?
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?
•
•
Join Date: Nov 2007
Posts: 290
Reputation:
Solved Threads: 7
•
•
•
•
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?
You can use for small files < 3.2MB;
char *t;
FILE *fp;
fgets( t, 32767, fp);
•
•
•
•
I wouldn't know how max characters in the file, is there a way to tell C to just read until \n?
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.
•
•
Join Date: Nov 2007
Posts: 290
Reputation:
Solved Threads: 7
I rather listen the tips.
remove it with
remove it with
C Syntax (Toggle Plain Text)
len = strlen( buffer); if( buffer[len-1] == '\n') buffer[len-1] = '\0';
Last edited by kv79; Dec 25th, 2007 at 6:00 am.
•
•
•
•
I do not meet that command .
You can use for small files < 3.2MB;
char *t;
FILE *fp;
fgets( t, 32767, fp);
>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:
c Syntax (Toggle Plain Text)
#include <stdio.h> #include <stdlib.h> #include <string.h> /** <summary>Read an arbitrarily long line from input</summary> <param name="in">in The input stream to read from</param> <returns> On success, a pointer to the new line is returned. If memory allocation fails, end-of-file is reached, or a read error occurs before storing any characters, a null pointer is returned. </returns> <remarks> jsw_getline returns a pointer that must be freed </remarks> */ char *jsw_getline ( FILE *in ) { char buffer[BUFSIZ]; /* Buffer for partial lines */ char *result = NULL; /* Buffer for the complete line */ size_t size = 0; /* Amount of memory assigned to result */ /* In the average case, this should only run once */ while ( fgets ( buffer, sizeof buffer, in ) != NULL ) { size_t len = strlen ( buffer ); char *save = realloc ( result, size + len + 1 ); /* If allocation fails, take what we've got */ if ( save == NULL ) break; /* Update the result buffer */ strcpy ( save + size, buffer ); result = save; size += len; /* Finish up if we found a newline */ if ( result[size - 1] == '\n' ) { result[size - 1] = '\0'; break; } } return result; }
I'm here to prove you wrong.
>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.
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.
Concerning:
If the pointer returned by jsw_getline function is used as
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
does
take care of freeing that originally allocated dynamic memory?
c Syntax (Toggle Plain Text)
/* jsw_getline returns a pointer that must be freed */
If the pointer returned by jsw_getline function is used as
c Syntax (Toggle Plain Text)
printf( "%s\n", jsw_getline( fp ) );
If a pointer is initialized to the function return
C Syntax (Toggle Plain Text)
fjpt = jsw_getline ( fp );
C Syntax (Toggle Plain Text)
free( fjpt );
•
•
•
•
Concerning:
c Syntax (Toggle Plain Text)
/* jsw_getline returns a pointer that must be freed */
If the pointer returned by jsw_getline function is used as
the ability of freeing that memory is lost. Correct? Is that what constitute a memory leak?c Syntax (Toggle Plain Text)
printf( "%s\n", jsw_getline( fp ) );
•
•
•
•
If a pointer is initialized to the function return
doesC Syntax (Toggle Plain Text)
fjpt = jsw_getline ( fp );
take care of freeing that originally allocated dynamic memory?C Syntax (Toggle Plain Text)
free( fjpt );
I'm here to prove you wrong.
•
•
Join Date: Aug 2008
Posts: 2
Reputation:
Solved Threads: 0
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.
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.
![]() |
Similar Threads
- Send data on a serial port (C++)
- how to retrieve part of the string (C++)
- C++ I/o (C++)
- wget into php (PHP)
- How do you read a .ini file? (Visual Basic 4 / 5 / 6)
- Schedule program (Python)
- PHP cookie question (PHP)
- To display an icon in a datagrid (VB.NET)
- I've got Trojan.Holax... is this bad? (Viruses, Spyware and other Nasties)
- not-a-virusadware (Viruses, Spyware and other Nasties)
Other Threads in the C Forum
- Previous Thread: Removing an item from head of linked list
- Next Thread: Possible Project in C
| Thread Tools | Search this Thread |
#include adobe api array arrays asterisks binarysearch calculate char cm copyanyfile copyimagefile copypdffile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax database directory dynamic feet fflush fgets file fork forloop frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hacking hardware highest homework i/o include incrementoperators input interest kernel kilometer linked linkedlist linux linuxsegmentationfault list lists locate logical_drives loopinsideloop. match matrix meter microsoft motherboard mqqueue mysql number odf open opensource owf pattern pdf performance pointer posix probleminc process program programming pyramidusingturboccodes radix read recursion recv repetition research scanf scheduling scripting segmentationfault send sequential shape socket socketprograming stack standard string systemcall turboc unix user voidmain() wab win32api windows.h






