View Single Post
Old Sep 30th, 2006, 12:03 AM   #14
beginnerCCC
Newbie
 
Join Date: Sep 2006
Posts: 29
Rep Power: 0 beginnerCCC is on a distinguished road
sorry again,
this is my code.
#include <stdio.h>
#include <stdlib.h>
#include<string.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;

/* formating the date*/
typedef struct {
int date;
int month;
int year;
}DATE;

/* naming each column*/
typedef struct {
char StockName[20];
DATE date;
float third;
float forth;
float closePrice;
float sixth;
}BHP;

char filename[15];

FILE * infile;

/*functions prototypes*/
unsigned char OpenFile();
void clearStdin(void);
char menu();

int main()
{

long lSize;
double **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 i,j,k; /* general purpose variable used for loop index */
char read;
char delims[] = ",";
char *result = NULL;


infile = fopen("bhp.csv","r");
if(infile == NULL)
{ 
printf("Error opening the file!!!\n\n");
return 0;
}

/*obtain file size.*/
fseek (infile,0, SEEK_END);
lSize = ftell (infile);
rewind (infile);

/****************************************
* *
* ALLOCATING MEMORY TO A 2D ARRAY *
* *
* *
* *
****************************************/

buffer = (int **)malloc(max_r * sizeof(int));
for(i=0; i<max_r; ++i)
buffer[i] = (int *)malloc(max_c * sizeof(int));
if (buffer == NULL) 
{
printf("not enough memory!!!");
return 0;
}


/**********/

/* copy the file into the buffer.*/
/* Read the file*/
for(i=0;i<max_r;++i)
for(j=0;j<max_c;++j)
fgets(buffer,max_r,infile);
puts(buffer);
/* spliting the columns*/
result = strtok( buffer, delims );
while( result != NULL ) {
printf( "result is \"%s\"\n", result );
result = strtok( NULL, delims );
} 

/*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'){ /*g option is selected*/
printf("\nDate (dd/mm/yyyy):");
scanf("%d/%s/%d",&day,&month,&year); 
printf("\n\nthe date choosed is %d/%s/%d",day,month,year); 
}
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 (infile);

/* now for each pointer, free its array of ints */
for (i = 0; i < max_c; i++) {
free(buffer[i]);
}
/* now free the array of pointers */
free(buffer[i]);

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;
}

Last edited by beginnerCCC; Sep 30th, 2006 at 12:04 AM. Reason: wrong code!!!!sorry
beginnerCCC is offline   Reply With Quote