Write this same code with "string" instead of "char"

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi,<br/>
<br/>
I wrote this code to read in values from a text file. The text file is ~180 columns and 200K rows. Each column represents a variable. For example column 1 is the policy number. After the code reads the policy number x1, I set x1 = PolicyNumber;
Where policynumber is a vector.
The problem is that the code uses "char" and I am finding that if I try and code with "char" I have problems because they are c-style arrays. I want to use strings instead, but the code below uses char. Is there an easy way to switch from char
to string below?
When I read in the first line of the file, the first step is to check if there are ",," values and then translate all blanks to ",0,". Then I read the line and make all the assignments. The second function takes one of the char values and converts
it to a number. But I was thinking there might be a problem if the names change size and it seems like it would become a headache in the same way the c-style arrays with numerical values became headaches.
int Read_xInforce_pointer2(void)<br/>
{ <br/>
int ret=0;<br/>
FILE * infile; <br/>
char temp_name[1000];<br/>
strcpy(temp_name, cRunLocation);<br/>
strcat(temp_name, "File.csv");<br/>
infile = fopen(temp_name, "r");<br/>
if(infile == NULL)<br/>
{ LogFile<<"Error - unsuccessful open of file"<<endl; exit(0);}<br/>
else<br/>
LogFile<<"Successfully opened file"<<endl;
int x1;<br/>
int x2;<br/>
int x3;<br/>
char x4[10];<br/>
int x5;<br/>
int x6;<br/>
int x7;<br/>
int x8;<br/>
int x9;<br/>
double x10;<br/>
double x11;<br/>
double x12;<br/>
double x13;<br/>
double x14;<br/>
double x15;<br/>
char x16[10];<br/>
int x17;<br/>
int x18;<br/>
double x19;<br/>
double x20;<br/>
int x21;<br/>
int x22;<br/>
double x23;<br/>
char x24[10];<br/>
char x25[10];<br/>
char x26[10];<br/>
char x27[10];<br/>
char x28[10];<br/>
char x29[10];<br/>
char x30[10];<br/>
char x31[10];<br/>
char x32[10];<br/>
char x33[10];<br/>
char x34[10];<br/>
char x35[10];<br/>
char x36[10];<br/>
char x37[10];<br/>
char x38[10];<br/>
double x39;<br/>
double x40;<br/>
double x41;<br/>
double x42;<br/>
double x43;<br/>
double x44;<br/>
double x45;<br/>
double x46;<br/>
double x47;<br/>
double x48;<br/>
double x49;<br/>
double x50;<br/>
double x51;<br/>
double x52;<br/>
double x53;<br/>
int x54;<br/>
double x55;<br/>
char x56[10];<br/>
char x57[10];<br/>
char x58[10];<br/>
char x59[10];<br/>
char x60[10];<br/>
char x61[10];<br/>
char x62[10];<br/>
char x63[10];<br/>
char x64[10];<br/>
char x65[10];<br/>
char x66[10];<br/>
char x67[10];<br/>
char x68[10];<br/>
char x69[10];<br/>
char x70[10];<br/>
double x71;<br/>
double x72;<br/>
double x73;<br/>
double x74;<br/>
double x75;<br/>
double x76;<br/>
double x77;<br/>
double x78;<br/>
double x79;<br/>
double x80;<br/>
double x81;<br/>
double x82;<br/>
double x83;<br/>
double x84;<br/>
double x85;<br/>
char x86[10];<br/>
int x87;<br/>
char x88[10];<br/>
char x89[10];<br/>
char x90[10];<br/>
char x91[10];<br/>
char x92[10];<br/>
char x93[10];<br/>
char x94[10];<br/>
char x95[10];<br/>
char x96[10];<br/>
char x97[10];<br/>
char x98[10];<br/>
char x99[10];<br/>
char x100[10];<br/>
char x101[10];<br/>
char x102[10];<br/>
double x103;<br/>
double x104;<br/>
double x105;<br/>
double x106;<br/>
double x107;<br/>
double x108;<br/>
double x109;<br/>
double x110;<br/>
double x111;<br/>
double x112;<br/>
double x113;<br/>
double x114;<br/>
double x115;<br/>
double x116;<br/>
double x117;<br/>
char x118[10];<br/>
double x119;<br/>
char x120[10];<br/>
double x121;<br/>
double x122;<br/>
char x123[10];<br/>
double x124;<br/>
char x125[10];<br/>
double x126;<br/>
char x127[10];<br/>
double x128;<br/>
char x129[10];<br/>
char x130[10];<br/>
char x131[10];<br/>
int x132;<br/>
char x133[10];<br/>
double x134;<br/>
int x135;<br/>
double x136;<br/>
int x137;<br/>
double x138;<br/>
double x139;<br/>
double x140;<br/>
int x141;<br/>
double x142;<br/>
int x143;<br/>
double x144;<br/>
double x145;<br/>
double x146;<br/>
double x147;<br/>
int x148;<br/>
int x149;<br/>
double x150;<br/>
int x151;<br/>
double x152;<br/>
double x153;<br/>
double x154;<br/>
double x155;<br/>
double x156;<br/>
double x157;<br/>
char x158[10];<br/>
int x159;<br/>
int x160;<br/>
int x161;<br/>
int x162;<br/>
int x163;<br/>
int x164;<br/>
char x165[10];<br/>
double x166;<br/>
double x167;<br/>
char x168[10];<br/>
char x169[10];<br/>
char x170[10];<br/>
int x171;<br/>
char x172[10];<br/>
char x173[10];<br/>
double x174;
int count=0;<br/>
int i = 0;<br/>
char chrX[5000];<br/>
char chrX2[5000];<br/>
int xyz = 0;<br/>
int j = 0;<br/>
int p = 0;
fgets(chrX,sizeof(chrX)-1,infile); //Skip past header<br/>
<br/>
while(fgets(chrX,sizeof(chrX)-1,infile))<br/>
{<br/>
char *ptr2 = chrX;<br/>
xyz = strlen(chrX);<br/>
j=0;<br/>
p=p+1;//policy index<br/>
for(i=0;i<=xyz;i++)//Step 1: Assign chrX --> chrX2<br/>
{<br/>
if(*(ptr2 + i) == , && *(ptr2 + i+1) == ,) <br/>
{<br/>
chrX2[j]= *(ptr2+i);<br/>
j=j+1;<br/>
chrX2[j]= 0; //Assign "0" as a default<br/>
}<br/>
else<br/>
chrX2[j] = *(ptr2+i);<br/>
j=j+1;<br/>
}<br/>
char *ptr = strtok(chrX2,",");<br/>
while(ptr)<br/>
{<br/>
++count;<br/>
switch(count)<br/>
{<br/>
case 1:<br/>
sscanf(ptr,"%d",&x1);<br/>
break;<br/>
case 2:<br/>
sscanf(ptr,"%d",&x2);<br/>
break;<br/>
case 3:<br/>
sscanf(ptr,"%d",&x3);<br/>
break;<br/>
case 4:<br/>
strcpy(x4,ptr);<br/>
break;<br/>
case 5:<br/>
sscanf(ptr,"%d",&x5);<br/>
break;<br/>
case 6:<br/>
sscanf(ptr,"%d",&x6);<br/>
break;<br/>
case 7:<br/>
sscanf(ptr,"%d",&x7);<br/>
break;<br/>
case 8:<br/>
sscanf(ptr,"%d",&x8);<br/>
break;<br/>
case 9:<br/>
sscanf(ptr,"%d",&x9);<br/>
break;<br/>
case 10:<br/>
sscanf(ptr,"%lf",&x10);<br/>
break;<br/>
case 11:<br/>
sscanf(ptr,"%lf",&x11);<br/>
break;<br/>
case 12:<br/>
sscanf(ptr,"%lf",&x12);<br/>
break;<br/>
case 13:<br/>
sscanf(ptr,"%lf",&x13);<br/>
break;<br/>
case 14:<br/>
sscanf(ptr,"%lf",&x14);<br/>
break;<br/>
case 15:<br/>
sscanf(ptr,"%lf",&x15);<br/>
break;<br/>
case 16:<br/>
strcpy(x16,ptr);<br/>
break;<br/>
case 17:<br/>
sscanf(ptr,"%d",&x17);<br/>
break;<br/>
case 18:<br/>
sscanf(ptr,"%d",&x18);<br/>
break;<br/>
case 19:<br/>
sscanf(ptr,"%lf",&x19);<br/>
break;<br/>
case 20:<br/>
sscanf(ptr,"%lf",&x20);<br/>
break;<br/>
case 21:<br/>
sscanf(ptr,"%d",&x21);<br/>
break;<br/>
case 22:<br/>
sscanf(ptr,"%d",&x22);<br/>
break;<br/>
case 23:<br/>
sscanf(ptr,"%lf",&x23);<br/>
break;<br/>
case 24:<br/>
strcpy(x24,ptr);<br/>
break;<br/>
case 25:<br/>
strcpy(x25,ptr);<br/>
break;<br/>
case 26:<br/>
strcpy(x26,ptr);<br/>
break;<br/>
case 27:<br/>
strcpy(x27,ptr);<br/>
break;<br/>
case 28:<br/>
strcpy(x28,ptr);<br/>
break;<br/>
case 29:<br/>
strcpy(x29,ptr);<br/>
break;<br/>
case 30:<br/>
strcpy(x30,ptr);<br/>
break;<br/>
case 31:<br/>
strcpy(x31,ptr);<br/>
break;<br/>
case 32:<br/>
strcpy(x32,ptr);<br/>
break;<br/>
case 33:<br/>
strcpy(x33,ptr);<br/>
break;<br/>
case 34:<br/>
strcpy(x34,ptr);<br/>
break;<br/>
case 35:<br/>
strcpy(x35,ptr);<br/>
break;<br/>
case 36:<br/>
strcpy(x36,ptr);<br/>
break;<br/>
case 37:<br/>
strcpy(x37,ptr);<br/>
break;<br/>
case 38:<br/>
strcpy(x38,ptr);<br/>
break;<br/>
case 39:<br/>
sscanf(ptr,"%lf",&x39);<br/>
break;<br/>
case 40:<br/>
sscanf(ptr,"%lf",&x40);<br/>
break;<br/>
case 41:<br/>
sscanf(ptr,"%lf",&x41);<br/>
break;<br/>
case 42:<br/>
sscanf(ptr,"%lf",&x42);<br/>
break;<br/>
case 43:<br/>
sscanf(ptr,"%lf",&x43);<br/>
break;<br/>
case 44:<br/>
sscanf(ptr,"%lf",&x44);<br/>
break;<br/>
case 45:<br/>
sscanf(ptr,"%lf",&x45);<br/>
break;<br/>
case 46:<br/>
sscanf(ptr,"%lf",&x46);<br/>
break;<br/>
case 47:<br/>
sscanf(ptr,"%lf",&x47);<br/>
break;<br/>
case 48:<br/>
sscanf(ptr,"%lf",&x48);<br/>
break;<br/>
case 49:<br/>
sscanf(ptr,"%lf",&x49);<br/>
break;<br/>
case 50:<br/>
sscanf(ptr,"%lf",&x50);<br/>
break;<br/>
case 51:<br/>
sscanf(ptr,"%lf",&x51);<br/>
break;<br/>
case 52:<br/>
sscanf(ptr,"%lf",&x52);<br/>
break;<br/>
case 53:<br/>
sscanf(ptr,"%lf",&x53);<br/>
break;<br/>
case 54:<br/>
sscanf(ptr,"%d",&x54);<br/>
break;<br/>
case 55:<br/>
sscanf(ptr,"%lf",&x55);<br/>
break;<br/>
case 56:<br/>
strcpy(x56,ptr);<br/>
break;<br/>
case 57:<br/>
strcpy(x57,ptr);<br/>
break;<br/>
case 58:<br/>
strcpy(x58,ptr);<br/>
break;<br/>
case 59:<br/>
strcpy(x59,ptr);<br/>
break;<br/>
case 60:<br/>
strcpy(x60,ptr);<br/>
break;<br/>
case 61:<br/>
strcpy(x61,ptr);<br/>
break;<br/>
case 62:<br/>
strcpy(x62,ptr);<br/>
break;<br/>
case 63:<br/>
strcpy(x63,ptr);<br/>
break;<br/>
case 64:<br/>
strcpy(x64,ptr);<br/>
break;<br/>
case 65:<br/>
strcpy(x65,ptr);<br/>
break;<br/>
case 66:<br/>
strcpy(x66,ptr);<br/>
break;<br/>
case 67:<br/>
strcpy(x67,ptr);<br/>
break;<br/>
case 68:<br/>
strcpy(x68,ptr);<br/>
break;<br/>
case 69:<br/>
strcpy(x69,ptr);<br/>
break;<br/>
case 70:<br/>
strcpy(x70,ptr);<br/>
break;<br/>
case 71:<br/>
sscanf(ptr,"%lf",&x71);<br/>
break;<br/>
case 72:<br/>
sscanf(ptr,"%lf",&x72);<br/>
break;<br/>
case 73:<br/>
sscanf(ptr,"%lf",&x73);<br/>
break;<br/>
case 74:<br/>
sscanf(ptr,"%lf",&x74);<br/>
break;<br/>
case 75:<br/>
sscanf(ptr,"%lf",&x75);<br/>
break;<br/>
case 76:<br/>
sscanf(ptr,"%lf",&x76);<br/>
break;<br/>
case 77:<br/>
sscanf(ptr,"%lf",&x77);<br/>
break;<br/>
case 78:<br/>
sscanf(ptr,"%lf",&x78);<br/>
break;<br/>
case 79:<br/>
sscanf(ptr,"%lf",&x79);<br/>
break;<br/>
case 80:<br/>
sscanf(ptr,"%lf",&x80);<br/>
break;<br/>
case 81:<br/>
sscanf(ptr,"%lf",&x81);<br/>
break;<br/>
case 82:<br/>
sscanf(ptr,"%lf",&x82);<br/>
break;<br/>
case 83:<br/>
sscanf(ptr,"%lf",&x83);<br/>
break;<br/>
case 84:<br/>
sscanf(ptr,"%lf",&x84);<br/>
break;<br/>
case 85:<br/>
sscanf(ptr,"%lf",&x85);<br/>
break;<br/>
case 86:<br/>
strcpy(x86,ptr);<br/>
break;<br/>
case 87:<br/>
sscanf(ptr,"%d",&x87);<br/>
break;<br/>
case 88:<br/>
strcpy(x88,ptr);<br/>
break;<br/>
case 89:<br/>
strcpy(x89,ptr);<br/>
break;<br/>
case 90:<br/>
strcpy(x90,ptr);<br/>
break;<br/>
case 91:<br/>
strcpy(x91,ptr);<br/>
break;<br/>
case 92:<br/>
strcpy(x92,ptr);<br/>
break;<br/>
case 93:<br/>
strcpy(x93,ptr);<br/>
break;<br/>
case 94:<br/>
strcpy(x94,ptr);<br/>
break;<br/>
case 95:<br/>
strcpy(x95,ptr);<br/>
break;<br/>
case 96:<br/>
strcpy(x96,ptr);<br/>
break;<br/>
case 97:<br/>
strcpy(x97,ptr);<br/>
break;<br/>
case 98:<br/>
strcpy(x98,ptr);<br/>
break;<br/>
case 99:<br/>
strcpy(x99,ptr);<br/>
break;<br/>
case 100:<br/>
strcpy(x100,ptr);<br/>
break;<br/>
case 101:<br/>
strcpy(x101,ptr);<br/>
break;<br/>
case 102:<br/>
strcpy(x102,ptr);<br/>
break;<br/>
case 103:<br/>
sscanf(ptr,"%lf",&x103);<br/>
break;<br/>
case 104:<br/>
sscanf(ptr,"%lf",&x104);<br/>
break;<br/>
case 105:<br/>
sscanf(ptr,"%lf",&x105);<br/>
break;<br/>
case 106:<br/>
sscanf(ptr,"%lf",&x106);<br/>
break;<br/>
case 107:<br/>
sscanf(ptr,"%lf",&x107);<br/>
break;<br/>
case 108:<br/>
sscanf(ptr,"%lf",&x108);<br/>
break;<br/>
case 109:<br/>
sscanf(ptr,"%lf",&x109);<br/>
break;<br/>
case 110:<br/>
sscanf(ptr,"%lf",&x110);<br/>
break;<br/>
case 111:<br/>
sscanf(ptr,"%lf",&x111);<br/>
break;<br/>
case 112:<br/>
sscanf(ptr,"%lf",&x112);<br/>
break;<br/>
case 113:<br/>
sscanf(ptr,"%lf",&x113);<br/>
break;<br/>
case 114:<br/>
sscanf(ptr,"%lf",&x114);<br/>
break;<br/>
case 115:<br/>
sscanf(ptr,"%lf",&x115);<br/>
break;<br/>
case 116:<br/>
sscanf(ptr,"%lf",&x116);<br/>
break;<br/>
case 117:<br/>
sscanf(ptr,"%lf",&x117);<br/>
break;<br/>
case 118:<br/>
strcpy(x118,ptr);<br/>
break;<br/>
case 119:<br/>
sscanf(ptr,"%lf",&x119);<br/>
break;<br/>
case 120:<br/>
strcpy(x120,ptr);<br/>
break;<br/>
case 121:<br/>
sscanf(ptr,"%lf",&x121);<br/>
break;<br/>
case 122:<br/>
sscanf(ptr,"%lf",&x122);<br/>
break;<br/>
case 123:<br/>
strcpy(x123,ptr);<br/>
break;<br/>
case 124:<br/>
sscanf(ptr,"%lf",&x124);<br/>
break;<br/>
case 125:<br/>
strcpy(x125,ptr);<br/>
break;<br/>
case 126:<br/>
sscanf(ptr,"%lf",&x126);<br/>
break;<br/>
case 127:<br/>
strcpy(x127,ptr);<br/>
break;<br/>
case 128:<br/>
sscanf(ptr,"%lf",&x128);<br/>
break;<br/>
case 129:<br/>
strcpy(x129,ptr);<br/>
break;<br/>
case 130:<br/>
strcpy(x130,ptr);<br/>
break;<br/>
case 131:<br/>
strcpy(x131,ptr);<br/>
break;<br/>
case 132:<br/>
sscanf(ptr,"%d",&x132);<br/>
break;<br/>
case 133:<br/>
strcpy(x133,ptr);<br/>
break;<br/>
case 134:<br/>
sscanf(ptr,"%lf",&x134);<br/>
break;<br/>
case 135:<br/>
sscanf(ptr,"%d",&x135);<br/>
break;<br/>
case 136:<br/>
sscanf(ptr,"%lf",&x136);<br/>
break;<br/>
case 137:<br/>
sscanf(ptr,"%d",&x137);<br/>
break;<br/>
case 138:<br/>
sscanf(ptr,"%lf",&x138);<br/>
break;<br/>
case 139:<br/>
sscanf(ptr,"%lf",&x139);<br/>
break;<br/>
case 140:<br/>
sscanf(ptr,"%lf",&x140);<br/>
break;<br/>
case 141:<br/>
sscanf(ptr,"%d",&x141);<br/>
break;<br/>
case 142:<br/>
sscanf(ptr,"%lf",&x142);<br/>
break;<br/>
case 143:<br/>
sscanf(ptr,"%d",&x143);<br/>
break;<br/>
case 144:<br/>
sscanf(ptr,"%lf",&x144);<br/>
break;<br/>
case 145:<br/>
sscanf(ptr,"%lf",&x145);<br/>
break;<br/>
case 146:<br/>
sscanf(ptr,"%lf",&x146);<br/>
break;<br/>
case 147:<br/>
sscanf(ptr,"%lf",&x147);<br/>
break;<br/>
case 148:<br/>
sscanf(ptr,"%d",&x148);<br/>
break;<br/>
case 149:<br/>
sscanf(ptr,"%d",&x149);<br/>
break;<br/>
case 150:<br/>
sscanf(ptr,"%lf",&x150);<br/>
break;<br/>
case 151:<br/>
sscanf(ptr,"%d",&x151);<br/>
break;<br/>
case 152:<br/>
sscanf(ptr,"%lf",&x152);<br/>
break;<br/>
case 153:<br/>
sscanf(ptr,"%lf",&x153);<br/>
break;<br/>
case 154:<br/>
sscanf(ptr,"%lf",&x154);<br/>
break;<br/>
case 155:<br/>
sscanf(ptr,"%lf",&x155);<br/>
break;<br/>
case 156:<br/>
sscanf(ptr,"%lf",&x156);<br/>
break;<br/>
case 157:<br/>
sscanf(ptr,"%lf",&x157);<br/>
break;<br/>
case 158:<br/>
strcpy(x158,ptr);<br/>
break;<br/>
case 159:<br/>
sscanf(ptr,"%d",&x159);<br/>
break;<br/>
case 160:<br/>
sscanf(ptr,"%d",&x160);<br/>
break;<br/>
case 161:<br/>
sscanf(ptr,"%d",&x161);<br/>
break;<br/>
case 162:<br/>
sscanf(ptr,"%d",&x162);<br/>
break;<br/>
case 163:<br/>
sscanf(ptr,"%d",&x163);<br/>
break;<br/>
case 164:<br/>
sscanf(ptr,"%d",&x164);<br/>
break;<br/>
case 165:<br/>
strcpy(x165,ptr);<br/>
break;<br/>
case 166:<br/>
sscanf(ptr,"%lf",&x166);<br/>
break;<br/>
case 167:<br/>
sscanf(ptr,"%lf",&x167);<br/>
break;<br/>
case 168:<br/>
strcpy(x168,ptr);<br/>
break;<br/>
case 169:<br/>
strcpy(x169,ptr);<br/>
break;<br/>
case 170:<br/>
strcpy(x170,ptr);<br/>
break;<br/>
case 171:<br/>
sscanf(ptr,"%d",&x171);<br/>
break;<br/>
case 172:<br/>
strcpy(x172,ptr);<br/>
break;<br/>
case 173:<br/>
strcpy(x173,ptr);<br/>
break;<br/>
case 174:<br/>
sscanf(ptr,"%lf",&x174);<br/>
break;<br/>
} //End Switch<br/>
//Iterate to the next Variable<br/>
ptr = strtok(NULL,",");<br/>
}<br/>
//Make All Assignments Here after line has been read<br/>
PolicyNumber[p] = 1;<br/>
InitialAV[p] = x10; <br/>
<br/>
}//End File<br/>
fclose(infile);<br/>
return 0;<br/>
}
int FundCodeToInteger(char c[])<br/>
{<br/>
int a,i;<br/>
for(i=1;i<=450;i++){<br/>
a = strcmp(FundCodeTable[0],c);<br/>
if(a == 0) return atoi(FundCodeTable[1]);<br/>
}

<br/>

View the full article
 
Back
Top