473,785 Members | 2,414 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Reading Data from a CSV file

emaghero
85 New Member
Greetings all,

I have a small amount of data that I want to read from a CSV file.

The data is stored in an array of size rows*cols, the array is zero-based.

I open the file using an fstream object

Expand|Select|Wrap|Line Numbers
  1. ifstream data;
  2. data.open(file,ios_base::in);
  3.  
I create a buffer to store the data

Expand|Select|Wrap|Line Numbers
  1. int buf_size=1024*1024*10;
  2. char *BUF=new(char[buf_size]);
  3. char *buf=BUF;
  4.  
I read the data from the file into the buffer

Expand|Select|Wrap|Line Numbers
  1. data.read(buf,(1024*1024*10));
  2.  
I now want to read the data from the buffer and store it an an array of size rows*cols

Expand|Select|Wrap|Line Numbers
  1. int pos;
  2.  
  3. arr=matrix(rows,cols);//This forms a zero-based array
  4.  
  5. for(int i=0;i<rows;i++){
  6.     for(int j=0;j<cols;j++){
  7.         arr[i][j]=(atof(buf));
  8.         pos=(int)(strcspn(buf,","));
  9.         buf=&buf[pos+1];
  10.     }
  11. }
  12.  
The problem is that with this code there needs to be a comma at the end of each line in order for the buffer to move to the next position and read the data in the correct order. For example

633 , 1.47154 , 0.00021,
832 , 1.46705 , 0.00022,
1306 , 1.460342 , 0.00034,
1544 , 1.457424 , 0.00028 ,

However, my data is stored without the commas at the end of each line.

How can I change the code in the last snippet so that I can read the data from the buffer and store it in the correct order?

As it stands now when I run the code it reads the data as follows

633 , 1.47154 , 0.00021
1.46705 , 0.00022 ,1.460342
0.00034 , 1.457424 , 0.00028
0, 0 , 0

The command
Expand|Select|Wrap|Line Numbers
  1.  pos=(int)(strcspn(buf,",")); 
means that the first data point on the new line is skipped and the data is not stored correctly.

Any suggestions on how to recitfy this, apart from going into each individual file and adding commas, would be much appreciated.

Thanks.
Apr 29 '09 #1
4 8529
JosAH
11,448 Recognized Expert MVP
Also do a search for "\n"; if that position is nearer/smaller than the position for a comma, use that one instead to advance the buffer to the next entry.

kind regards,

Jos
Apr 29 '09 #2
emaghero
85 New Member
@JosAH
Thanks very much.

That did the trick.

I replaced the line

Expand|Select|Wrap|Line Numbers
  1. pos=(int)(strcspn(buf,","));
  2.  
with

Expand|Select|Wrap|Line Numbers
  1. posa=(int)(strcspn(buf,","));
  2. posb=(int)(strcspn(buf,"\n"));
  3. pos=Min(posa,posb); // Min is a template function that returns the minimum of posa and posb
  4.  
and that took care of it.
Apr 29 '09 #3
donbock
2,426 Recognized Expert Top Contributor
How general purpose does this program need to be? Both comma and newline can be escaped in a CSV file -- do you need to properly handle those cases?
Apr 29 '09 #4
emaghero
85 New Member
@donbock
I only need to be able to read in different sets of experimental data that I already have. So I won't be using it as part of something else that will have to accomodate multiple file formats.
Apr 29 '09 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

0
3593
by: Andy | last post by:
Hi, In the code below (not pretty I know but it's an early version :-P) I'm having problems reading the data object back in. If I move the reading code to immediately after the section where it is written ( commented out in code) then it reads in OK. However, when I move the code to the right place ( as shown here) it throws an IO exception. Both the Filter class and it's constituent class implement Serializable and it would seem the...
1
7056
by: fabrice | last post by:
Hello, I've got trouble reading a text file (event viewer dump) by using the getline() function... After 200 - 300 lines that are read correctly, it suddenly stops reading the rest of the file... Thank you to all of you who can help me with this one...
3
2210
by: SB | last post by:
Hello. I have an input file which is laid out in the following manner... Name Day 1 am time 1 am time 2 appointment pm time 1 pm time 2 appointment Day 2
1
6762
by: Magnus | last post by:
allrite folks, got some questions here... 1) LAY-OUT OF REPORTS How is it possible to fundamentaly change the lay-out/form of a report in access? I dont really know it that "difficult", but listen up; Reports, the way I look at them, all present data downwards, in this way; TITLE data
7
6063
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 error. Can I ask a simple question to start with: I'm trying to read the file using the...
4
12808
by: Amit Maheshwari | last post by:
I need to read text file having data either comma seperated or tab seperated or any custom seperator and convert into a DataSet in C# . I tried Microsoft Text Driver and Microsoft.Jet.OLEDB.4.0 to read text file but could not get the data in correct format. All columns are not coming in dataset and rows are messing up. Suggestions please ???
6
5274
by: arne.muller | last post by:
Hello, I've come across some problems reading strucutres from binary files. Basically I've some strutures typedef struct { int i; double x; int n; double *mz;
5
14992
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 longitude, lattitude, altitude, etc. (hundreds of terms, these are just the well known ones). In the newer...
13
3713
by: swetha | last post by:
HI Every1, I have a problem in reading a binary file. Actually i want a C program which reads in the data from a file which is in binary format and i want to update values in it. The file consists of structures of type---- struct record { int acountnum; char name; float value;
6
3531
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++.
0
9646
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9483
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10157
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10096
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
9956
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8982
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5386
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4055
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
2
3658
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.