By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,967 Members | 815 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,967 IT Pros & Developers. It's quick & easy.

How to terminate fetching a string from a database by using a NULL character?

P: 61
Hai,

i had tried to write a string which is fetched from a database into a file....

Here are my codings to write that fetched string into a text file...

Expand|Select|Wrap|Line Numbers
  1. HANDLE hFile;
  2. DWORD wmWritten;
  3. hFile =CreateFile"D:\\test.text",GENERIC_READ|GENERIC_WRITE,  FILE_SHARE_READ,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
  4.  
  5. while (( row = mysql_fetch_row(res_set)) != NULL)
  6. {
  7.  
  8.        char str[100];
  9.        char *tmp = str;
  10.        strcpy(str, row[2]);
  11.  
  12.        //For writing a file
  13.        WriteFile(hFile,str,(DWORD)(sizeof(str)),&wmWritten,NULL);
  14.        CloseHandle(hFile);
  15.  }
  16.            mysql_close(conn);
  17.  }
Here row[2] contains a string "senthil"..... when i run the above program

the string "senthil" easily fetched from the database and write into the text

file perfectly... But there are some special garbage characters

like "'EY|6@p@ C~/"

which are also automatically writing into the file....

This is bcoz the "senthil" contains only 7 characters.... but the str array size
is str[100]...

so the remaining space are filled up by these garbage characters and

writing into the file...

so i want to add a null character into string to stop fetching these garbage
characters...

By which method shall i use the null character to stop fetching the garabage
characters....

i'm a beginner to c++...

so any one can help me..
Jan 3 '07 #1
Share this Question
Share on Google+
1 Reply


Ganon11
Expert 2.5K+
P: 3,652
Well, the null character is '\0'. If you always know your array of characters will hold 7 significant characters, you can set the 8th element of the array to the character '\0'. Then, when writing to the file, you can write characters only while that character isn't the null char '\0'
Jan 3 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.