Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 205
  • Last Modified:

simple database program help

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
defisnms
Asked:
defisnms
  • 4
  • 2
  • 2
  • +2
2 Solutions
 
defisnmsAuthor Commented:
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
 
gj62Commented:
Sure.

Check out

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

You guys are in the same class... LOL
0
 
defisnmsAuthor Commented:
ROFL...I think I know him too!!!
Can you explain a little more on how to add each entry into memory or struct?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
gj62Commented:
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
 
posternbCommented:
You can fread/fwrite entire structures at once, i.e:

fwrite(&myStruct, sizeof(my_rec_t), 1, myFile);
0
 
gj62Commented:
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
 
gj62Commented:
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
 
Mayank SAssociate Director - Product EngineeringCommented:
>> 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
 
jmcgOwnerCommented:
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
 
Mayank SAssociate Director - Product EngineeringCommented:
Please proceed with that recommendation.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 4
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now