Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Sep 29th, 2006, 9:05 PM   #11
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
In that case, ignore the criticism. If you'd care to present some of your code, to indicate that you're giving it a good shot, I'll be happy to help.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Sep 29th, 2006, 10:03 PM   #12
beginnerCCC
Newbie
 
Join Date: Sep 2006
Posts: 29
Rep Power: 0 beginnerCCC is on a distinguished road
thanks.
this is the code i am working with:
<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,sep=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;
}


</code>
thanks for ur help
beginnerCCC is offline   Reply With Quote
Old Sep 29th, 2006, 10:43 PM   #13
lectricpharaoh
Caffeinated Neural Net
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,034
Rep Power: 5 lectricpharaoh will become famous soon enough
Argh! Code tags! I know you tried, but they use brackets, not less-than/greaten than signs to enclose the tags. Here's a hint: select the text you want to put in code tags, then click the button that looks like a pound sign (#).

You can also hit the 'Preview Post' button before 'Submit Reply'; this will let you see if you got it right.
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot.
- Vaarsuvius, Order of the Stick
lectricpharaoh is offline   Reply With Quote
Old Sep 29th, 2006, 11:03 PM   #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 29th, 2006 at 11:04 PM. Reason: wrong code!!!!sorry
beginnerCCC is offline   Reply With Quote
Old Sep 30th, 2006, 4:03 AM   #15
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
I suggest you're taking the wrong approach to reading the file. The field separator is a comma, so that's what you'll tokenize on, okay. The record separator is a newline, so if you read line by line, each read will return one record. You then tokenize that. You'll need a little extra in case quoted fields contain an interior comma.

In line with Narue's suggestion, if you're just searching for an item, there's no need to put the entire file into an array. Read a record, tokenize it, search it, if the searched item isn't found, read another. If it's found, you're done.

If you want further information, you're going to have to make the file readable (formatted with indentation, primarily). If your file is indented, but you added the code tags from the unindented stuff you first posted, the indentation went away. I suggest that you indent the file and just zip it up and attach it to a post as a link. Then, those of us who might be inclined to compile it, correct it, add notations, and so forth, won't have to break out our beautifiers just to get started.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Sep 30th, 2006, 5:34 AM   #16
beginnerCCC
Newbie
 
Join Date: Sep 2006
Posts: 29
Rep Power: 0 beginnerCCC is on a distinguished road
ok.
i am really confused now.
i cant get anything done.
i will attach the design specification to this reply.
so if you can have a look at it and see if i am on right track.
if you can do this i ll be thankfull to u.
Attached Files
File Type: txt qweqw.txt (6.7 KB, 11 views)
beginnerCCC is offline   Reply With Quote
Old Sep 30th, 2006, 8:42 AM   #17
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Well, it's nice to know the parameters of the problem. Because the user may make multiple queries, on various combinations, I probably would pull in the entire file. That doesn't change anything I said about reading the information. The file structure is still the same. If you're confused about something, you need to say what about, and why. Let me suggest that you read the sticky post, "How to post a question." It's right at the top of this very forum. Then post back specifics, and include a .zip of your code. Incidentally, the "file size" that you need to determine is not the size of the file, but the number of records in the file. If you determine that in advance (as you've tried, but you have the wrong number), then allocating memory for your array is like falling off a log.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Sep 30th, 2006, 9:06 PM   #18
beginnerCCC
Newbie
 
Join Date: Sep 2006
Posts: 29
Rep Power: 0 beginnerCCC is on a distinguished road
ok,
i will attach my code to this reply.
to be honest with you, i donno what i am doing at the moment, and the due date is in 2 days. i am getting really stresset now. sooo confused,
do you have any advice???
Attached Files
File Type: zip last1.zip (2.1 KB, 4 views)
beginnerCCC is offline   Reply With Quote
Old Sep 30th, 2006, 9:39 PM   #19
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
You need to be quite clear on two things: each line in your file is one record and each record contains fields which are delimited with a comma. Your record has been specified to have 7 fields. I suggest working with that limited view, instead of a more generalized .csv view, simply to ease the allocation of dynamic memory. If we know how many lines are in the file, we know how many 'rows' in a 2-D array we will need. We know the number of fields, so we know how long each row has to be. If we determine the number of rows in advance, we will know precisely how much memory to allocate. That is because the array will not be holding the field contents, but pointers to the field contents, and those pointers are all the same size. Furthermore, by getting all the memory at once, we have a fully contiguous array. We won't need a separate array to hold row pointers, because we can easily calculate them (desired row * number of fields, added to the base address of the array).

Here's the procedure. Read the entire file, a line at a time (say, fgets), ignoring the data and just counting lines. Rewind the file. Ask malloc for number of lines * number of fields * size of a char pointer. Read the first line. Tokenize it with strtok, on commas (I'd toss in the double quote, too, just to get rid of it, since there are no commas internal to the quoted field, per your specification). At this point you could malloc for memory for the string and strcpy the field into it. If you call strdup, though, it will do those things for you, and hand you the pointer to it. Take this pointer and put it in Array [0][0]. Get the next field and put it in Array [0][1]. Each time you get a token, you increment the field counter and use it as 'c' in Array [r][c]. When the line is finished, you set 'c' back to zero and increment 'r'. There's actually no need to do that in this case if the file isn't corrupt. You can treat the entire file as a single list of tokens. Whatever. Once that's done, you're ready to do your searching and reporting. This approach solves your problem. Making a generic .csv parser would not be wise until you've conquered your confusion.

Try to accomplish this first (you have the nucleus of this in your current code). When that works, and you've determined that you can find certain fields and print them out, then you can proceed to the rest of the app, the searching and reporting.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Oct 1st, 2006, 12:57 AM   #20
climbnorth
Newbie
 
Join Date: Oct 2006
Posts: 16
Rep Power: 0 climbnorth is on a distinguished road
Here is some example code for allocating memory for your records and then deleting it:

int iter = 0;
records *ptr;  /* For the array of records */
ptr = (record*)calloc(numRecords, sizeof(record));

for(i = 0; i < numRecords; i++)
{
  ptr[i] = (record)calloc(1, sizeof(record));
  /* if you need to allocate mem for record fields, do it here */
  /* copy record data here */
}

for(i = 0; i < numRecords; i++)
{
  /* if you allocated mem for record fields, delete them here */
  free(ptr[i]);
}
free(ptr);
climbnorth is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
OnlineTextEditor.Com! Sane Show Off Your Open Source Projects 43 Jun 16th, 2006 8:55 AM
How to read unknown total of int data from text file to a 2-dim array in C ladyscarlet99 C 2 Sep 9th, 2005 2:28 AM
Installing IPB 2.03 bh4575 Other Web Development Languages 0 Apr 23rd, 2005 2:36 AM
After execution - Error cannot locate /Skin File? wchar Visual Basic 1 Mar 5th, 2005 9:04 PM
airport Log program using 3D linked List : problem reading from file gemini_shooter C++ 0 Mar 2nd, 2005 4:12 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 11:35 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC