Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

file handling in c

Options
  • 27-02-2002 4:16pm
    #1
    Closed Accounts Posts: 64 ✭✭


    hey
    i know at this stage u r bound to be fed up giving people site names etc but i REALLY need help with files! i dont understand them AT ALL! and time is running out!

    few questions!..

    - for every file do i need a seperate #define at the start?
    -do i need a seperate struct for each file?
    and finally
    - do i declare my structs b4 or after main?

    thanks in advance!


Comments

  • Registered Users Posts: 1,783 ✭✭✭Puck


    Now it's been a while since I did C and I never used it much anyway so I'm probably not the best person to be answering this but seen as nobody else has replied yet I thought I'd at least try to help. The experts can correct me on this later.

    As far as I know:

    - You do not need a seperate #define at the start for each file. In fact I don't think you really need any.

    - Same as above I don't think you actually need any.

    - Declare your structs before main.

    Now I know that wasn't very helpful but I don't really know what you are trying to do.

    This very simple piece of code will declare and open two files, one for reading from and the other for writing to:
    include "stdio.h"
    #include "string.h"
    void main()
    {
      FILE *ifp, *ofp;
      char inputfile[25], outputfile[25];
      char c;
    
      printf("Enter filename for input file -> ");
      scanf("%s", inputfile);
      ifp = fopen(inputfile, "r");
    
      printf("Enter filename for output file -> ");
      scanf("%s", outputfile);
      ofp = fopen(outputfile, "w");
    
    /* rest of code....*/
    
    


  • Registered Users Posts: 4,188 ✭✭✭pH


    - for every file do i need a seperate #define at the start?

    Nope!

    -do i need a seperate struct for each file?

    Nope!

    - do i declare my structs b4 or after main?

    What structs? Are you planning on using a struct to map a data record or something?

    pH


  • Closed Accounts Posts: 64 ✭✭wee_lady


    sorry...i guess i should have made myself a little clearer!

    the reason i need these files is because i'm creating a system to be used in a business...it's for my major project in college!

    i need to use several files in it i.e i need one to add new customers, one to store customer bookings and the like! but i've gotten a tad confused! this is where my problem lies!

    at this stage i have only attempted one file which is not working too well! it's letting me add a customer to my records and it's storing it but once i break out of my program the information is lost!

    the code i have at the start is as follows:


    /*define constant for the name and path of file*/
    #define FILENAME "X:\\project\\files\\customer.bin"



    /*Declare Structure Template*/
    typedef struct Cust_Record
    {
    int cust_no;
    char cust_name[60];
    char cust_street[15];
    char cust_town[15];
    char cust_county[15];
    char cust_tel[10];
    char cust_mob[15];
    }CUSTOMER;

    /*Declare array of 100 structure variables*/
    CUSTOMER cust_records[100];

    /*Declare variable that will store total records in the file & initialise it to 0*/
    int totalRecords=0;


    then this is the code i'm actually using for the file:

    //New_Customer
    void New_Customer()
    {
    char ans;
    scr_outline();
    do{
    gotoxy(30,4);
    printf("Add New Customer");
    gotoxy(20,10);
    printf("Customer Number: ");
    scanf("%d", &cust_records [totalRecords ].cust_no);
    gotoxy(20,11);
    fflush(stdin);
    printf("Customer Name: ");
    gets(cust_records [totalRecords ].cust_name);
    gotoxy(20,12);
    printf("Street: ");
    gets(cust_records [totalRecords ].cust_street);
    gotoxy(20,13);
    printf("Town: ");
    gets(cust_records [totalRecords ].cust_town);
    gotoxy(20,14);
    printf("County: ");
    gets(cust_records [totalRecords ].cust_county);
    gotoxy(20,15);
    printf("Tel: ");
    gets(cust_records [totalRecords ].cust_tel);
    gotoxy(20,16);
    printf("Mob: ");
    gets(cust_records [totalRecords ].cust_mob);
    totalRecords ++;
    gotoxy(20,18);

    printf("TotRec: %d", totalRecords);
    fflush (stdin);
    gotoxy(20,20);
    printf("Do you want to add another record: (Y/N)" );
    ans=getche();
    }while ((ans == 'Y') || (ans == 'y'));
    } //end New_Customer

    i'm quite confused about this at this stage! obviously i'm doing it all wrong! but i dont really know where i've gone wrong!

    also once i've gotten that sorted i need to be able to search through my customer file to find some1 i mite be looking for...i have the same problem here! if i add records to my file and i go to my search option and type in the name of the person i'm looking for it'll come up on my screen but the information is then lost when i break out....the code i'm using for that is...


    //Search_Results
    void Search_Results()
    {
    int i;
    char query_name[40];
    char ans;
    scr_outline();

    fflush(stdin);
    clrscr();
    scr_outline();
    gotoxy(30,4);
    printf("View Customer");
    gotoxy(20,10);
    printf("Enter customer name: ");
    gets(query_name);

    for (i=0; i<totalRecords; i++)
    {
    if (strcmp(query_name, cust_records .cust_name) ==0)
    {
    gotoxy(20,12);
    printf("\n<< Customer Record Found >>");
    gotoxy(20,14);
    printf("\n\n%s", cust_records .cust_name);
    gotoxy(20,15);
    printf("\n%s", cust_records . cust_street);
    gotoxy(20,16);
    printf("\n%s", cust_records . cust_town);
    gotoxy(20,17);
    printf("\n%s", cust_records . cust_county);
    gotoxy(20,18);
    printf("\n%d", cust_records .cust_no);
    gotoxy(20,20);
    printf("\n\nCorrect Customer Record (Y/N):");
    ans=getche();
    if (ans=='Y' || ans=='y')
    return;
    }
    }

    printf("\n\n<<No Customer Record Found>>");
    getch();
    } //end Search_Results



    i know my code mustn't be far off because it's working until i break out of my program! it's got to the stage where i'm ready to tear my hair out here!


  • Closed Accounts Posts: 285 ✭✭marauder


    Is this all your code? THe reason I ask is I don't see where you are writing out to the file..

    Your code creates 100 customer records in memory and as you use your program you start filling up these spaces. when you do a search you are searching memory for the customer, which it finds. Then when you exit the program the contents of the memory are erased.

    If I understand this correctly you need only one customer record.
    Then each time one is entered it should be written out to the file.
    When you do a search you should be reading back in the records one at a time from the file until you hit a match

    Use the code like Puck suggested...

    Hope this helps


  • Closed Accounts Posts: 64 ✭✭wee_lady


    hehe it's FAR from all my code i'll tell ya! its actually hitting around 1600 lines at present!

    will the code that puck suggested store all the records that i need and also let me search through them?

    also if a #define is not used here then where else would it be used? that's wot we were using in class to write the file to our own hard drives! oooo the confusion!!


  • Advertisement
  • Closed Accounts Posts: 285 ✭✭marauder


    You will have to modify the code to handle writing out structures.

    like
    fprintf(yourfile, " ......your format..... "... your variables....);

    and for reading back

    fscanf yourfile, " ......your format..... "... your variables....);

    you will also have to use lseek/set (?) to get to the end of the file and to get back to the start

    finally I believe the #define here is just being used to define
    FILENAME as a constant pointing to your file. This would replace the 'inputfile' in the fopen statement in Puck's code. You would do this so that if you want to change the file you were using you would only have to do it it this one place rather than in multiple places in your code....

    good luck


  • Registered Users Posts: 4,188 ✭✭✭pH


    Well, have you considered not using a raw file, but using a database instead?

    For something like you're doing a small database (even Access .DBFs) would make life so much more simple. Is this allowed under whatever rules the project must follow?

    Storing, sorting, looking up, amending and deleting the records is going to be a fair amount of work. As you will have 3 files (with different file layouts) you will probably have to write generic routines. As I said this all seems like a lot of work (unless the point is to learn to write these routines that is!)

    Continuing down the path you're on, and loading the files into arrays, manipulating them there and then saving them back to disk at the end is one option, however linked lists may be a better method than arrays.

    Also from looking at the code is that for DOS?

    pH


  • Registered Users Posts: 2,660 ✭✭✭Baz_


    The code you have so far seems to be capable of doing the job you want it to. I didnt go over it with a fine tooth comb, but there are no glaringly obvious errors.

    Now as marauder suggested the data is not being written to file anywhere, but the best thing to do would be to leave the code the way it is and write a new function to write to your file, which you can call before your program ends (before you exit main). You should read the contents of the file at the start of your program too.

    Because you are using structs you should look into random access files, and learning about the fread() and fwrite() functions will help you. I can only assume that since you are expected to use both structs and files, that the info will be somewhere within your notes.

    A short point. If you do as marauder said and save each record to disk as you go along, it will lead to much slower search times, whereas searching through the already loaded array of structs will be a lot faster.


Advertisement