943,793 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 7541
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Aug 3rd, 2005
0

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

Expand Post »
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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
NolanVW is offline Offline
12 posts
since Jun 2005
Aug 3rd, 2005
0

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

Say, that code looks quite familiar. How much have you tried doing yourself?

[edit]Cross-posted.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Aug 4th, 2005
0

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

Quote 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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
NolanVW is offline Offline
12 posts
since Jun 2005
Aug 4th, 2005
0

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

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
NolanVW is offline Offline
12 posts
since Jun 2005
Aug 4th, 2005
0

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

>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?
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Aug 5th, 2005
0

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

Quote 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.

Quote 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.


Quote 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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
NolanVW is offline Offline
12 posts
since Jun 2005
Aug 5th, 2005
0

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

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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
NolanVW is offline Offline
12 posts
since Jun 2005
Aug 5th, 2005
0

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

Vectors, strings vs dynamic allocation in C was the reason for that.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Aug 5th, 2005
0

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

Quote 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.

Quote 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?

Quote 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.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Aug 5th, 2005
0

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

the number of rows is not fixed
Reputation Points: 10
Solved Threads: 0
Newbie Poster
NolanVW is offline Offline
12 posts
since Jun 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: graphices header file
Next Thread in C Forum Timeline: Pointer question





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC