Assign value char *ptr = strtok(chrX2,",");

Answered Assign value char *ptr = strtok(chrX2,",");

  • domingo, 19 de agosto de 2012 20:38
     
      Contém Código

    Hi,

    I am trying to assign the vector "Names" the values in a text file.  So I wrote a routine to grab the data, but I am running into problems assigning the value.

    Here is my code

    namespace RESERVES
    {
    	int ReadCOI_Names(vector<string>&Names);	
    }
    int RESERVES::ReadCOI_Names(vector<string> &Names)
    {	//READ INFORCE FILE MOSES FORMAT
    	//CHANGE TO TAKE IN location and file name
    	int ret=0;
    	FILE * infile;  
    	char temp_name[1000];
    	strcpy(temp_name,"C:\\Documents and Settings\\Desktop\\VS Reserves\\Reserves\\");
    	strcat(temp_name, "coi.csv");
    	infile = fopen(temp_name, "r");
    	if(infile == NULL)
    	{	LogFile<<"Error - unsuccessful open"<<endl;  exit(0);}
    	else
    		LogFile<<"Successfully opened"<<endl;
    	int x2;
    	int x3;
    	string x4; //char x4[10];
    	int x5;
    	double x6; double x7;
    	int count=0;
    	int i = 0;
    	char chrX[5000];
    	char chrX2[5000];
    	char x1[5000];
    	int xyz = 0;
    	int j = 0;
    	int p = 0;
    	fgets(chrX,sizeof(chrX)-1,infile); //Skip past header
    	
    	while(fgets(chrX,sizeof(chrX)-1,infile))
    	{
    		char *ptr2 = chrX;
    		xyz = strlen(chrX);
    		j=0;
    		p=p+1;//policy index
    		for(i=0;i<=xyz;i++)//Step 1: Assign chrX --> chrX2
    		{
    			if(*(ptr2 + i) == ',' && *(ptr2 + i+1) == ',') 
    			{
    				chrX2[j]= *(ptr2+i);
    				j=j+1;
    				chrX2[j]= '0'; //Assign "0" as a default if there is a blank in the file
    			}
    			else
    				chrX2[j] = *(ptr2+i);
    			j=j+1;
    		}
    		char *ptr = strtok(chrX2,",");
    		while(ptr)
    		{
    			++count;
    			switch(count)
    			{
    				case 1:
    					//x1 = strcpy(x1,ptr);				//sscanf(ptr,"%d",&x1);
    					break;
    				case 2:
    					//x4 = strcpy(x4,ptr);
    					break;
    				case 3:
    					//x4 = strcpy(x4,ptr);
    					break;
    				case 4:
    					//x4 = strcpy(x4,ptr);
    					break;
    				case 5:
    					//x4 = strcpy(x4,ptr);
    					break;
    				case 6:
    					//x4 = strcpy(x4,ptr);
    					break;
    				case 7:
    					Names = ptr;//strcpy(x1,ptr);
    					break;
    			} //End Switch
    			//Iterate to the next Variable
    			ptr = strtok(NULL,",");
    		}
    	
    	}//End While Loop
    	fclose(infile);
    	return 0;
    }


Todas as Respostas

  • domingo, 19 de agosto de 2012 20:42
     
     

    I also tried...

    Names = string(strcpy(x1,ptr));

    Error 6 error C2679: binary '=' : no operator found which takes a right-hand operand of type 'std::basic_string<_Elem,_Traits,_Ax>' (or there is no acceptable conversion) c:\

  • domingo, 19 de agosto de 2012 20:43
     
     Respondido

    Hold it...

     

    Names[1] = string(strcpy(x1,ptr));

    Seems to work.  Does this code look OK?

    THanks

    • Marcado como Resposta Jeffs_Programs domingo, 19 de agosto de 2012 21:01
    •  
  • domingo, 19 de agosto de 2012 22:45
     
     

    fgets() will read a maximum number of characters that is one less than the amount you specify, thereby insuring there is always room for the terminating '\0'.  You don't need the -1 in your calls to fgets().

    Names is a vector of string.  Don't you need to use a member function like push_back to add an element to it?  Doesn't the data you are adding to the vector (char * or C string)have to be the same type as the vector elements (C++ string)?

  • domingo, 19 de agosto de 2012 22:47
     
     
    Names[1] = string(strcpy(x1,ptr));

    Seems to work.  Does this code look OK?


    It depends on what you mean by "OK", and what your
    intentions are for that operation. Do you really
    understand what it's doing?

    Given:

    (vector<string> &Names)

    char x1[5000];

    char *ptr = strtok(chrX2,",");

    Then this:

    string(strcpy(x1,ptr))

    copies the C string pointed to by ptr to the char array x1,
    then strcpy returns the destination (char*): x1

    A temporary std::string object is created containing the
    C string pointed to by x1.

    You then assign that std::string to the 2nd element in the
    vector.

    Given that all three of these operations:

    Names[1] = string(strcpy(x1,ptr));

    Names[1] = ptr;

    strcpy(x1,ptr);
    Names[1] = x1;

    will yield the same result in Names[1], then whether
    it's "OK" depends on your intentions for x1. If it's
    not needed, then your code is needlessly convoluted.

    - Wayne
  • domingo, 19 de agosto de 2012 23:49
     
     


    I never push back to add an element.  I resize the vector and then assign elements using

    vector[n] = "Value";

  • domingo, 19 de agosto de 2012 23:56
     
     


    I never push back to add an element.  I resize the vector and then assign elements using

    vector[n] = "Value";

    Umm, fine. But I don't see how that's relevant to the way
    in which you identify the string to assign.

    - Wayne

  • segunda-feira, 20 de agosto de 2012 00:40
     
     


    I never push back to add an element.  I resize the vector and then assign elements using

    vector[n] = "Value";


    Typically that's not the way it is done. You should be using push_back() instead, just to avoid tracking the size of the vector.