| | |
Dynamic Array, Writing to CSV, Floating Point ?'s
![]() |
•
•
Join Date: Jun 2005
Posts: 12
Reputation:
Solved Threads: 0
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.
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
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.
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <stdlib.h> #define ARRAYSIZE(x) (sizeof(x)/sizeof(*(x))) int main(void) { const char filename[] = "file.csv"; /* Open the file */ FILE *file = fopen(filename, "r"); if (file) { /* Need dynamic allocation here */ float array[10][10]; size_t i, j, k; char buffer[BUFSIZ], *ptr; /* Read each line from the file */ for(i=0; fgets(buffer, sizeof buffer, file); i++) { /* Parse the comma-seperated values from each line into 'array' */ for(j=0, ptr=buffer; j < ARRAYSIZE(*array); ++j, ++ptr) { array[i][j] = (int)strtol(ptr, &ptr, 10); } } fclose(file); /* Print the data in 'array' */ for(j = 0; j < i; ++j) { printf("array[%lu]:", (long unsigned)j); for(k = 0; k < ARRAYSIZE(*array); ++k) { printf("%4d", array[j][k]); } putchar('\n'); } } else { perror(filename); } system("pause"); return 0; }
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
"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
•
•
Join Date: Jun 2005
Posts: 12
Reputation:
Solved Threads: 0
•
•
•
•
Originally Posted by Dave Sinkula
Say, that code looks quite familiar. How much have you tried doing yourself?
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
•
•
Join Date: Jun 2005
Posts: 12
Reputation:
Solved Threads: 0
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.
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.
>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?
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
•
•
Join Date: Jun 2005
Posts: 12
Reputation:
Solved Threads: 0
•
•
•
•
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...
•
•
•
•
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].
•
•
•
•
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?
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.
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
•
•
•
•
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.
•
•
•
•
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.
•
•
•
•
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.
"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
![]() |
Other Threads in the C Forum
- Previous Thread: graphices header file
- Next Thread: Pointer question
| Thread Tools | Search this Thread |
* ansi api array arrays binarysearch calculate centimeter changingto char character convert copyanyfile copypdffile creafecopyofanytypeoffileinc createcopyoffile createprocess() csyntax directory dynamic fflush file floatingpointvalidation forloop frequency function getlasterror getlogicaldrivestrin givemetehcodez grade graphics gtkgcurlcompiling gtkwinlinux highest histogram homework i/o inches include infiniteloop input intmain() iso keyboard km linked linkedlist linux linuxsegmentationfault list logical_drives looping loopinsideloop. lowest match matrix microsoft mqqueue mysql oddnumber odf open opendocumentformat openwebfoundation pdf posix power program programming pyramidusingturboccodes radix read recursion recv recvblocked repetition reversing scanf scheduling segmentationfault send shape single socketprogramming stack standard strchr string suggestions test threads turboc unix urboc user variable whythiscodecausesegmentationfault win32api windows.h windowsapi






