Reading a null-terminated string

I am working on a file parser and need to read in a null terminated string. It is easy to read fields of known length but I am having trouble grasping the idea of pulling a string of unknown length. Can someone please show me sample code to do this? How can I use fread to ensure I only read until the null character?

Thanks!
Last edited on
fread is for files, so you wont be doing much with null terminated strings;
http://cplusplus.com/reference/clibrary/cstdio/fread/

and heres an example from somewhere on the internet
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <stdio.h>
#include <stdlib.h>

void main( void )
{
   int  count, total = 0;
   char buffer[100];
   FILE *stream;

   if( (stream = fopen( "feof.c", "r" )) == NULL )
      exit( 1 );

   /* Cycle until end of file reached: */
   while( !feof( stream ) )
   {
      /* Attempt to read in 10 bytes: */
      count = fread( buffer, sizeof( char ), 100, stream );
      if( ferror( stream ) )      {
         perror( "Read error" );
         break;
      }

      /* Total up actual bytes read */
      total += count;
   }
   printf( "Number of bytes read = %d\n", total );
   fclose( stream );
}
Last edited on
Topic archived. No new replies allowed.