Reading Columnwise in a file

S

Sobhan

Hi all,
I am writing a program in C++ in which I need to read a data
file and export to the excel.The Data in the file in CSV format.
Values in the .txt file are as follows:
"a","b","c"
10,20,30
40,50,60
70,80,90

For that I need to read the values column wise.
Like I have to store the values in a buffer like the following:
"a",
10,
40,
70
etc.

I did the following:

1.Open the file in Read Mode
2.Read line by line
3.Search for comma
4.When Got comma
Printf("\n");
5.Problem is how I will return the buffer(Which is set of integers) to
calling program reading the data values column wise???

Regards
Bubunia
 
C

Chris Theis

Sobhan said:
Hi all,
I am writing a program in C++ in which I need to read a data
file and export to the excel.The Data in the file in CSV format.
Values in the .txt file are as follows:
"a","b","c"
10,20,30
40,50,60
70,80,90

For that I need to read the values column wise.
Like I have to store the values in a buffer like the following:
"a",
10,
40,
70
etc.

I did the following:

1.Open the file in Read Mode
2.Read line by line
3.Search for comma
4.When Got comma
Printf("\n");
5.Problem is how I will return the buffer(Which is set of integers) to
calling program reading the data values column wise???

Regards
Bubunia

You can return a vector of a vector (=matrix) storing your values. However,
show some code which allows people to give more specific answers.

Chris
 
S

Sobhan

In My C++/VC++ program:

/* Snippets of Code */
for (int z=0;z<50;z++)
{
atemp1 = CString("A");
btemp1 = CString("B");
//Column A - Of Excel
app.SetDisplayAlerts(FALSE);
oRange = oSheet.GetRange(COleVariant (atemp1),COleVariant
(atemp2));
CString peak_memoryUsage = read_file("a",in);
int buf[z-1] = (int)read_values("a",in);


oRange.SetValue2(COleVariant(buf[z-1]));
}

int read_values(CString str1,ifstream in)
{

int pos;
char lineone[256];
char buf[200];
char buff[1024];
CString filename=ptr;
int status;
status = _open(filename,_O_RDONLY);
if(status == -1)
{
printf("Couldnot able to Open file ");

}

else
{

printf("Opening of file Successful");

}

pdest=strstr(lineone,str1);
pos = pdest - lineone + str1.GetLength();
if (!in.eof())
{
in.getline(lineone,sizeof(lineone),'\n');
if(str1 == ",") /* If got a comma */ In this exp:
"a",(Comma)
{
printf("\n");
for(i=0;i<=50;i++) /* No of Rows */
{

/* Here is the Problem */
Question is :How I will return a Buffer
of integer to the calling program column wise? */

pos=1;
/* Here I need to return buff to the calling program
(10
40 pos = pdest - lineone +
str1.GetLength();
70 ) How???

*/

return buf;
}

/* pos =pdest -lineone+str1.GetLength(); */
}
}

}


CString read_file(CString str1, ifstream in)
{

char *pdest;
int pos;
char lineone[256];

if (!in.eof())
{
in.getline(lineone,sizeof(lineone),'\n');
pdest = strstr(lineone,str1);
while (pdest == 0)
{
in.getline(lineone,sizeof(lineone),'\n');
pdest = strstr(lineone,str1);
}


CString s = lineone;
CString s1 = s.Mid(pos);
return s1;
}
else
{
return "EOF";

}

}
 
J

John Harrison

[horrible mess of code snipped]

I think the answer is exactly as Chris described. You need to read the
column of data into some data structure, and return the entire column in one
go to the calling program. Then the calling program can loop though all the
data in whatever way it likes.

The best data structure would be a std::vector<int> (I think you're dealing
with integer data). There's an MFC alternative to this but I forget what its
called, ask in an MFC newsgroup. You could even use an array or a pointer to
dynamically allocated memory.

Nothing in the code you posted (apart from the OLE stuff) cannot be done
with standard C++, so there is no good reason to use CString (prefer
std::string), _open (prefer ifstream). Even OLEVariant can possibly be
replaced with boost::any (see www.boost.org). Don't use non standard code
unnecessarily, apart from any other consideration (such as portability) it
means that your code is off topic in a group that discusses standard C++
like this one.

John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top