Reading the last column of data in a file, and removing the "square" character

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi,<br/>
<br/>
I have a routine that reads a comma seperated text file. The last column of data is a file path. The function reads the file paths into a vector of string variables.<br/>
<br/>
The problem is that if the filename in the file being read is
"C:file.csv"
If I assign the variable the name XX[1] = "C:file.csv" it stores the path as "C:file.csv{}", where "{} is the square symbol that means a carriage return. How can I get rid of this symbol.
<br/>
<br/>
In the next step in the program I use "XX[1]" as the file path but this isnt working with a "{}" carriage return symbol. Here is the function that reads in the names.
<pre class="prettyprint int RESERVES::ReadCOI_Names(vector<string> &Names,string file_loc,string file_nm)
{
FILE * infile;
char temp_name[1000];
string x = file_loc + file_nm;
for(int yy = 0;yy<=x.size();yy++){temp_name[yy]=x[yy];}//Convert String to Char for "fopen"
infile = fopen(temp_name, "r");
if(infile == NULL)
{ LogFile<<"Error - unsuccessful open"<<endl; exit(0);}
else
LogFile<<"Successfully opened"<<endl;
int count = 0;
int i = 0;
char chrX[5000];
char chrX2[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;
j=0;
p=p+1;//policy index
for(i=0;i<=strlen(chrX);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,",");
count = 0; //Reset Column Count
while(ptr)
{
++count;
switch(count)
{
case 7: //COI Names are in the 7th Column
{ //Names[p] = string(strcpy(x1,ptr));
Names[p] = ptr;
break;
}
} //End Switch
ptr = strtok(NULL,",");
}
}//End While Loop
fclose(infile);
return 0;
}[/code]
<br/>

View the full article
 
Back
Top