473,397 Members | 2,084 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,397 software developers and data experts.

Read data from a file into array

5
Dear All,

I have 2 cvs file like this:

<spec.csv>
Type,sybType,ID
CASH,ON,1
FRA,1x4,2
...

<data.csv>
ID,Rate
1,3
2,5.23
...

I want to read the files and put them into a 2D array. I am new to c++, pls help to give me some simple code to do this.

The follow are some of my current code to read one csv file, but still not success.

Expand|Select|Wrap|Line Numbers
  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <fstream>
  4. #include <string>
  5.  
  6. #define DELIM ","
  7.  
  8. using namespace std;
  9.  
  10. int main(int argc, char *argv[])
  11. {
  12.     ifstream specf, dataf;
  13.     int      specId[50],dataId[50];
  14.     char     *buff,*sep;
  15.     char     *InstrumentType[50],*subType[50];
  16.     int      i=0,j=0;
  17.  
  18.  
  19.     specf.open(argv[1]); //Open Spec File
  20.     if (specf.is_open()) 
  21.     {
  22.         while(specf.getline(buff,256))
  23.         {
  24.             sep = strtok(buff, DELIM);
  25.             while (sep != NULL)
  26.             {
  27.                   if(i==0) 
  28.                   {
  29.                      InstrumentType[j]=sep;
  30.                      cout<<j<<"IT="<<InstrumentType[j]<<endl;
  31.                      i++;
  32.                   }
  33.                   else if(i==1)
  34.                   {
  35.                      subType[j]=sep;
  36.                      cout<<j<<"ST="<<subType[j]<<endl;
  37.                      i++;
  38.  
  39.                   }
  40.                   else if(i==2)
  41.                   {
  42.                      specId[j]=atoi(sep);
  43.                      cout<<j<<"ID="<<specId[j]<<endl;                     
  44.                      i=0;
  45.                   }
  46.                   sep = strtok(NULL, DELIM);          
  47.             }
  48.             j++;            
  49.         }
  50.  
  51.     }
  52.     else
  53.     {
  54.         cout <<"Error opening file "<<argv[1]<<"\n";
  55.         return -1;
  56.     }
  57.  
  58.     for(j=0;j<10;j++)
  59.     {
  60.         cout<<j<<":IT="<<InstrumentType[j]<<":ST="<<subType[j]<<":ID="<<specId[j]<<endl;
  61.     }
  62.  
  63. }
  64.  
Thanks.
Jul 19 '07 #1
12 3643
ravenspoint
111 100+
A problem that I notice is that you are defining pointers to characters, but you are NOT initializing where those pointers point.

For example

char *buf

getline(buff,256)

will write the data from file to a random location - who knows where?

Better is:

char buf[258]

getline(buff,256)

which will write your data to somewhere that the compiler has laid aside for the purpose.

Some of you oither character pointers have the same problem.

James
Jul 19 '07 #2
macus
5
What I need to correct if I want to use a pointer to access the array !?It is because I don't want to limit the size of the array.Thanks.

A problem that I notice is that you are defining pointers to characters, but you are NOT initializing where those pointers point.

For example

char *buf

getline(buff,256)

will write the data from file to a random location - who knows where?

Better is:

char buf[258]

getline(buff,256)

which will write your data to somewhere that the compiler has laid aside for the purpose.

Some of you oither character pointers have the same problem.

James
Jul 19 '07 #3
ravenspoint
111 100+
Arrays, by definition, have a limited size.

In this case, it is no big deal. Notice that 256 in your call to getline? You will never get more than 256 characters returned from that routine.

James
Jul 19 '07 #4
macus
5
It is a big problem, because every lines won't exceed 256 characters. But, the program will read every line data and put into the array. Since the date many be so long, I don't want to array to limit the size of the data storage array. How do I use it with pointers ?Thanks.
Jul 19 '07 #5
I suggest the use of vectors over arrays. use push_back() method to fill them.

Ras.
Jul 19 '07 #6
macus
5
Could you mind to give me some example code!? I don't know how to use the vector.Thanks.

I suggest the use of vectors over arrays. use push_back() method to fill them.

Ras.
Jul 19 '07 #7
Could you mind to give me some example code!? I don't know how to use the vector.Thanks.
Expand|Select|Wrap|Line Numbers
  1. std::ifstream file;  //declare an input stream
  2. vector<string> vData; // declare your vector as a container for strings
  3. string temp; //needed to push the data in the vector
  4.  
  5. try{
  6.         file.open (path); // replace "path" by your path, or declare and define the variable path
  7. }
  8. catch(...){
  9.         //Do something
  10. }
  11.  
  12. while(!file.eof()){ // Read the file to a vector until its end.
  13.         file>> temp;  //put in temporary string
  14.         vData.push_back(temp);  //fill the vector
  15. }
  16.  
  17. //Close your file after
  18.  
  19.  
This code has not been tested for errors but the idea is here. You should very easily find all the methods of vectors on google. And don't forget to include the library vector.h


Ras.
Jul 20 '07 #8
ravenspoint
111 100+
And don't forget to include the library vector.h


Ras.
Umm. Excuse me, but shouldn't that be

Expand|Select|Wrap|Line Numbers
  1. #include <vector>
He will also need to write

Expand|Select|Wrap|Line Numbers
  1. #include <string>
  2. using namespace std;
  3.  
There is quite a learning curve on STL vector use. It seems a bit cruel to tell someone to use them, and then just point them to google.

STL vector<string> is definitely the correct way to go, but the OP needs a gentle tutorial.
Jul 20 '07 #9
There is quite a learning curve on STL vector use. It seems a bit cruel to tell someone to use them, and then just point them to google.
Your comment makes sense, and I do take some blame.
But it seemed to me that a page like http://www.cplusplus.com/reference/stl/vector/ , on top results in google for simple queries, would be a more concise and helpful information than going on from scratch in this one thread. Also, a simple search on vectors in this forum would pop up all the basics. I admit, this way is not the most courteous.

As for the includes, it maybe was clumsy to assume one would automatically think to include "string" and I should have remembered that "vector.h" used in Borland is not universal.

Oh, "using namespace std;" is not needed.

Kindly,


Ras.
Jul 20 '07 #10
ravenspoint
111 100+
I bet this comes up all the time. ( I have only being posting here for one day, so I am just finding my way around. ) My thought is that this site should have a few pointers to frequently used gentle tutorials ( not reference guides!!!!! ) that we could use. Because you are perfectly correct, it would be wasteful to fill up every thread with every persons lame attempt to provide a finely tuned tutorial.

Well, if you leave out "using namespace std;" then you are going to have to add std:: in front of every reference to vector - which I had noticed you did not do in the code you posted.
Jul 20 '07 #11
Well, if you leave out "using namespace std;" then you are going to have to add std:: in front of every reference to vector - which I had noticed you did not do in the code you posted.
I understand the whole thing now: I use Borland SDK, and instead of doing "#include <vector>", I work with "#include "vector.h" and then the vectors I use are not actually the ones of the standard library... no "using namespace std;" needed, and last 2 posts confusions understood.

And actually as I'm writing this I only understand now why I was having an unexpected behaviour doing certain specific manipulations with vectors (see one of my previous threads).... that was worth the discussion!

Actually, I'm really wondering if any of the info pointed here has helped the initial poster...


Ras.
Jul 20 '07 #12
ravenspoint
111 100+

Actually, I'm really wondering if any of the info pointed here has helped the initial poster...


Ras.
The OP has been very quiet. We may have scared them away ;-(

If you are still there, macus, then post and let us know how you are getting along with your problem - and we will try to refocus this thread.

Let us know what C++ toolset you are using. MSVC, Borland or something else.
Jul 20 '07 #13

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

Similar topics

4
by: pisscot | last post by:
Lind Apr 13, 6:32 am show options Newsgroups: comp.lang.c From: piss...@gmail.com (Lind) - Find messages by this author Date: 13 Apr 2005 06:32:15 -0700 Local: Wed,Apr 13 2005 6:32 am...
3
by: Wei-Chao Hsu | last post by:
There are some data files look like 1.) data1.txt ---------------- 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0 2.) data2.txt
19
by: ranjeet | last post by:
Hay Guys can you all suggest me the points on the below issue Problem : The thing is that I have the data some thing like this. 1486, 2168, 3751, 9074, 12134, 13944, 17983, 19173, 21190,...
9
by: Adi | last post by:
Hello eveyone, I wanna ask a very simple question here (as it was quite disturbing me for a long time.) My problem is to read a file line by line. I've tried following implementations but still...
6
by: xdeath | last post by:
Hi guys, i've currently got an assignment, whereby, im supposed to create 2 classes, a Vehicle superclass, and a Taxi subclass. Vehicle class needs to have Reg Number, model, price, and Taxi is...
0
by: lovecarole | last post by:
hi, i am the student who should write a program about reading wav file and do the DFT. actually i don't know how to read data of the wav song and save it into the array... if i want to read...
7
by: bowlderster | last post by:
Hello, all. This is the text file named test.txt. 1041 1467 7334 9500 2169 7724 3478 3358 9962 7464 6705 2145 6281 8827 1961 1491 3995 3942 5827 6436 6391 6604 4902 1153 1292 4382 9421 1716...
6
by: trl10 | last post by:
Hi Everyone, I'm still trying to learn C++ and this is my final project. We have to read sales data from a file into an array, process data in a partially filled array via functions, pass an array...
13
by: rohit | last post by:
Hi All, I am new to C language.I want to read integers from a text file and want to do some operation in the main program.To be more specific I need to multiply each of these integers with another...
5
by: dm3281 | last post by:
Hello, I have a text report from a mainframe that I need to parse. The report has about a 2580 byte header that contains binary information (garbage for the most part); although there are a...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
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
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
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,...

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.