Results 1 to 3 of 3

Thread: How to convert char * to char []?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2007
    Posts
    27

    How to convert char * to char []?

    Hai this is senthil...

    i'm working in the field of VC++....

    Now i'm trying to writing a string into a file which is fetched from the
    mysql database....

    i tried the following codings for the above process

    i had tried to write a simple string into a file(any file such as xml,txt
    or html)....

    if we put .txt.. then the file automatically saving as txt..

    But i dont know how to retrive a string from the Mysql database and
    write that retrived string to a file

    My codings are below...
    --------------------------------------------------------------------
    #include<stdio.h>
    #include <mysql.h>
    #include <conio.h>
    #include <windows.h>
    #include<string.h>

    #define host "192.168.0.2"
    #define username "root"
    #define password ""
    #define database "indesign"

    void main()
    {

    MYSQL *conn;
    conn = mysql_init(NULL);

    mysql_real_connect(conn,host,username,password,dat abase,0,NULL,0);
    MYSQL_RES *res_set;

    MYSQL_ROW row;

    mysql_query(conn,"SELECT text FROM sen1");
    res_set = mysql_store_result(conn);
    unsigned int numrows = mysql_num_rows(res_set);


    //For Creating a file
    HANDLE hFile;
    DWORD wmWritten;
    char strData[] = row[0];
    hFile = CreateFile("C:\\test2.txt",GENERIC_READ|GENERIC_WR ITE,
    FILE_SHARE_READ,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NO RMAL,NULL);

    while ((row = mysql_fetch_row(res_set)) != NULL)
    {


    WriteFile(hFile,strData,(DWORD)(sizeof(strData)),& wmWritten,NULL);
    CloseHandle(hFile);
    }
    mysql_close(conn);
    getch();

    }

    --------------------------------------------------------------------------

    in row[0] i just put a string "senthil"...

    But if there is no db connectivity the above program works
    supervly... it creates xml, html, txt any type of files.

    Now just i'm writing that string "senthil" which is retrived from the database
    to a text file...

    But it is not working..

    My database is like this
    --------
    text
    --------
    senthil

    i'm just trying to retrive the above string(senthil)...

    Wen i run the above program... the following error occurs like this

    error C2440: 'initializing' : cannot convert from 'char *' to 'char []'
    There are no conversions to array types, although there are conversions to references or pointers to arrays

    i'm a beginner to C++ concepts..

    What shall i do for this?

    can any one plzz help me?

    thanks
    senthil..

  2. #2
    Hyperactive Member
    Join Date
    Oct 2006
    Posts
    354

    Re: How to convert char * to char []?

    a char[] and a char * are different. You don't need to convert them.

    char[] means it is an array of characters.
    char * is a pointer to a character.
    One of the other properties of pointers in c++ is it can point to an array. In this case it is an array characters or char[].

    Remember all a pointer is a memory address. All it does it point to a location in memory.

    this document explains it pretty well.
    http://www.cs.bu.edu/teaching/cpp/string/array-vs-ptr/

    this is a general document on pointers.
    http://www.cplusplus.com/doc/tutorial/pointers.html

    Code:
    char label[] = "Single";
    char label2[10] = "Married";
    char *labelPtr;
    
    labelPtr = label;
    Last edited by superbovine; Jan 3rd, 2007 at 01:01 PM.

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: How to convert char * to char []?

    Use code tags when posting code. Never use void main, main returns an int.

    Now, MYSQL_ROW looks like an opaque type to me, so I'm pretty sure you shouldn't be using the index operator on it in the first place. What's the line that throws the error supposed to mean, anyway?
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width