Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Help with newb's project

Reply
Thread Tools

Help with newb's project

 
 
Al Bowers
Guest
Posts: n/a
 
      10-25-2004


The_Kingpin wrote:

> Hi again all,
>


>
> Anyway I started making my main function which reads every struct objects
> of a given file. To test it, I printed the results in the command window
> of the project. Here's what it gives so far. Just want some feedback on
> the way my function reads one line of the file and puts it in a new
> struct's field: (I didnt found the rules as far as posting code, if anyone
> can point it out if its not correct)


The code appears ok but I have a few suggestions.

>
> struct book
> {
> char title[80];
> char autor[40];
> char editor[20];
> char ISBN[10];

Made this bigger.
char ISBN[20];
> char subject[20];
> int release;
> };
> typedef struct book bookInfo;
>


I would define another struct that has as members, one, a pointer
to the array of books, and, two, a member in which you keep a count
of the number of elements. Example:

typedef struct BOOKSET
{
bookInfo *book;
size_t num;
} BOOKSET;

Instead of putting all the code in function main, write seperate
functions that will manipulate the struct object. For example,
functions AddBook, PrintBookSet, FreeBookSet, etc.

> int main()
> {
> char c[50]; /* Store read lines */


Make this bigger.
char c[200];

> FILE *file; /* File pointer */
> bookInfo *books; /* array of book */
> int num_books = 0; /* keep count of number read in */
> int i, j;
>
> file = fopen("books.txt", "r");
> if(file==NULL) {
> printf("Error: can't open file.\n");
> return 1;
> }
> else {
> while(fgets(c, 50, file)!=NULL) {
> /* keep looping until NULL pointer */
> /* allocate memory dynamically for our books array */
>
> if(num_books==0) {
> books = calloc(1, sizeof(bookInfo));
> }
> else {
> books = realloc(books, (num_books+1)*sizeof(bookInfo));
> }
>


The if-else is not needed. Function realloc can handle all
allocations. See the code below for an example.

>
> //Title Field
> for(i=0, j=0 ; c[i]!='\t' ; i++, j++) {
> /* Loops until a tab character is reached */
> books[num_books].title[j] = c[i];
> }
> /* Terminate string by adding a null character */
> books[num_books].title[j] = '\0';
>
>
> // Autor Field
> for(i++, j=0 ; c[i]!='\n'; i++, j++) {
> /* Loops until a tab character is reached
> books[num_books].autor[j] = c[i];
> }
> /* Terminate string by adding a null character *//
> books[num_books].autor[j] = '\0';
>
> // other struct's fields...


You might consider using sscanf to parse the line in the file.

> }
>
> fclose(file); /* close file */
>


Here is an example that uses the colon( as the delimiter.

#include <stdio.h>
#include <stdlib.h>

struct BOOK
{
char title[80];
char autor[40];
char editor[20];
char ISBN[20];
char subject[20];
int release;
};

typedef struct BOOKSET
{
struct BOOK *book;
size_t num;
} BOOKSET;
/* Prototypes */
int AddBook(BOOKSET *p, char *s);
void PrintBook(struct BOOK *p);
void PrintBookSet(BOOKSET *p);
void FreeBOOKSET(BOOKSET *p);

int main(void)
{
char c1[] = "The New American Bible:IMPRIMATURE James Hickey:"
"World Publishing:052906484-7:Religion:5";
char c2[] = "Mastering Algorithms With C:Kyle Loudon:O'Reilly:"
"1-5652-453-3rogamming:2";
BOOKSET mylibrary = {NULL}; /* Empty bookshelf */

AddBook(&mylibrary,c1);
AddBook(&mylibrary,c2);
PrintBookSet(&mylibrary);
FreeBOOKSET(&mylibrary);
return 0;
}

int AddBook(BOOKSET *p, char *s)
{
struct BOOK *tmp;

if((tmp = realloc(p->book,(sizeof *p->book)*(p->num+1))) == NULL)
return 0;
p->book = tmp;
if(6 != sscanf(s,"%[^:]:%[^:]:%[^:]:%[^:]:%[^:]:%d",
p->book[p->num].title,p->book[p->num].autor,
p->book[p->num].editor,p->book[p->num].ISBN,
p->book[p->num].subject,&p->book[p->num].release))
return 0;
p->num++;
return 1;
}

void PrintBook(struct BOOK *p)
{
printf("Title: %s\n"
"Author: %s\n"
"Editor: %s\n"
"Subj: %s\n"
"ISBN: %s\n"
"Release: %d\n\n",p->title,p->autor,p->editor,
p->subject,p->ISBN,p->release);
return;
}

void PrintBookSet(BOOKSET *p)
{
size_t i;

for(i = 0; i < p->num;i++) PrintBook(&p->book[i]);
return;
}

void FreeBOOKSET(BOOKSET *p)
{
free(p->book);
p->book = NULL;
p->num = 0;
}


--
Al Bowers
Tampa, Fl USA
mailto: http://www.velocityreviews.com/forums/(E-Mail Removed) (remove the x to send email)
http://www.geocities.com/abowers822/

 
Reply With Quote
 
 
 
 
Joe Wright
Guest
Posts: n/a
 
      10-27-2004
Malcolm wrote:

> "Joe Wright" <(E-Mail Removed)> wrote
>
>>>#include <stdio.h>
>>>int main(void) {return EOF == puts("Done");}
>>>
>>>It has the minor bug of undefined behaviour on an i/o error.
>>>

>>
>>`info libc` says puts() returns EOF on error. Your program's return
>>value is well defined it seems to me.
>>

>
> However a return value of "true" from main() is not defined.
>
>>Cheapskate. I bid $105 an hour.
>>

>
> You can get advice from some highly qualified people for absolutely free on
> comp.lang.c, and certainly if a company decided to hire me out as a guru
> then a hundred dollars (or about seventy quid) an hour wouldn't be too
> unreasonable a figure.
>
> The problem with "help me with my homework" requests is that you want the OP
> to put in some effort, but simply writing some code not normally a good way
> to approach a problem. He needs to break down the task into sections, and
> plan the program. So the best time to get advice is at the beginning, before
> you have written a single line.
>
> Advice isn't the same thing as a homework doing service. Obviously most
> homework problems are so simple that an experienced C programmer could just
> sit down and write the thing straight off, but this isn't helpful. However
> just saying "show me some code" isn't too helpful either.
>
>


That is a very nice, thoughtful and serious response to my note to
Chuck. I was being a little facetious. I'm sure most of us agree
with your sentiments above. I do.

Often, the poster would offer money for one or any of us to do the
homework. Although we delight in working for money, our clients are
not usually programming students asking for help cheating on their
exams. The 'student' poster might attract a bid from someone, "I'll
do it for $100" for example. The poster sees the 'bite' and assumes
others will under-bid the first one.

We don't. We bid it up. Each new bid is higher than the previous
one. None of us actually accept the offer (do we?) but simply
delight in the poster's confusion as bids go up beyond his means and
he finally realizes his foolishness.

--
Joe Wright (E-Mail Removed)
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
 
Reply With Quote
 
 
 
Reply

Thread Tools

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
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Error while trying to run project: Unable to start debugging on the web server. The project is not configured to be debugged. windows 2003 server Claude seraphin ASP .Net 12 02-15-2014 04:29 PM
need ur help for my Masters project(TASM project) Mohanajeeva.D C Programming 2 07-02-2010 09:25 PM
Add C# web project to VB Web project Brad ASP .Net 2 04-01-2004 02:05 PM
HELP! HELP! HELP! Opening Web Application Project Error =?Utf-8?B?dHJlbGxvdzQyMg==?= ASP .Net 0 02-20-2004 05:16 PM
Error while trying to run project: Unable to start debugging on the web server. The project is not configured Ken Stealth ASP .Net 2 01-31-2004 05:46 PM



Advertisments