473,569 Members | 2,916 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3942

"Sobhan" <bu*********@ya hoo.co.in> wrote in message
news:81******** *************** ***@posting.goo gle.com...
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.SetDisplayA lerts(FALSE);
oRange = oSheet.GetRange (COleVariant (atemp1),COleVa riant
(atemp2));
CString peak_memoryUsag e = read_file("a",i n);
int buf[z-1] = (int)read_value s("a",in);
oRange.SetValue 2(COleVariant(b uf[z-1]));
}

int read_values(CSt ring 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("Couldno t able to Open file ");

}

else
{

printf("Opening of file Successful");

}

pdest=strstr(li neone,str1);
pos = pdest - lineone + str1.GetLength( );
if (!in.eof())
{
in.getline(line one,sizeof(line one),'\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.Ge tLength(); */
}
}

}
CString read_file(CStri ng str1, ifstream in)
{

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

if (!in.eof())
{
in.getline(line one,sizeof(line one),'\n');
pdest = strstr(lineone, str1);
while (pdest == 0)
{
in.getline(line one,sizeof(line one),'\n');
pdest = strstr(lineone, str1);
}
CString s = lineone;
CString s1 = s.Mid(pos);
return s1;
}
else
{
return "EOF";

}

}







"Chris Theis" <Ch************ *@nospam.cern.c h> wrote in message news:<c0******* ***@sunnews.cer n.ch>...
"Sobhan" <bu*********@ya hoo.co.in> wrote in message
news:81******** *************** ***@posting.goo gle.com...
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
3049
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
10283
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 text is available until I have read it... which seems to imply that multiple reads of the input stream will be inevitable. Now I can correctly...
4
9816
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 = tmpfile(); /* write into tmpFile */ ...
6
6329
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 browsing through this group and other sources on the web, it seems that there are many ways to do it. Some suggest that simply fseek'ing to 8K bytes...
1
2008
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: mdyn"comment~""comment~"\n Now, I wrote some code to read these records, and it works perfectly for every date I've tried it on, except when the day is 26. I tried...
7
6046
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 populate a series of structures of specified variable composition. I have the structures created OK, but actually reading the files is giving me an...
5
14975
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 and why I have to do it. Hopefully someone has a suggestion... Alright, so I'm using a gps-simulation program that outputs gps data, like...
6
3515
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 to an old program that I wrote in delphi and it's a good opportunity to start with c++.
2
2831
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 wireless where I'm working could just be ungodly slow-- which it is.) Is reading a file much more resource/processor intensive than, say, including a...
0
7605
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7917
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7665
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6277
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5217
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3651
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3631
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2105
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
933
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.