Thread: Char To String & Save In Array.

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    27

    Char To String & Save In Array.

    I am currently taking data from a database that is in the form of char data and attempting to convert to a string and then saving in a char array. It appears that these strings have to be saved in a two dimension array. The first array or inner array containing each element of the date in the form of chars. The outer array then contains each record or occurance of the date. Can someone help me in copying the string data into the array? Listed below is what I have so far.

    Code:
    char Date1[10] = {};
    vFieldDate      = rec->Fields->GetItem("Date")->Value;
    
    
    for(int r = 0; r < 1; r++)
    	{
    	for(int c = 0; c < 10; c++)
    	       {
    	       strcpy (Date1,vFieldDate);
    	       strcpy (Date[r][c],Date1[10]);
    	       printf ("%s",Date[r][c]);
    	       }
    	}

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    What's the point copying the data twice?
    Code:
    strcpy (Date1,vFieldDate);
    strcpy (Date[r][c],Date1[10]);
    The program needs to compare dates also in case of duplicates.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    27
    Second version of strcpy is not necessary.

  4. #4
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Code:
    char Date1[10] = {};
    I don't think it's standard C.
    Even with gcc C99 -pedantic gives:
    warning: ISO C forbids empty initializer braces

  5. #5
    Registered User
    Join Date
    Nov 2010
    Posts
    27

    Answer.

    Through research the following was derived from an MSDN website:

    Code:
    #define ARR 65500            //Total Records
    #define TARGSIZE 11         //Field Length
    
    char Date[ARR][TARGSIZE] = {};
    
    orig = rec->Fields->GetItem("Time")->Value;  //ADO Data
    
    // Convert to a char*
    const size_t newsize = 100;
    char nstring[newsize];
    strcpy_s(nstring, (char *)orig);          //Typecast & Copy Data Elements
    strncpy(Date[cyc],nstring,TARGSIZE - 1);//Copy String To Array
    printf("%s",nstring);
    printf("\n");
    Original MSDN website:
    [url=http://msdn.microsoft.com/en-us/library/ms235631(v=vs.80).aspx]
    Last edited by mcertini; 01-27-2011 at 10:44 PM. Reason: Replace crossref. website.

  6. #6
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Did you notice the line
    // convert_from_wchar_t.cpp
    C++

  7. #7
    Registered User
    Join Date
    Nov 2010
    Posts
    27
    Bayint Naung,

    Yes, I did note it and successfully converted with this conversion as well. Is this a better conversion?

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I don't know fully what you're doing, but a database is likely to keep "strings" as char's, and not as strings. (That is, the char's are not null terminated to elevate them to string status, within the data). Instead, the database program will add the '\0' char to the data, if and when it's needed for it's operations. That saves a database a bit of space.

    Are the record fields variable, or fixed in their length? Which format do you want to use for your final field and record type? That's critical to know, IMO.

  9. #9
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unable to compare string with 'getter' returned string.
    By Swerve in forum C++ Programming
    Replies: 2
    Last Post: 10-30-2009, 05:56 PM
  2. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  3. comparing fields in a text file
    By darfader in forum C Programming
    Replies: 9
    Last Post: 08-22-2003, 08:21 AM
  4. char array to string
    By Bad_Scooter in forum C++ Programming
    Replies: 5
    Last Post: 07-27-2003, 10:03 AM
  5. Character arrays
    By PsychoBrat in forum C++ Programming
    Replies: 7
    Last Post: 06-21-2002, 12:02 PM