<< split from the "removing the null terminator at the end of string" thread >>

But, Lets say what you have stored in your buffer is the contents of your file that you've open,. How do you get all the content of that file into your string without it stopping after the first null.???

Recommended Answers

All 8 Replies

But, Lets say what you have stored in your buffer is the contents of your file that you've open,. How do you get all the content of that file into your string without it stopping after the first null.???

Sorry dint really get u when u said "without it stopping after the first null.??". Do u want the contents of the file into file string. Is that u wanted. That should be straight forward.

Clarify my question, will post a sample code

ssharish

>How do you get all the content of that file into your
>string without it stopping after the first null.???
It's possible to store multiple strings in one block of memory:

#include <stdio.h>

int main ( void )
{
  char lines[] = "this\n\0is\n\0a\n\0test\n";
  int n = -1;
  int i;

  for ( i = 0; i < 5; i++ )
    n += printf ( "%s", &lines[++n] );

  return 0;
}

But that doesn't happen unless you force it manually, such as I did above, or by skipping over a null character before you start writing the next string. None of the standard string functions will leave a null character anywhere except the end of the string. For example, if you concatenate all of the lines from a stream onto a single string, everything "just works" because the standard library knows how to handle strings correctly:

#include <stdio.h>

#define LINE_SIZE  100
#define LINE_COUNT 10

int main ( void )
{
  char lines[LINE_COUNT * LINE_SIZE];
  int i = 0;

  lines[0] = '\0'; /* Prepare the string for concatenation */

  while ( i < LINE_COUNT ) {
    char buffer[LINE_SIZE];

    if ( fgets ( buffer, sizeof buffer, stdin ) == NULL )
      break;

    strcat ( lines, buffer );
  }

  puts ( lines );

  return 0;
}

Sorry dint really get u when u said "without it stopping after the first null.??". Do u want the contents of the file into file string. Is that u wanted. That should be straight forward.

Clarify my question, will post a sample code

ssharish

What i want is that when i copy/add the data that is in the buffer (in this case the read file) to be fully copied into the string. What i does now i copys the the data that is in the buffer but it thinks that it's finished once is comes accross a nullterminator '\0'. I want it to get all the data in de buffer so not stop at null termininators. It should only stop when there is nothing left to copy.

Thanks by the way for helping

What i want is that when i copy/add the data that is in the buffer (in this case the read file) to be fully copied into the string. What i does now i copys the the data that is in the buffer but it thinks that it's finished once is comes accross a nullterminator '\0'. I want it to get all the data in de buffer so not stop at null termininators. It should only stop when there is nothing left to copy.

Thanks by the way for helping

OK Thats fine, So you wanted all the data from your buffer that is your read file into one single string. Narue had already posted a sample code which uses strcat. And that what you are looking for.

ssharish

Hey i'm trying to do the same thing but with a read file. I want to get all the contents of the file into one single string.. How do i define my LINE_COUNT and LINE_Size

>Hey i'm trying to do the same thing but with a read file.
So open a file and use that stream instead of stdin. The example I gave works with files too.

>How do i define my LINE_COUNT and LINE_Size
Like this:

#define LINE_SIZE [I]<a suitable number>[/I]
#define LINE_COUNT [I]<another suitable number>[/I]

But strcat works only if u want to copy from char to char right? I want to copy from here:
int flen = f.GetLength();
fBuf = new char[flen];
CString crap;
f.SeekToBegin();
pos = f.Read(fBuf,flen);
fBuf[pos]='\0';

To:
CString crap;

I want all the read content into the string without stopping at '\0'

Ooooh, so you're really using C++ and just posted in the wrong forum. That changes absolutely nothing because the CString class supports concatenation. Go RTFM to find a cure for cluelessness.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.