473,387 Members | 3,781 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,387 software developers and data experts.

reading into an array.

hey guys,
i am trying to read a csv file into an 3d array.

the file format is the following:
ABC,"12 aug 2006",323.343,5454,4545.4545.4545...
..
...
...
...

for 762 rows and 7 columns.

how can i read this file into a 3D array, one for the column one for the rows and one for the length of the stream..

thanks guys.
Sep 24 '06 #1
2 5043
risby
30
hey guys,
i am trying to read a csv file into an 3d array.

the file format is the following:
ABC,"12 aug 2006",323.343,5454,4545.4545.4545...
..
...
...
...

for 762 rows and 7 columns.

how can i read this file into a 3D array, one for the column one for the rows and one for the length of the stream..

thanks guys.
You might get a better response if you gave some feedback to the previous attempts to help you, e.g. regarding separating fields in a csv file using fgets() and strtok()

http://www.thescripts.com/forum/post2098535-4.html

The code I posted here can be easily adapted by invoking strcpy() and possibly malloc() instead of simply fprintf() with each field read in. You'll also have to maintain column and row counters to index into your array.
Sep 24 '06 #2
you can use getline, but it's only useful if you have a fixed amount of elements on each line of the text file.
Expand|Select|Wrap|Line Numbers
  1.     while (!ifile.eof()) {
  2.         getline(ifile,first,',');
  3.         stdArray[j] = first;
  4.         getline(ifile,second,',');
  5.         stdArray[j] = second;
  6.         getline(ifile,third,' ' );
  7.         stdArray[j] = third;
  8.         getline(ifile,last);
  9.         stdArray[j] = last;
  10. }
  11.  
Make sure the file is open of course before you run the loop. The getline will read everything up to your delimiter, in this case it's "," and store it in your array. you can simply change it to a 3d array. Also, when using getline() to get the last element on the row of your cvs file, make sure you don't specify the delimiter, because it'll start reading the next line if you do.
Everything would have been much easier if you had space between elements. You could have used >> operator to read the file because it automatically scans for spaces.
Sep 24 '06 #3

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

Similar topics

0
by: Bernhard Kuemel | last post by:
Hi! I want to read/write commands and program input to/from /bin/bash several times before I close the stdin pipe. However, reading from cat hangs unless I first close the stdin pipe. <?php...
2
by: Dariusz | last post by:
Below is part of a code I have for a database. While the database table is created correctly (if it doesn't exist), and data is input correctly into the database when executed, I have a problem...
6
by: Dietrich Epp | last post by:
Are there any good modules for reading a bitstream? Specifically, I have a string and I want to be able to get the next N bits as an integer. Right now I'm using struct.unpack and bit operations,...
6
by: Foxy Kav | last post by:
Hi, another question from me again, i was just wondering if anyone could give me a quick example of reading data from a file then placing the data into an array for some manipulation then reading...
3
by: Nick | last post by:
I have found a class that compresses and uncompresses data but need some help with how to use part of it below is the deflate method which compresses the string that I pass in, this works OK. At...
21
by: JoKur | last post by:
Hello, First let me tell you that I'm very new to C# and learning as I go. I'm trying to write a client application to communicate with a server (that I didn't write). Each message from the...
10
by: T Cordon | last post by:
I am using a StreamReader to read text from an HTML file and display it as part of a page in a Label Control. Buy it is not displaying characters as: ñ, ó, ú, etc. Please Help. Thanks
4
by: taquito | last post by:
Hi, a C newbie here. I have been reading threads here about reading txt files into array. I simply copied a couple of useful codes from here and modified to fit my situations. Then, I have two...
0
by: Anish G | last post by:
Hi, I have an issue with reading CSV files. I am to reading CSV file and putting it in a Datatable in C#. I am using a regular expression to read the values. Below is the code. Now, it reads...
21
by: Stephen.Schoenberger | last post by:
Hello, My C is a bit rusty (.NET programmer normally but need to do this in C) and I need to read in a text file that is setup as a table. The general form of the file is 00000000 USNIST00Z...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: 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
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...

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.