473,405 Members | 2,167 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Reading Columnwise in a file

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
Jul 22 '05 #1
3 3932

"Sobhan" <bu*********@yahoo.co.in> wrote in message
news:81**************************@posting.google.c 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
Jul 22 '05 #2
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" <Ch*************@nospam.cern.ch> wrote in message news:<c0**********@sunnews.cern.ch>...
"Sobhan" <bu*********@yahoo.co.in> wrote in message
news:81**************************@posting.google.c 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

Jul 22 '05 #3
[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
Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Xah Lee | last post by:
# -*- coding: utf-8 -*- # Python # to open a file and write to file # do f=open('xfile.txt','w') # this creates a file "object" and name it f. # the second argument of open can be
19
by: Lionel B | last post by:
Greetings, I need to read (unformatted text) from stdin up to EOF into a char buffer; of course I cannot allocate my buffer until I know how much text is available, and I do not know how much...
4
by: Oliver Knoll | last post by:
According to my ANSI book, tmpfile() creates a file with wb+ mode (that is just writing, right?). How would one reopen it for reading? I got the following (which works): FILE *tmpFile =...
6
by: Rajorshi Biswas | last post by:
Hi folks, Suppose I have a large (1 GB) text file which I want to read in reverse. The number of characters I want to read at a time is insignificant. I'm confused as to how best to do it. Upon...
1
by: Need Helps | last post by:
Hello. I'm writing an application that writes to a file a month, day, year, number of comments, then some strings for the comments. So the format for each record would look like:...
7
by: John Dann | last post by:
I'm trying to read some binary data from a file created by another program. I know the binary file format but can't change or control the format. The binary data is organised such that it should...
5
blazedaces
by: blazedaces | last post by:
Ok, so you know my problem, java is running out of memory reading with SAX, the event-based xml parser intended more-so than DOM for extremely large files. I'll try to explain what I've been doing...
6
by: efrenba | last post by:
Hi, I came from delphi world and now I'm doing my first steps in C++. I'm using C++builder because its ide is like delphi although I'm trying to avoid the vcl. I need to insert new features...
2
by: Derik | last post by:
I've got a XML file I read using a file_get_contents and turn into a simpleXML node every time index.php loads. I suspect this is causing a noticeable lag in my page-execution time. (Or the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.