• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1432
  • Last Modified:

assign a char arry to std::string in VC++ 7.0

i was not able to assign any char array with more than 15 charactors into a std::string variable.  the variable will be corrupted.  it works fine with anything less or equal to 15 chars.  same code worked perfectly with VC++6.0.  why is this happening?  my program talks to an API, which expects its args as type of std::string, i have no control over that.  please help.

thanks!
hxia

0
hxia
Asked:
hxia
  • 7
  • 5
  • 2
  • +1
1 Solution
 
ExceterCommented:
Post yout code. Here's an example that should work fine,

#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

int main()
{
    char arr[] = "My name is Exceter, well not really but it is my login name!!!";
    string s = arr;

    cout << s << endl;

    return 0;
}

Are you sure that the char array you are working with is null terminated?

Exceter
0
 
jkrCommented:
Hmm, sounds strange - have you tried

char acLongArray [] = "kkropti89irokvlrotoitk,,,diieuhjmllwoooso88urjmmmmm!";

string str;

str.reserve ( strlen ( acLongArray));

str = acLongArray;

?
0
 
AxterCommented:
Please post your code.

The following should also work:

char acLongArray [] = "kkropti89irokvlrotoitk,,,diieuhjmllwoooso88urjmmmmm!";

string str(acLongArray, acLongArray + sizeof(acLongArray)-1);

or

string str(acLongArray, acLongArray + strlen(acLongArray));
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
hxiaAuthor Commented:
nothing works at this point, i tried both above.  it is sth with VC++ 7.0.   this was originally 6.0 code, then my .net upgraded to vc++ 7.0  then this happened.
0
 
ExceterCommented:
Post your code.
0
 
hxiaAuthor Commented:

      string str;
      char a[100];

      _snprintf(a, 100, "andy testing sjusfna;sdlfjasl;fsd");
      str = a;

above code is just one of a many i tried.  i even started a new project to do this.  i am guessing my string libs are not running right on my machine, i just tried the same code on the other machine in VC++7.0 and it worked just fine.  what should i do at this point?
0
 
AxterCommented:
What header files are you using?

Are you using the <string> header file or the <string.h> header file?

You should be using <string> header file.

You should try posting an entire example code.
0
 
AxterCommented:
I just ran the following code on VC++ 7.1, and it ran just fine:

#include <stdlib.h>
#include <string>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
      string str;
      char a[100];

      _snprintf(a, 100, "andy testing sjusfna;sdlfjasl;fsd");
      str = a;

      printf("%s\n", str.c_str());

      system("pause");
      return 0;
}


Please post an entire example code, like the above, so that we can see exactly what's failing, and how you're getting the failure results.
0
 
AxterCommented:
Is your project a UNICODE project or ANSI?
0
 
hxiaAuthor Commented:
// andy.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "andy.h"
#include <stdlib.h>
#include <string>
#include <stdio.h>
#define MAX_LOADSTRING 100

using namespace std;

// Global Variables:
HINSTANCE hInst;                                                // current instance
TCHAR szTitle[MAX_LOADSTRING];                              // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING];                  // the main window class name

// Forward declarations of functions included in this code module:
ATOM                        MyRegisterClass(HINSTANCE hInstance);
BOOL                        InitInstance(HINSTANCE, int);
LRESULT CALLBACK      WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK      About(HWND, UINT, WPARAM, LPARAM);

int APIENTRY _tWinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR    lpCmdLine,
                     int       nCmdShow)
{

      string str;
      char a[100];

      _snprintf(a, 100, "andy testing sjusfna;sdlfjasl;fsd");
      str = a;

       // TODO: Place code here.
      MSG msg;
      HACCEL hAccelTable;

      // Initialize global strings
      LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
      LoadString(hInstance, IDC_ANDY, szWindowClass, MAX_LOADSTRING);
      MyRegisterClass(hInstance);

      // Perform application initialization:
      if (!InitInstance (hInstance, nCmdShow))
      {
            return FALSE;
      }

      hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_ANDY);

      // Main message loop:
      while (GetMessage(&msg, NULL, 0, 0))
      {
            if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
            {
                  TranslateMessage(&msg);
                  DispatchMessage(&msg);
            }
      }

      return (int) msg.wParam;
}

The same code runs on other machines.  But on mine, it complies, runs and gets junks from the assignment.  This test code is win32.
0
 
AxterCommented:
How do you know it's failing?
The above code does not ouput the contents of str, so there's no way to tell if it fails.

Try modifiying it to the following:
      string str;
      char a[100];

      _snprintf(a, 100, "andy testing sjusfna;sdlfjasl;fsd");
      str = a;

      MessageBox(NULL,str.c_str(), "", 0);

When I run this on 7.1, I get a messagebox with the full contents.
0
 
hxiaAuthor Commented:
i ran it in debuggar, and watched the values.  the string variable is corrupted after assignment.
0
 
AxterCommented:
>>i ran it in debuggar, and watched the values.  the string variable is corrupted after assignment.

Sometimes the debugger can display misleading information.  Try displaying it via MessageBox or on the screen to verify it's contents.

MessageBox(NULL,str.c_str(), "", 0);


That will tell you for sure if it's currupt.
0
 
hxiaAuthor Commented:
ok, i tried to display.  only junks in it.  again, in the debugger, the str had a valid pointer and correct contents, but stringbuf is currupted and had those junks in there somehow.    none of this is happened on other machines when i tried.  at this point i will try to upgrade my .net to 2003, use VC++7.1 instead, hopefully, i will work right this time.  all the answers are correct i am sure, thank you all.
0
 
AxterCommented:
>> i will try to upgrade my .net to 2003, use VC++7.1 instead

7.1 is much better then 7.0, so it's well worth the effort and the cost for the upgrade.

The 7.1 version is more C++ compliant then the 3.x GNU compiler.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 7
  • 5
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now