4 Replies - 602 Views - Last Post: 05 May 2013 - 08:06 PM Rate Topic: -----

#1 ShankarLall  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 15-April 13

Read, Write and Update C txt files.

Posted 05 May 2013 - 07:07 PM

How do I ensure that I don't have Duplicated ID.
Have no idea how to compare new ID with IDs in file.


void addNewEmployee(FILE *fPtr)
{
    struct employeeStr data = {0, "", "", 0, "", 0};
    int id, i;


    printf("Enter Employee ID (1-1000): ");
    scanf("%d", &id);


    if (data.empID == id)
    {
//validate ID
        printf("\nThis is a Duplicated Employee ID\n");
    }
    else
    {
        data.empID = id;
        printf("Enter Employee First Name: ");
        scanf("%s", &data.fName);
        printf("Enter Employee Last Name: ");
        scanf("%s", &data.lName);
        printf("Enter Date of Birth (YYYYMMDD): ");
        scanf("%d", &data.dob);
        printf("Enter Employee Gender (M/F): ");
        scanf("%s", &data.sex);
        printf("Enter Employee Start Date (YYYYMMDD): ");
        scanf("%d", &data.hireDate);


        fprintf(fPtr, "%d %s %s %d %s %d", data.empID, data.fName, data.lName, data.dob, data.sex, data.hireDate);

    }
}




Is This A Good Question/Topic? 0
  • +

Replies To: Read, Write and Update C txt files.

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3569
  • View blog
  • Posts: 11,089
  • Joined: 05-May 12

Re: Read, Write and Update C txt files.

Posted 05 May 2013 - 07:40 PM

I feel that the point of your assignment is for you to either scan through your file and check each record for a duplicate id, or alternately load the contents of your file into memory and scan through that.

In a real production system, though, you will typically guarantee that there are no duplicate ID's by having your code assign the ID's instead of letting the user enter an ID for a record. It could be a monotonically increasing number, or a generated GUID, or hash of the data record and the current time. Heck, if you know that a user cannot enter more than one record a second, you could simply use the results of the time() function to assign your ID's.
Was This Post Helpful? 0
  • +
  • -

#3 ShankarLall  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 15-April 13

Re: Read, Write and Update C txt files.

Posted 05 May 2013 - 07:45 PM

View PostSkydiver, on 05 May 2013 - 07:40 PM, said:

I feel that the point of your assignment is for you to either scan through your file and check each record for a duplicate id, or alternately load the contents of your file into memory and scan through that.

In a real production system, though, you will typically guarantee that there are no duplicate ID's by having your code assign the ID's instead of letting the user enter an ID for a record. It could be a monotonically increasing number, or a generated GUID, or hash of the data record and the current time. Heck, if you know that a user cannot enter more than one record a second, you could simply use the results of the time() function to assign your ID's.


How do I scan through the file. I know it have something to do with rewind and also putting to cursor at the end of the file to write a new record or something like that. I just dont know how to use the code. I at just starting programming.
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3569
  • View blog
  • Posts: 11,089
  • Joined: 05-May 12

Re: Read, Write and Update C txt files.

Posted 05 May 2013 - 08:03 PM

So read up and experiment with fseek() and rewind(). It is usually best to start up a different program where you can do your experiments and learning instead of trying to mash it into your current project, but some people learn better when their working project suddenly stops working.

Additionally, I was under the impression that there was a C tutorial here on DIC that deals with C file I/O. That would probably help you as well. Of course, I don't know why you would need to resort to a tutorial here or be reading the documentation I linked to, when almost any decent C book would cover file I/O.
Was This Post Helpful? 0
  • +
  • -

#5 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,788
  • Joined: 20-September 08

Re: Read, Write and Update C txt files.

Posted 05 May 2013 - 08:06 PM

View PostShankarLall, on 05 May 2013 - 10:45 PM, said:

View PostSkydiver, on 05 May 2013 - 07:40 PM, said:

I feel that the point of your assignment is for you to either scan through your file and check each record for a duplicate id, or alternately load the contents of your file into memory and scan through that.

In a real production system, though, you will typically guarantee that there are no duplicate ID's by having your code assign the ID's instead of letting the user enter an ID for a record. It could be a monotonically increasing number, or a generated GUID, or hash of the data record and the current time. Heck, if you know that a user cannot enter more than one record a second, you could simply use the results of the time() function to assign your ID's.


How do I scan through the file. I know it have something to do with rewind and also putting to cursor at the end of the file to write a new record or something like that. I just dont know how to use the code. I at just starting programming.


It could be a good idea to load all your data records (struct's) into an array of struct

Then ... sort that array ... say in increasing 'ID' order ... (could use C's qsort)

Then traverse the array, from the front ... to see if any id's following a value are the same
(i.e. are there any duplicates? ... not sure if this is what you wish to test?)

Or ... do a traverse of the array to 'find' an existing id ... if this is what you want?
If you find it before it finishes ... i.e. if id found in array ... then that id already exists ...

If you exit the traverse without finding the id ... then that id does NOT already exist

You may like to look here for some examples of 'find' and 'obtaining unique id's in an array) or Cvec or Clist


Six Fast Steps to Programming in C

Delete duplicates example

This post has been edited by David W: 05 May 2013 - 08:09 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1