![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#11 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#12 |
|
Newbie
Join Date: Sep 2006
Posts: 29
Rep Power: 0
![]() |
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 |
|
|
|
|
|
#13 |
|
Caffeinated Neural Net
![]() Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,034
Rep Power: 5
![]() |
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 |
|
|
|
|
|
#14 |
|
Newbie
Join Date: Sep 2006
Posts: 29
Rep Power: 0
![]() |
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 |
|
|
|
|
|
#15 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#16 |
|
Newbie
Join Date: Sep 2006
Posts: 29
Rep Power: 0
![]() |
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. |
|
|
|
|
|
#17 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#18 |
|
Newbie
Join Date: Sep 2006
Posts: 29
Rep Power: 0
![]() |
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??? |
|
|
|
|
|
#19 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#20 |
|
Newbie
Join Date: Oct 2006
Posts: 16
Rep Power: 0
![]() |
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); |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
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 |