Dynamic Array, Writing to CSV, Floating Point ?'s

Reply

Join Date: Jun 2005
Posts: 12
Reputation: NolanVW is an unknown quantity at this point 
Solved Threads: 0
NolanVW NolanVW is offline Offline
Newbie Poster

Dynamic Array, Writing to CSV, Floating Point ?'s

 
0
  #1
Aug 3rd, 2005
Below is a program in C that I am trying to use to read in a spreadsheet (in CSV form), which I will eventually be doing calculations on and then writing out to a new file. I'm currently trying to figure out the following three things:

1) How to make it so that the matrix is dynamically allocated memory. Right now it's set to be 10 x 10, but I want it to read the number of items between comma's as the width and the number of rows as the height. Can I just add the number of occurences of commas and add 1 to the total to get the width?

2) Right now it just writes out the data that it's reading in from the file, but could someone point me in the right direction for writing to a new CSV file with the - eventually modified - data?

3) I want to work with floating point numbers to two spaces (i.e. 10.14), where should I be defining that? I tried "float array[10][10] and a few other things, but wasn't too successful.

I'm pretty new to C, and these forums have been a huge help so far, thanks to everyone who's contributed to any of the threads.




  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define ARRAYSIZE(x) (sizeof(x)/sizeof(*(x)))
  5.  
  6. int main(void)
  7. {
  8.  
  9. const char filename[] = "file.csv";
  10.  
  11. /* Open the file */
  12. FILE *file = fopen(filename, "r");
  13.  
  14. if (file)
  15. {
  16. /* Need dynamic allocation here */
  17. float array[10][10];
  18. size_t i, j, k;
  19. char buffer[BUFSIZ], *ptr;
  20.  
  21. /* Read each line from the file */
  22. for(i=0; fgets(buffer, sizeof buffer, file); i++)
  23. {
  24. /* Parse the comma-seperated values from each line into 'array' */
  25. for(j=0, ptr=buffer; j < ARRAYSIZE(*array); ++j, ++ptr)
  26. {
  27. array[i][j] = (int)strtol(ptr, &ptr, 10);
  28. }
  29. }
  30.  
  31. fclose(file);
  32.  
  33. /* Print the data in 'array' */
  34. for(j = 0; j < i; ++j)
  35. {
  36. printf("array[%lu]:", (long unsigned)j);
  37.  
  38. for(k = 0; k < ARRAYSIZE(*array); ++k)
  39. {
  40. printf("%4d", array[j][k]);
  41. }
  42.  
  43. putchar('\n');
  44. }
  45. }
  46.  
  47. else
  48. {
  49. perror(filename);
  50. }
  51.  
  52. system("pause");
  53.  
  54. return 0;
  55. }


Sample data I'm using:

2018,015,110.87,110.9,110.87,110.9,9,9,9,9
2018,020,110.9,110.9,110.86,110.88,5,5,5,5
20,025,110.87,110.88,110.85,110.88,7,7,7,7
2018,030,110.88,110.95,110.87,110.93,10,10,10,10
18,035,110.93,110.94,110.91,110.92,9,9,9,9
20,040,110.92,110.93,110.9,110.91,11,11,11,11
218,045,110.91,110.93,110.91,110.91,6,6,6,6
20218,050,110.91,110.93,110.9,110.9,10,10,10,10
218,055,110.9,110.93,110.9,110.92,11,11,11,11
20218,100,110.89,110.93,110.9,110.94,12,12,12,12



Thanks for any help!

Nolan
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,321
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 230
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Dynamic Array, Writing to CSV, Floating Point ?'s

 
0
  #2
Aug 3rd, 2005
Say, that code looks quite familiar. How much have you tried doing yourself?

[edit]Cross-posted.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 12
Reputation: NolanVW is an unknown quantity at this point 
Solved Threads: 0
NolanVW NolanVW is offline Offline
Newbie Poster

Re: Dynamic Array, Writing to CSV, Floating Point ?'s

 
0
  #3
Aug 4th, 2005
Originally Posted by Dave Sinkula
Say, that code looks quite familiar. How much have you tried doing yourself?
As I said, I am quite new to C programming, that code is someone else's instructions on how to read a CSV file. It took me a half hour just to look through it and understand what was going on (and I still don't understand it all).

I'm trying to understand a process...I searched around for code on how to read in CSV files, found what I posted, attempted to change what I needed to to make floating point numbers, was unsuccessful, signed up for a message board, asked how to work with floating point numbers and then I realized I would need to output to CSV, so I asked about how to do that.

So basically...from the beginning I knew nothing about working with CSV files, so instead of asking how to do the above, I researched sites to find it. If you'd rather me waste more of your time by asking how to do the above, I'd be glad to. I've been working on the code today and still to no avail. It's a lot more efficient and effective to learn from others than screw up code for 2 weeks straight before giving up on a program. At least that's how I see it.

I'm not looking for you to write my code, I'm looking for someone who can point me in a right direction.

Thanks for anyone who can offer some help
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 12
Reputation: NolanVW is an unknown quantity at this point 
Solved Threads: 0
NolanVW NolanVW is offline Offline
Newbie Poster

Re: Dynamic Array, Writing to CSV, Floating Point ?'s

 
0
  #4
Aug 4th, 2005
though I did just see that you offered advice on the other forum, so thank you.

I didn't realize that people would take such offense to asking advice on pre-written code on multiple boards and will point out sources in the future.

I made the basic assumption that the more people who saw the code, the more advice I'd get.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,321
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 230
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Dynamic Array, Writing to CSV, Floating Point ?'s

 
0
  #5
Aug 4th, 2005
>though I did just see that you offered advice on the other forum, so thank you.

Now you yourself have tasted the confusion of cross posting.

>I didn't realize that people would take such offense to asking advice on pre-written code on multiple boards and will point out sources in the future.

Perhaps I was too jumpy as well. But discovering code I wrote being posted here after it was made incorrect, and then seeing an identical thread elsewhere...

I find cross-posting to be rude. And at first I thought you just grabbed the code and didn't acknowledge that it wasn't yours (I read code before the text, usually). I've seen folks copy my code and say, "Hey, look, I wrote this but I need help making a little change..."

>I made the basic assumption that the more people who saw the code, the more advice I'd get.

Perhaps. Not all the advice you get may be in regard to the code, no?

If you cross-post, that's your choice. I suggest at least updating each cross-post with links to where it is cross-posted. Otherwise someone might spend a fair amount of time trying to help, only to be handed one of those "thanks, but I got it working" [with the help from one of the cross posts].


But back to the original issue. What is the big picture?

Do you even need an array, or could you simply read, modify and write each value?

Is the matrix rectangular? Is the size fixed or could it vary?
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 12
Reputation: NolanVW is an unknown quantity at this point 
Solved Threads: 0
NolanVW NolanVW is offline Offline
Newbie Poster

Re: Dynamic Array, Writing to CSV, Floating Point ?'s

 
0
  #6
Aug 5th, 2005
Originally Posted by Dave Sinkula
>Perhaps I was too jumpy as well. But discovering code I wrote being posted here after it was made incorrect, and then seeing an identical thread elsewhere...
My apologies, I didn't even see that you were the one who wrote that code. Now that I step back, it seems like everywhere I've looked or posted, it had something to do with you. Strange.

Originally Posted by Dave Sinkula
>If you cross-post, that's your choice. I suggest at least updating each cross-post with links to where it is cross-posted. Otherwise someone might spend a fair amount of time trying to help, only to be handed one of those "thanks, but I got it working" [with the help from one of the cross posts].
Will do.


Originally Posted by Dave Sinkula
>But back to the original issue. What is the big picture?

Do you even need an array, or could you simply read, modify and write each value?

Is the matrix rectangular? Is the size fixed or could it vary?
Well each value will have something different applied to it. The basic formats of each row go as follows:

date, time, price, price, price, price, volume,

So it will always be 7 columns, but the number of rows will always vary.

The reason I thought I would need an array is to go about sorting the columns by date and time, though I could also do that in excel before saving the .csv file. A problem I'm dealing with is that there is more than 65,000 rows of data, so I have to use rows A-G, then move to H-N for the rest, and so on.

At that point I have to format the date from mm/dd/yyyy to yyyymmdd and the time from standard to military (which I assume won't be hard to just have done in the program as well). I then copy all of the rows into notepad where the file is then complete for my purposes and moved on to someone else.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 12
Reputation: NolanVW is an unknown quantity at this point 
Solved Threads: 0
NolanVW NolanVW is offline Offline
Newbie Poster

Re: Dynamic Array, Writing to CSV, Floating Point ?'s

 
0
  #7
Aug 5th, 2005
Also, I guess there's no real reason why this couldn't be done in C++. Do you see some specific advantages to going that route?
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,321
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 230
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Dynamic Array, Writing to CSV, Floating Point ?'s

 
0
  #8
Aug 5th, 2005
Vectors, strings vs dynamic allocation in C was the reason for that.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,321
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 230
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Dynamic Array, Writing to CSV, Floating Point ?'s

 
0
  #9
Aug 5th, 2005
Originally Posted by NolanVW
Well each value will have something different applied to it. The basic formats of each row go as follows:

date, time, price, price, price, price, volume,

So it will always be 7 columns, but the number of rows will always vary.
Sounds like you might want an array of structs.

Originally Posted by NolanVW
A problem I'm dealing with is that there is more than 65,000 rows of data, so I have to use rows A-G, then move to H-N for the rest, and so on.
Gotcha. Is the number of rows fixed?

Originally Posted by NolanVW
At that point I have to format the date from mm/dd/yyyy to yyyymmdd and the time from standard to military (which I assume won't be hard to just have done in the program as well). I then copy all of the rows into notepad where the file is then complete for my purposes and moved on to someone else.
M'kay.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 12
Reputation: NolanVW is an unknown quantity at this point 
Solved Threads: 0
NolanVW NolanVW is offline Offline
Newbie Poster

Re: Dynamic Array, Writing to CSV, Floating Point ?'s

 
0
  #10
Aug 5th, 2005
the number of rows is not fixed
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC