| | |
Dynamic Array, Writing to CSV, Floating Point ?'s
![]() |
Then vectors may be more suitable. But this is how I might have adapted things if I would have chosen C.
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <stdlib.h> struct sRecord { int date, time; double price[4], volume; }; int main(void) { static const char filename[] = "file.csv"; FILE *file = fopen(filename, "r"); if ( file ) { size_t i = 0, size = 100000; struct sRecord *array = malloc(size * sizeof *array); if ( array ) { char line[128]; while ( i < size && fgets(line, sizeof line, file) ) { if ( sscanf(line, "%d,%d,%lf,%lf,%lf,%lf,%lf", &array[i].date, &array[i].time, &array[i].price[0], &array[i].price[1], &array[i].price[2], &array[i].price[3], &array[i].volume) == 7 ) { ++i; } } fclose(file); /* * [Modify contents] */ for ( size = i, i = 0; i < size; ++i ) { printf("%d,%d,%g,%g,%g,%g,%g\n", array[i].date, array[i].time, array[i].price[0], array[i].price[1], array[i].price[2], array[i].price[3], array[i].volume); } free(array); } } else { perror(filename); } return 0; }
"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: Jul 2005
Posts: 1,681
Reputation:
Solved Threads: 264
I'll try to enter gently (into the conversation of others), but I believe the process could be demonstrated something like this using C++:
//variables to write to file
int date = 10;
char month[12] = "June";
float first = 1.011;
float second = 2005.2;
//using C++ style file I/O
ofstream fout("sample.txt");
fout << date << ',' << month << ',' << first << ',' << second;
sample.txt should now be in CSV format.
To read in from file would be:
ifstream fin("sample.txt");
//use different variables to confirm that things read back in correctly.
int date_;
char month_[12];
float first_;
float second_;
char dummy;
fin >> date_ >> dummy >> month_ >> dummy >> first_ >> dummy >> second_;
I agree with Dave Sinkula, that this sounds like a good project to use struct/class with and overload the << for the struct if you have that option available.
I don't have access to a compiler to confirm my code sample, sorry.
//variables to write to file
int date = 10;
char month[12] = "June";
float first = 1.011;
float second = 2005.2;
//using C++ style file I/O
ofstream fout("sample.txt");
fout << date << ',' << month << ',' << first << ',' << second;
sample.txt should now be in CSV format.
To read in from file would be:
ifstream fin("sample.txt");
//use different variables to confirm that things read back in correctly.
int date_;
char month_[12];
float first_;
float second_;
char dummy;
fin >> date_ >> dummy >> month_ >> dummy >> first_ >> dummy >> second_;
I agree with Dave Sinkula, that this sounds like a good project to use struct/class with and overload the << for the struct if you have that option available.
I don't have access to a compiler to confirm my code sample, sorry.
In C++ I might write it something like this (for starters).
[D'oh. Pokey on the C++ conversion.]
C Syntax (Toggle Plain Text)
#include <iostream> #include <fstream> #include <string> #include <vector> #include <algorithm> #include <iterator> #include <cstdio> struct sRecord { int date, time; double price[4], volume; }; bool operator< (const struct sRecord &a, const struct sRecord &b) { return a.date < b.date; } std::ostream& operator<< (std::ostream &o, struct sRecord &r) { o << r.date << "," << r.time << "," << r.price[0] << "," << r.price[1] << "," << r.price[2] << "," << r.price[3] << "," << r.volume; return o; } int main(void) { std::vector<sRecord> record; const std::string filename("file.csv"); std::ifstream file(filename.c_str()); if ( file ) { std::string line; while ( file >> line ) { sRecord data; if ( std::sscanf(line.c_str(), "%d,%d,%lf,%lf,%lf,%lf,%lf", &data.date, &data.time, &data.price[0], &data.price[1], &data.price[2], &data.price[3], &data.volume) == 7 ) { record.push_back(data); } } } /* * [Modify contents] */ std::sort(record.begin(), record.end()); std::copy(record.begin(), record.end(), std::ostream_iterator<sRecord>(std::cout, "\n")); return 0; }
"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
Alright, just so you get an idea of where I'm at...I'm putting in red any of the lines that I don't understand. At this point I'm going to just sit here and study this code and try and get to the point where I can walk through it.
One question, the program isn't outputting anything? Is there something missing?
One question, the program isn't outputting anything? Is there something missing?
•
•
•
•
Originally Posted by Dave Sinkula
Then vectors may be more suitable. But this is how I might have adapted things if I would have chosen C.
#include <stdio.h> #include <stdlib.h> struct sRecord { int date, time; double price[4], volume; }; int main(void) { static const char filename[] = "file.csv"; FILE *file = fopen(filename, "r"); if ( file ) { size_t i = 0, size = 100000; struct sRecord *array = malloc(size * sizeof *array); if ( array ) { char line[128]; while ( i < size && fgets(line, sizeof line, file) ) { if ( sscanf(line, "%d,%d,%lf,%lf,%lf,%lf,%lf", &array[i].date, &array[i].time, &array[i].price[0], &array[i].price[1], &array[i].price[2], &array[i].price[3], &array[i].volume) == 7 ) { ++i; } } fclose(file); /* * [Modify contents] */ for ( size = i, i = 0; i < size; ++i ) { printf("%d,%d,%g,%g,%g,%g,%g\n", array[i].date, array[i].time, array[i].price[0], array[i].price[1], array[i].price[2], array[i].price[3], array[i].volume); } free(array); } } else { perror(filename); } return 0; }
Declare a pointer and attempt to allocate memory to contain an array of these structures, assigning the result of the attempt to the pointer: A buffer into which to read a line of data: Read a line of data: Sprinkle some debugging printfs around.
C Syntax (Toggle Plain Text)
struct sRecord *array = malloc(size * sizeof *array);
C Syntax (Toggle Plain Text)
char line[128];
while ( i < size && fgets(line, sizeof line, file) )•
•
•
•
Originally Posted by NolanVW
One question, the program isn't outputting anything? Is there something missing?
"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: Jul 2005
Posts: 1,681
Reputation:
Solved Threads: 264
Darn it, reading CSV files directly into numerical values using C++ is going to take more using >> directly. Using >> to read from a CSV will consider the comma to be part of a string, not a delimiter; and for numerical value it will fill the initial variable, but since commas are not valid in int/float/double variables, the input will stop and the fail bit of the input stream is likely to be flipped when the delimiting comma is found. This in turn will invalidate the input stream until it is cleared, which would be very cumbersome to do after each input into a numerical value. I should have realized that initially, sorry.
Instead I think it would be best to read each piece of information between delimiting commas into strings using get() or getline() using ',' as the terminating char (I'd probably use getline() so it would remove the terminating comma from the input buffer when it was encountered without an additional step like using get() would require). Then since the fields would be known in advance, I'd convert the necessary strings to int/double/float whatever after the read in. Messy, but doable.
Seems like the OP is leaning toward C style rather than C++ so it's probably moot, except as (imperfect initial) demonstration.
EDIT:
Are comma separated files routinely divided into lines or is everything data comma data comma data comma until EOF without newline separation, or is either way appropriate?
I don't use C style I/O routinely, so how will sscanf() handle the comma delimiters--remove them from the string so they don't muddle up things automatically or do users have to account for them?
Instead I think it would be best to read each piece of information between delimiting commas into strings using get() or getline() using ',' as the terminating char (I'd probably use getline() so it would remove the terminating comma from the input buffer when it was encountered without an additional step like using get() would require). Then since the fields would be known in advance, I'd convert the necessary strings to int/double/float whatever after the read in. Messy, but doable.
Seems like the OP is leaning toward C style rather than C++ so it's probably moot, except as (imperfect initial) demonstration.
EDIT:
Are comma separated files routinely divided into lines or is everything data comma data comma data comma until EOF without newline separation, or is either way appropriate?
I don't use C style I/O routinely, so how will sscanf() handle the comma delimiters--remove them from the string so they don't muddle up things automatically or do users have to account for them?
•
•
Join Date: Jun 2005
Posts: 12
Reputation:
Solved Threads: 0
Okay...so here's where it's at:
Here's the test data:
20041201,0:00,102.74,102.77,102.74,102.76,5
20041201,0:05,102.78,102.78,102.75,102.75,3
20041201,0:10,102.75,102.75,102.7,102.7,7
20041201,0:15,102.7,102.72,102.7,102.72,8
20041201,0:20,102.73,102.74,102.73,102.74,2
20041201,0:25,102.73,102.73,102.7,102.7,4
20041201,0:30,102.71,102.71,102.68,102.7,10
20041201,0:35,102.71,102.73,102.68,102.69,15
20041201,0:40,102.7,102.71,102.69,102.71,12
20041201,0:45,102.71,102.73,102.71,102.73,2
20041201,0:50,102.74,102.78,102.74,102.77,12
20041201,0:55,102.77,102.8,102.77,102.79,17
Here's the output:
file opened
Press any key to continue . . .
assigned memory to array address
Press any key to continue . . .
entering While loop
Press any key to continue . . .
leaving While loop
Press any key to continue . . .
Closed File
Press any key to continue . . .
20041201,0,0.000000,0.000000,0.000000,0.000000,3998072
0, ,0.000000,0.000000,0.000000,0.000000,0
0, ,0.000000,0.000000,0.000000,0.000000,0
Test Row
Press any key to continue . . .
20041201,0,0.000000,0.000000,0.000000,0.000000,3998072
0, ,0.000000,0.000000,0.000000,0.000000,0
0, ,0.000000,0.000000,0.000000,0.000000,0
0, ,0.000000,0.000000,0.000000,0.000000,0
0, ,0.000000,0.000000,0.000000,0.000000,0
0, ,0.000000,0.000000,0.000000,0.000000,0
0, ,0.000000,0.000000,0.000000,0.000000,0
0, ,0.000000,0.000000,0.000000,0.000000,0
0, ,0.000000,0.000000,0.000000,0.000000,0
0, ,0.000000,0.000000,0.000000,0.000000,0
0, ,0.000000,0.000000,0.000000,0.000000,0
0, ,0.000000,0.000000,0.000000,0.000000,0
0, ,0.000000,0.000000,0.000000,0.000000,0
0, ,0.000000,0.000000,0.000000,0.000000,0
0, ,0.000000,0.000000,0.000000,0.000000,0
0, ,0.000000,0.000000,0.000000,0.000000,0
0, ,0.000000,0.000000,0.000000,0.000000,0
0, ,0.000000,0.000000,0.000000,0.000000,0
0, ,0.000000,0.000000,0.000000,0.000000,0
0, ,0.000000,0.000000,0.000000,0.000000,0
0, ,0.000000,0.000000,0.000000,0.000000,0
0, ,0.000000,0.000000,0.000000,0.000000,0
0, ,0.000000,0.000000,0.000000,0.000000,0
0, ,0.000000,0.000000,0.000000,0.000000,0
0, ,0.000000,0.000000,0.000000,0.000000,0
0, ,0.000000,0.000000,0.000000,0.000000,0
0, ,0.000000,0.000000,0.000000,0.000000,0
0, ,0.000000,0.000000,0.000000,0.000000,0
0, ,0.000000,0.000000,0.000000,0.000000,0
0, ,0.000000,0.000000,0.000000,0.000000,0
File printed
Press any key to continue . . .
My Questions:
1) How is malloc supposed to provide the correct amount of memory if it doesn't even know what's stored in array[] yet? Doesn't array[] get filled in the loop below the allocation statement? So it won't know the size until then?
2) The program is only printing the correct first date in the list and then prints 0's for everything else...anyone see a reason?
Thanks for the help,
Nolan
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <stdlib.h> struct sRecord { int date, volume; char time; double price[4]; }; int main(void) { static const char filename[] = "file.csv"; FILE *file = fopen(filename, "r"); printf("file opened\n"); system("pause"); if ( file ) { size_t i = 0, size = 30; struct sRecord *array = malloc(size * sizeof *array); printf("assigned memory to array address\n"); system("pause"); if ( array ) { char line[128]; printf("entering While loop\n"); system("pause"); while ( i < size && fgets(line, sizeof line, file) ) { if ( sscanf(line, "%d,%c,%lf,%lf,%lf,%lf,%ld", &array[i].date, &array[i].time, &array[i].price[0], &array[i].price[1], &array[i].price[2], &array[i].price[3], &array[i].volume) == 7 ) { ++i; } } printf("leaving While loop\n"); system("pause"); fclose(file); printf("Closed File\n"); system("pause"); /* Test first three rows of Array */ printf("%d,%c,%lf,%lf,%lf,%lf,%ld\n", array[0].date, array[0].time, array[0].price[0], array[0].price[1], array[0].price[2], array[0].price[3], array[0].volume); printf("\n"); printf("%d,%c,%lf,%lf,%lf,%lf,%ld\n", array[1].date, array[1].time, array[1].price[0], array[1].price[1], array[1].price[2], array[1].price[3], array[1].volume); printf("\n"); printf("%d,%c,%lf,%lf,%lf,%lf,%ld\n", array[2].date, array[2].time, array[2].price[0], array[2].price[1], array[2].price[2], array[2].price[3], array[2].volume); printf("Test Row\n"); system("pause"); /* * [Modify contents] */ for ( i = 0; i < size; ++i ) { printf("%d,%c,%lf,%lf,%lf,%lf,%ld\n", array[i].date, array[i].time, array[i].price[0], array[i].price[1], array[i].price[2], array[i].price[3], array[i].volume); } printf("File printed\n"); system("pause"); free(array); } } else { perror(filename); } system("pause"); return 0; }
Here's the test data:
20041201,0:00,102.74,102.77,102.74,102.76,5
20041201,0:05,102.78,102.78,102.75,102.75,3
20041201,0:10,102.75,102.75,102.7,102.7,7
20041201,0:15,102.7,102.72,102.7,102.72,8
20041201,0:20,102.73,102.74,102.73,102.74,2
20041201,0:25,102.73,102.73,102.7,102.7,4
20041201,0:30,102.71,102.71,102.68,102.7,10
20041201,0:35,102.71,102.73,102.68,102.69,15
20041201,0:40,102.7,102.71,102.69,102.71,12
20041201,0:45,102.71,102.73,102.71,102.73,2
20041201,0:50,102.74,102.78,102.74,102.77,12
20041201,0:55,102.77,102.8,102.77,102.79,17
Here's the output:
file opened
Press any key to continue . . .
assigned memory to array address
Press any key to continue . . .
entering While loop
Press any key to continue . . .
leaving While loop
Press any key to continue . . .
Closed File
Press any key to continue . . .
20041201,0,0.000000,0.000000,0.000000,0.000000,3998072
0, ,0.000000,0.000000,0.000000,0.000000,0
0, ,0.000000,0.000000,0.000000,0.000000,0
Test Row
Press any key to continue . . .
20041201,0,0.000000,0.000000,0.000000,0.000000,3998072
0, ,0.000000,0.000000,0.000000,0.000000,0
0, ,0.000000,0.000000,0.000000,0.000000,0
0, ,0.000000,0.000000,0.000000,0.000000,0
0, ,0.000000,0.000000,0.000000,0.000000,0
0, ,0.000000,0.000000,0.000000,0.000000,0
0, ,0.000000,0.000000,0.000000,0.000000,0
0, ,0.000000,0.000000,0.000000,0.000000,0
0, ,0.000000,0.000000,0.000000,0.000000,0
0, ,0.000000,0.000000,0.000000,0.000000,0
0, ,0.000000,0.000000,0.000000,0.000000,0
0, ,0.000000,0.000000,0.000000,0.000000,0
0, ,0.000000,0.000000,0.000000,0.000000,0
0, ,0.000000,0.000000,0.000000,0.000000,0
0, ,0.000000,0.000000,0.000000,0.000000,0
0, ,0.000000,0.000000,0.000000,0.000000,0
0, ,0.000000,0.000000,0.000000,0.000000,0
0, ,0.000000,0.000000,0.000000,0.000000,0
0, ,0.000000,0.000000,0.000000,0.000000,0
0, ,0.000000,0.000000,0.000000,0.000000,0
0, ,0.000000,0.000000,0.000000,0.000000,0
0, ,0.000000,0.000000,0.000000,0.000000,0
0, ,0.000000,0.000000,0.000000,0.000000,0
0, ,0.000000,0.000000,0.000000,0.000000,0
0, ,0.000000,0.000000,0.000000,0.000000,0
0, ,0.000000,0.000000,0.000000,0.000000,0
0, ,0.000000,0.000000,0.000000,0.000000,0
0, ,0.000000,0.000000,0.000000,0.000000,0
0, ,0.000000,0.000000,0.000000,0.000000,0
0, ,0.000000,0.000000,0.000000,0.000000,0
File printed
Press any key to continue . . .
My Questions:
1) How is malloc supposed to provide the correct amount of memory if it doesn't even know what's stored in array[] yet? Doesn't array[] get filled in the loop below the allocation statement? So it won't know the size until then?
2) The program is only printing the correct first date in the list and then prints 0's for everything else...anyone see a reason?
Thanks for the help,
Nolan
•
•
•
•
Originally Posted by NolanVW
1) How is malloc supposed to provide the correct amount of memory if it doesn't even know what's stored in array[] yet?
•
•
•
•
Originally Posted by NolanVW
2) The program is only printing the correct first date in the list and then prints 0's for everything else...anyone see a reason?
[edit]Maybe I'll edit again to highlight some of the differences.
#include <stdio.h>
#include <stdlib.h>
struct sRecord
{
int date, volume;
char time[8];
double price[4];
};
int main(void)
{
static const char filename[] = "file.csv";
FILE *file = fopen(filename, "r");
if ( file )
{
size_t i = 0, count, size = 30;
struct sRecord *array = malloc(size * sizeof *array);
if ( array )
{
char line[128];
while ( i < size && fgets(line, sizeof line, file) )
{
if ( sscanf(line, "%d,%7[^,],%lf,%lf,%lf,%lf,%d",
&array[i].date, array[i].time,
&array[i].price[0], &array[i].price[1],
&array[i].price[2], &array[i].price[3],
&array[i].volume) == 7 )
{
++i;
}
}
fclose(file);
/*
* [Modify contents]
*/
for ( count = i, i = 0; i < count; ++i )
{
printf("%d,%s,%f,%f,%f,%f,%d\n", array[i].date, array[i].time,
array[i].price[0], array[i].price[1], array[i].price[2],
array[i].price[3], array[i].volume);
}
free(array);
}
}
else
{
perror(filename);
}
return 0;
}
/* file.csv
20041201,0:00,102.74,102.77,102.74,102.76,5
20041201,0:05,102.78,102.78,102.75,102.75,3
20041201,0:10,102.75,102.75,102.7,102.7,7
20041201,0:15,102.7,102.72,102.7,102.72,8
20041201,0:20,102.73,102.74,102.73,102.74,2
20041201,0:25,102.73,102.73,102.7,102.7,4
20041201,0:30,102.71,102.71,102.68,102.7,10
20041201,0:35,102.71,102.73,102.68,102.69,15
20041201,0:40,102.7,102.71,102.69,102.71,12
20041201,0:45,102.71,102.73,102.71,102.73,2
20041201,0:50,102.74,102.78,102.74,102.77,12
20041201,0:55,102.77,102.8,102.77,102.79,17
*/
/* my output
20041201,0:00,102.740000,102.770000,102.740000,102.760000,5
20041201,0:05,102.780000,102.780000,102.750000,102.750000,3
20041201,0:10,102.750000,102.750000,102.700000,102.700000,7
20041201,0:15,102.700000,102.720000,102.700000,102.720000,8
20041201,0:20,102.730000,102.740000,102.730000,102.740000,2
20041201,0:25,102.730000,102.730000,102.700000,102.700000,4
20041201,0:30,102.710000,102.710000,102.680000,102.700000,10
20041201,0:35,102.710000,102.730000,102.680000,102.690000,15
20041201,0:40,102.700000,102.710000,102.690000,102.710000,12
20041201,0:45,102.710000,102.730000,102.710000,102.730000,2
20041201,0:50,102.740000,102.780000,102.740000,102.770000,12
20041201,0:55,102.770000,102.800000,102.770000,102.790000,17
*/ "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 |
adobe api array arrays bash binarysearch calculate centimeter char cm convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic feet fflush file floatingpointvalidation fork forloop frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators initialization intmain() iso km linked linkedlist linux linuxsegmentationfault list locate logical_drives loopinsideloop. match matrix microsoft motherboard mqqueue mysql oddnumber odf open opendocumentformat opensource openwebfoundation pattern pdf performance pointer pointers posix power program programming pyramidusingturboccodes read recursion recv recvblocked repetition scanf scheduling scripting segmentationfault send shape socketprograming socketprogramming stack standard strchr string suggestions test testautomation unix urboc user variable voidmain() whythiscodecausesegmentationfault win32api windows.h






