?
Solved

simple database program help

Posted on 2003-03-17
11
Medium Priority
?
204 Views
Last Modified: 2010-04-15
Hey all you C experts...this newbie needs major help on his program lol
anyways

rec.c:

int main(){

int size = 0;
int choice;
list arr[100];
do{
     printf("Options:\n");
     printf("1: Initialize from a file\n");
     printf("2: Add a record\n");
     printf("3: Display records\n");
     printf("4: Remove a record\n");
     printf("5: Change a record\n");
     printf("6: Write to a file\n");
     printf("0: Quit\n");
     scanf("%d",&choice);


     switch(choice){
          case 1:
               size= read_file(arr);    
               break;
          case 2:
               size = rec_add(arr,size);
               break;
          case 3:
               display(arr,size);
               break;
          case 4:
               size = remove_rec(arr,size);
               break;
          case 5:
               modify_rec(arr,size);

               break;
          case 6:
               write_file(arr,size);
               break;
          }

} while(choice!=0);

rec_func.c:
int read_file(char returnvalue[];)
{
    FILE *fp;
     char filename[100];
     char temp;
     int returnfunc;
     printf("Enter filename:\n");
     scanf("%s",filename);
     scanf("%c",&temp);
    if ( (fp=fopen(filename, "rb")) == NULL) {
       printf("File does not exists.\n");
       return 0;
    }
     returnfunc = fread(returnvalue , sizeof (returnvalue) , 1 , fp);
      if (ferror(fp)) {
       printf("An error occurred while reading file.\n");
      return 0;
      }
        fclose(fp);


     return returnfunc;
}

right now i get a returnvalue has a forward delcaration compile error!.....
also...case 1, which is the read_file function i'm working on,

This function will read records from a file and initialize the
array. Ask the user for the name of the file. If the file doesn't exist,
give an error message and just return 0. If the file does exist, it will be
in binary format and use the fread function. The function returns the number
of records read in from the file. Any records currently in program will be
lost when initialize is called.

am i doing it right so far?

Thanks in advance!
0
Comment
Question by:defisnms
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
  • +2
11 Comments
 

Author Comment

by:defisnms
ID: 8156118
err...actually i figured out how lol....sorry
but i do have another question
can someone give me a little head start on the add a record function?

do i use fgets and save it to the corresponding struct...also how do i use it with arrays? thanks
0
 
LVL 6

Expert Comment

by:gj62
ID: 8156136
Sure.

Check out

http://www.experts-exchange.com/Programming/Programming_Languages/C/Q_20553572.html

You guys are in the same class... LOL
0
 

Author Comment

by:defisnms
ID: 8156383
ROFL...I think I know him too!!!
Can you explain a little more on how to add each entry into memory or struct?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 6

Expert Comment

by:gj62
ID: 8156430
Referring to the other post, you are writing the "contents of the structs" to disk, not the structs themselves - that's important to remember...

But starting with those functions to read and write from disk, you can expand that as follows:

typedef struct {
    char first[10];
    char last[20];
    char addr[40];
    int age;
    int id;
}my_rec_t;

void readData(my_rec_t myStruct)
{
 fread(myStruct.first, sizeof(my_rec_t->first, 1, myFile);
 fread(myStruct.last, sizeof(my_rec_t->last, 1, myFile);
 fread(myStruct.addr, sizeof(my_rec_t->addr, 1, myFile);
 fread(myStruct.age, sizeof(my_rec_t->age, 1, myFile);
 fread(myStruct.id, sizeof(my_rec_t->age, 1, myFile);
}

void writeData(my_rec_t myStruct)
{
 fwrite(myStruct.first, sizeof(my_rec_t->first, 1, myFile);
 fwrite(myStruct.last, sizeof(my_rec_t->last, 1, myFile);
 fwrite(myStruct.addr, sizeof(my_rec_t->addr, 1, myFile);
 fwrite(myStruct.age, sizeof(my_rec_t->age, 1, myFile);
 fwrite(myStruct.id, sizeof(my_rec_t->age, 1, myFile);
}

my_rec_t myArray[100]; - creates an array of 100 structs...

readData(&myArray[0]); - read one structure of data into the first structure in the array, put this in a loop to read all the data.

Let me know if you get stuck...
0
 

Expert Comment

by:posternb
ID: 8159361
You can fread/fwrite entire structures at once, i.e:

fwrite(&myStruct, sizeof(my_rec_t), 1, myFile);
0
 
LVL 6

Expert Comment

by:gj62
ID: 8159494
Posternb,

You can, but it is not considered a good idea.

Structures can be padded differently in C, so it is recommended that you write the structure field by field.

See the C FAQ:

http://www.eskimo.com/~scs/C-faq/q2.11.html

As this is homework, I figured they should do it the right way to begin with, and it isn't that much more work...
0
 
LVL 6

Accepted Solution

by:
gj62 earned 152 total points
ID: 8159506
Also, if they do what I recommended and store only char * in their struct and dynamically allocate the string space, your method won't work (it will write the pointer, not the string).  
0
 
LVL 30

Assisted Solution

by:Mayank S
Mayank S earned 148 total points
ID: 8164471
>> rec_func.c:
>> int read_file(char returnvalue[];)
>> {

Remove the semi-colon after returnvalue[]

Make it:

int read_file ( char returnvalue[] )

>> scanf("%c",&temp);

You are probably doing this to clear a character from the input buffer. This problem often happens while reading lots of strings/ characters together. Please replace this line with:

fflush ( stdin ) ;

to flush the input buffer.

You should generally use fread () to read if you have used fwrite () to write.

>> fread(returnvalue , sizeof (returnvalue) , 1 , fp);

sizeof ( returnvalue ) will always be 100 - so what if you have only 10 records in the file? Please modify it to something like:  

fread ( returnvalue, sizeof ( int ), number_of_elements, fp ) ;

That should correct your errors as far as you've shown your code.


Mayank.
0
 
LVL 20

Expert Comment

by:jmcg
ID: 10144543
Nothing has happened on this question in more than 9 months. It's time for cleanup!

My recommendation, which I will post in the Cleanup topic area, is to
split points between gj62 and mayankeagle.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jmcg
EE Cleanup Volunteer
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10144579
Please proceed with that recommendation.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question