Hi all,
I got a problem with this question:
Read the string "hello" include the NULL character in the end of string.
How can I do it ?

Recommended Answers

All 12 Replies

The NULL character is '\0', or just simply 0. Use while loop to check each character in the string.

The NULL character is '\0', or just simply 0. Use while loop to check each character in the string.

Thanks,
Could you give me the sample codes to do this ? Can we just use strcmp function for this situation ? And which is the length of null character 0 or 1 ?

>>Read the string "hello" include the NULL character in the end of string.

What exactly does that mean? Read it from where?

>>Read the string "hello" include the NULL character in the end of string.

What exactly does that mean? Read it from where?

That's what I'm wondering ? This is the original request: The ID field is a string, which has 6 character (include the NULL character in the end): "hello".
That means we need to read "hello" string and the NULL character to ID field so that length of ID field is 6 character. But I don't know how can we do it ?

Read it from the keyboard, a data file, socket stream, or what?
From a data file. ifstream will put the null terminating character at the end for you. You don't have to do anything with that.

#include <fstream>
#include <iostream>
using std::ifstream;
using std::cout;

int main()
{
   ifstream in("filename.txt");
   if( in.is_open() )
   {
      char id[6];
      in >> id;  // read the ID from a data file.
      cout << id << '\n';
   }
}

I can't really figure it out. Why do you (Ancient Dragon) keep posting C++ code in the C section? Am I missing something?

Oops! my mistake, I was thinking we were in the c++ forum. Sorr for that. But the concept is the same.

#include <stdio.h>

int main()
{
   char id[7];
   FILE* fp = fopen("filename.txt", "r");
   fgets(id, sizeof(id), fp); // read the string
   // fgets() puts '\n' at the end so we need to remove it
   if( id[strlen(id)-1] == '\n')
       id[strlen(id)-1] = '\0';
   printf("%s\n", id);
}

Read it from the keyboard, a data file, socket stream, or what?
From a data file. ifstream will put the null terminating character at the end for you. You don't have to do anything with that.

#include <fstream>
#include <iostream>
using std::ifstream;
using std::cout;

int main()
{
   ifstream in("filename.txt");
   if( in.is_open() )
   {
      char id[6];
      in >> id;  // read the ID from a data file.
      cout << id << '\n';
   }
}

Many thanks, Ancient Dragon.
I only need copy "hello" string to ID field ("hello" is a constant), but "hello" only has 5 characters, while the length of ID field is 6 characters. So I need the last character of ID field is NULL. When I used strlen function to get the length of ID field, the result is 5, not 6. What's my problem? Is the length of NULL character is zero?
Here's my code:

char IDField[6];
strcpy(IDField, "hello");
IDField[5] = '\0'; //assign last character to NULL

then just use strcpy() -- it will put the null character at the end for you.

char id[6];
strcpy(id, "Hello");

then just use strcpy() -- it will put the null character at the end for you.

char id[6];
strcpy(id, "Hello");

Got it ! Thanks, man.

in the c language , charset and string end with '\0'

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.