Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. reading in a file and storing the words in a char[]

reading in a file and storing the words in a char[]

Scheduled Pinned Locked Moved C / C++ / MFC
helpc++data-structures
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    johnstonsk
    wrote on last edited by
    #1

    I am reading in a file and trying to put words read in a char[]. My array in within a struct and the notation is:

    struct TSimHeader
    {
    char Name[47];
    char Unit[47];
    double Min[47];
    double Max[47];
    int SignalCount;
    int SimStatus;
    int simdkid;
    int modmode;
    int write;
    int call;
    }static TSimHeader_arr[10];

    This is where I read in the file and try to put the values in the appropiate containers:

    string name, unit,min,max,value;

    	for(int i=0; i<47; i++){
    		getline(fin, name, ',');
    		
    		if(name == "STOP")
    			break;
    		getline(fin, unit, ',');
    		getline(fin, min, ',');
    		getline(fin, max, ',');
    		getline(fin, value);
    		
    	   	strcpy(TSimHeader\_arr\[0\].Name\[i\], name.c\_str());
    		strcpy(TSimHeader\_arr\[0\].Unit\[i\], unit.c\_str());
    		
    		//TSimHeader\_arr\[0\].Name\[i\]  =  name;
    		//strcpy(TSimHeader\_arr\[0\].Name, name.c\_str());
    		
    		TSimHeader\_arr\[0\].Min\[i\]   =  atof(min.c\_str());
    		TSimHeader\_arr\[0\].Max\[i\]   =  atof(max.c\_str());
    	        TSimSignal\_arr\[0\].Value\[i\] =  atof(value.c\_str());
    		
    		sig\_count ++;
    

    }

    The error I get is: :*.cpp(104) : error C2664: 'strcpy' : cannot convert parameter 1 from 'char' to 'char *' Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast Thanks for the help, Steven

    C 1 Reply Last reply
    0
    • J johnstonsk

      I am reading in a file and trying to put words read in a char[]. My array in within a struct and the notation is:

      struct TSimHeader
      {
      char Name[47];
      char Unit[47];
      double Min[47];
      double Max[47];
      int SignalCount;
      int SimStatus;
      int simdkid;
      int modmode;
      int write;
      int call;
      }static TSimHeader_arr[10];

      This is where I read in the file and try to put the values in the appropiate containers:

      string name, unit,min,max,value;

      	for(int i=0; i<47; i++){
      		getline(fin, name, ',');
      		
      		if(name == "STOP")
      			break;
      		getline(fin, unit, ',');
      		getline(fin, min, ',');
      		getline(fin, max, ',');
      		getline(fin, value);
      		
      	   	strcpy(TSimHeader\_arr\[0\].Name\[i\], name.c\_str());
      		strcpy(TSimHeader\_arr\[0\].Unit\[i\], unit.c\_str());
      		
      		//TSimHeader\_arr\[0\].Name\[i\]  =  name;
      		//strcpy(TSimHeader\_arr\[0\].Name, name.c\_str());
      		
      		TSimHeader\_arr\[0\].Min\[i\]   =  atof(min.c\_str());
      		TSimHeader\_arr\[0\].Max\[i\]   =  atof(max.c\_str());
      	        TSimSignal\_arr\[0\].Value\[i\] =  atof(value.c\_str());
      		
      		sig\_count ++;
      

      }

      The error I get is: :*.cpp(104) : error C2664: 'strcpy' : cannot convert parameter 1 from 'char' to 'char *' Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast Thanks for the help, Steven

      C Offline
      C Offline
      canniballl
      wrote on last edited by
      #2

      Here's your problem: strcpy() copies the contents from one character-array to another. in the string class, c_str points to the char-array (which is good). However, you're telling strcpy() to copy the contents of c_str to a single character in the array (Name[i] signifies the i-th character in Name). Looking at your strcuture, it "LOOKS" like Name and Unit are supposed to be character-arrays (strings) of length 47, and there are 10 elements of type TSimHeader, which equates to 10 character-arrays (string) of length 47. If this is the case, you must change the loop for 10 entries instead of 47. string name, unit,min,max,value ; for(int i = 0 ; i < 10 ; i++){ getline(fin, name, ',') ; if(name == "STOP") break ; getline(fin, unit, ',') ; getline(fin, min, ',') ; getline(fin, max, ',') ; getline(fin, value) ; // The following assumes that name and unit // are less than 47 characters long strcpy(TSimHeader_arr[i].Name, name.c_str()) ; strcpy(TSimHeader_arr[i].Unit, unit.c_str()) ; TSimHeader_arr[i].Min[i] = atof(min.c_str()) ; TSimHeader_arr[i].Max[i] = atof(max.c_str()) ; TSimSignal_arr[i].Value[i] = atof(value.c_str()) ; sig_count ++ ; } Again, this is assuming I interpretted this correctly.

      J 1 Reply Last reply
      0
      • C canniballl

        Here's your problem: strcpy() copies the contents from one character-array to another. in the string class, c_str points to the char-array (which is good). However, you're telling strcpy() to copy the contents of c_str to a single character in the array (Name[i] signifies the i-th character in Name). Looking at your strcuture, it "LOOKS" like Name and Unit are supposed to be character-arrays (strings) of length 47, and there are 10 elements of type TSimHeader, which equates to 10 character-arrays (string) of length 47. If this is the case, you must change the loop for 10 entries instead of 47. string name, unit,min,max,value ; for(int i = 0 ; i < 10 ; i++){ getline(fin, name, ',') ; if(name == "STOP") break ; getline(fin, unit, ',') ; getline(fin, min, ',') ; getline(fin, max, ',') ; getline(fin, value) ; // The following assumes that name and unit // are less than 47 characters long strcpy(TSimHeader_arr[i].Name, name.c_str()) ; strcpy(TSimHeader_arr[i].Unit, unit.c_str()) ; TSimHeader_arr[i].Min[i] = atof(min.c_str()) ; TSimHeader_arr[i].Max[i] = atof(max.c_str()) ; TSimSignal_arr[i].Value[i] = atof(value.c_str()) ; sig_count ++ ; } Again, this is assuming I interpretted this correctly.

        J Offline
        J Offline
        johnstonsk
        wrote on last edited by
        #3

        You are correct but, There are 47 names, units, min, max. At each (ith) position the data is related. If I just run through the loop 10 times then I won't get the rest of the data all put in the correct array within the struct. I have it working with a 2 dimensional array. I have limited memory so I need to really be sparing on memory. So, I really need to store the names and units in a single dimension array I hope all of that made sense. I have a feeling that it won't work. thanks, Steven

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups