Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Reading Columnwise in a file

Reply
Thread Tools

Reading Columnwise in a file

 
 
Sobhan
Guest
Posts: n/a
 
      02-06-2004
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
 
Reply With Quote
 
 
 
 
Chris Theis
Guest
Posts: n/a
 
      02-06-2004

"Sobhan" <> wrote in message
news: om...
> 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


 
Reply With Quote
 
 
 
 
Sobhan
Guest
Posts: n/a
 
      02-07-2004
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[i] 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";

}

}



















"Chris Theis" <> wrote in message news:<c00cuu$cee$>...
> "Sobhan" <> wrote in message
> news: om...
> > 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

 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      02-07-2004
[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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Printing strings columnwise Umesh C Programming 17 08-21-2007 09:15 PM
Reading a file and resuming reading. Karim Ali Python 2 05-25-2007 02:04 PM
UnauthorizedAccessException when reading XML files (no problem when reading other file-types) blabla120@gmx.net ASP .Net 0 09-15-2006 02:08 PM
reading the DB vs. reading a text file...performance preference? Darrel ASP .Net 3 11-11-2004 02:27 PM
How to read data columnwise Sobhan C Programming 3 02-07-2004 01:35 AM



Advertisments