i have the following code but i am stuck.
i have to read from a csv file into an array, than use the array to sort the data in the file, than search the file for specific values.
my csv file is as the following:
BHP,"27 Aug 2002",9.53,9.56,9.45,9.45,8117955
BHP,"26 Aug 2002",9.45,9.48,9.4,9.48,6990617
BHP,"28 Aug 2002",9.54,9.55,9.4,9.44,12771227
BHP,"29 Aug 2002",9.31,9.31,9.21,9.27,11986227
BHP,"30 Aug 2002",9.21,9.23,9.11,9.11,19340245
BHP,"02 Sep 2002",9.15,9.23,9.05,9.23,8681758
BHP,"03 Sep 2002",9.14,9.25,9.14,9.24,7833427
BHP,"04 Sep 2002",9,9.01,8.88,8.9,18221850
i donno how to read each column in the file.
so if u plz can help me.
CODE
#include <stdio.h>
#include <stdlib.h>
#define max_r 762
#define max_c 7
typedef enum
{jan=1,feb=2,mar=3,apr=4,may=5,jun=6,jul=7,aug=8,s ep=9,oct=10,nov=11,dec=12}month;
typedef struct {
int date;
int month;
int year;
}DATE;
typedef struct {
char stockName[20];
DATE date;
int closePrice;
}BHP;
char filename[15];
FILE * pFile;
/*functions prototypes*/
unsigned char OpenFile();
void clearStdin(void);
char menu();
int main()
{
long lSize;
char * buffer;
unsigned char quit=0; /*used to terminate the program = exit*/
char menuoption; /* identifies the menu used*/
unsigned char FileOpened=0; /* used to open the file intended to read*/
int day, year;
char month[4];
int i,j;
char read;
pFile = fopen("bhp.csv","r");
if(pFile == NULL)
{
printf("Error opening the file!!!\n\n");
return 0;
}
/*obtain file size.*/
fseek (pFile,0, SEEK_END);
lSize = ftell (pFile);
rewind (pFile);
/* allocate memory to contain the whole file.*/
buffer = (char*) malloc (lSize);
if (buffer == NULL)
{
printf("not enough memory!!!");
return 0;
}
/**********/
/* copy the file into the buffer.*/
for(i=0;i<max_r;++i)*/ /*specify what to read*/
/*for(j=0;j<1;++j)
{
fgets(buffer,max_r, pFile); */ /*read from the file bhp.csv*/
/*}*/
/*** the whole file is loaded in the buffer. ***/
/*opening the file and the operations*/
while(quit==0) /* check that the entered option is valid*/
{
if(FileOpened==0)
FileOpened=OpenFile();
else{
menuoption=menu(); /* display menu an get the option entered */
if(menuoption=='g'){
for(i=0;i<max_r;++i)
fgets(buffer,4,pFile);
puts(buffer);
printf("the buffer is %s", read); /*g option is selected*/
printf("\nDate (dd/mm/yyyy):");
}
else if(menuoption=='s'){ /*s option is selected*/
printf("User pressed s\n");
}
else if (menuoption=='q'){ /*q option is selcted*/
printf("\n\n\nThank you for using StockAnalyser verion (1.0)\n\n");
quit=1;
}
else{
printf("Wrong Input\n");
}
}
}
/* termiate*/
fclose (pFile);
free(buffer);
return 0;
}
unsigned char OpenFile()
{
printf("$ StockAnalyser\n");
printf("Enter the stock file name:");
scanf("%s", filename);
printf("Filename to open: %s\n\n", &filename);
pFile = fopen(filename,"r"); /* open the file to read*/
if (pFile == NULL) /*checks if the program is open correctly*/
{
printf("Error opening file %s\n\n", &filename);
return 0;
}
printf("\n\n");
printf("Number of records: 762 from 26/08/2002 to 26/08/2005\n\n");
printf("Welcome to use StockAnalyser (version 1.0)\n\n\n");
return 1;
return 0;
}
void clearStdin(void) /* remove wrong characters from stdin */
{
scanf("%*[^\n]"); /* skip to the end-of-line */
scanf("%*1[\n]"); /* skip one new-line */
return;
}
char menu()
{
char option;
clearStdin();
printf("[g] Get the price of a specific date\n");
printf("[s] Get the statistics of a period\n");
printf("[q] Quit\n");
printf(" please select [g, s or q]:");
scanf("%c", &option);
return option;
}
edit: added [code] tags ~ jayman9