Thread: how to delete data

  1. #1
    Registered User
    Join Date
    Oct 2009
    Location
    Indonesia
    Posts
    68

    how to delete data

    hi there.. I have some problem here...
    I have already struct the variabel...
    and when I input 3 times,, automaticly the files are 3 right?
    so, how can I delete one of them? can we set them into NULL?(I don't know how)
    example, if the files is
    1. asd
    2. qwe.
    3. rty

    I wanna delete the second one.. after that, it will automaticly print like this
    1. asd
    2. rty

    any ideas? thanks before

  2. #2
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Are you using arrays or linked lists? Are you familiar with copying strings and using malloc/free? Questions like this pose more questions by us. What have you coded so far?

  3. #3
    Registered User
    Join Date
    Oct 2009
    Location
    Indonesia
    Posts
    68
    hehe.. I edited it.. because it's my assignment... xD
    Last edited by Kinshara; 11-03-2009 at 12:57 AM.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    What i do is put a few end of string char's into the char array on the low end (0,1,2). Only array[0] should be needed, but I like to to sure.

    End of string char is '\0'.
    Last edited by Adak; 11-02-2009 at 07:06 PM.

  5. #5
    Registered User
    Join Date
    Oct 2009
    Location
    Indonesia
    Posts
    68
    you mean, adding '\0' on the end of char which I want to delete?

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Kinshara View Post
    you mean, adding '\0' on the end of char which I want to delete?
    No, I mean putting the end of string char into the first 3 char's of the name you want to delete:

    Say I have "Joseph" in a name I want to delete. I change it to: "\0\0\0eph".

    Now, as long as I'm using string handling, and not printing char by char, Joseph will not be printed:

    Code:
    if(names[i])  //if the name string exists
       printf("\n next name is: ", names[i]);
    Where names is the 2D char array names[20][20], for example.

  7. #7
    Registered User
    Join Date
    Oct 2009
    Location
    Indonesia
    Posts
    68
    Code:
    for(int c=0;c<jum_data;c++)// so I am searching the "nim" which must be same with the files
    	{
    	if(strcmp(nim,data_mahasiswa[c].nim)==0) 
    	{printf("NIM : %s\n",data_mahasiswa[c].nim);
    	printf("Nama : %s\n",data_mahasiswa[c].nama);
    	printf("Are you sure want to delete??(y/n)");
    	tombol=getchar();
    	if(tombol=='y')
    	{
    	data_mahasiswa[c].nim[0]=='\0';
    	data_mahasiswa[c].nim[1]=='\0';
    	data_mahasiswa[c].nim[2]=='\0';
    	}
    can I use that?
    I declare "nim" as char..
    but I won't work.. am I wrong?

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Kinshara View Post
    Code:
    for(int c=0;c<jum_data;c++)// so I am searching the "nim" which must be same with the files
    	{
    	if(strcmp(nim,data_mahasiswa[c].nim)==0)        //this is OK :)
    	{printf("NIM : %s\n",data_mahasiswa[c].nim);
    	printf("Nama : %s\n",data_mahasiswa[c].nama);
    	printf("Are you sure want to delete??(y/n)");
    	tombol=getchar();
    	if(tombol=='y')                                       //this is OK
    	{
    	data_mahasiswa[c].nim[0]=='\0';              //This is wrong, just one =
    	data_mahasiswa[c].nim[1]=='\0';              // this is wrong, just one =
    	data_mahasiswa[c].nim[2]=='\0';              //this is worng, just one =
    	}
    can I use that?
    I declare "nim" as char..
    but I won't work.. am I wrong?
    Not == (compared to), but = (assignment). Remove one = .


    Also, inside your curly braces, please indent your code 2 or three spaces:

    not:
    Code:
    {printf...
    printf...
    printf
    }
    but:
    Code:
    {  
       printf
       printf...
       printf...
    }
    Last edited by Adak; 11-03-2009 at 05:42 AM.

  9. #9
    Registered User
    Join Date
    Oct 2009
    Location
    Indonesia
    Posts
    68
    WAW!! you are right!!! hahaha..
    it works!
    but, how about int type?
    can we just use same? "\0"???

    can we delete it directly to their struct?
    Code:
    struct profile
    {
      char a[11];
      char b[12];
      int c;
    };
    struct profile work_profile[10];
    /* can we delete at once by deleting work_profile[3]? */
    Last edited by Kinshara; 11-03-2009 at 05:55 AM.

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Kinshara View Post
    WAW!! you are right!!! hahaha..
    it works!
    You don't have to act **SO** damn surprised! <LOL>

    but, how about int type?
    can we just use same? "\0"???


    No. '\0' is for char's. For int's, set them to zero.

    "Can we delete it directly to their struct?"

    You can delete a struct, but you don't want to, usually. You want to set the members of the struct, to their proper value, and then print up only the one's that have the values you are looking for.


    For instance, say "Ishmael" leaves the class, and you need to remove his record from your class program.

    You don't delete his struct. You recycle it.

    You set his name to '\0\0\0" and or set his school ID number, to 0. Now your program logic knows that the record struct for Ishmael, is unused, and can be given data for another student - and no struct was deleted, or needs to be created.

    You could print out your records using logic like this:

    Code:
    for(i = 0; i < NumberOfRecords; i++) {
      if(records[i].studentNum)
        printf("\n %d %s ", records[i].studentNum, records[i].name);
    }
    A database will keep several records like this, available for reuse. It saves a lot of computer time.
    Last edited by Adak; 11-03-2009 at 07:08 AM.

  11. #11
    Registered User
    Join Date
    Oct 2009
    Location
    Indonesia
    Posts
    68
    Hemm..
    I have got some problem when I set int variable into zero(0),,
    it has be printed when I run the program..
    Code:
    Name              Score
    ==============
                              0
    Ray                     65
    it become like that when I use that...
    fortunately,, I set the second array to be the first array(automaticly, the first array would be erased and change by the second now.. and the second is null.. hehehe..)

    really thanks for the helps.. now I want to focus learn array.. ^^

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    When you add the if statement I showed you above, you won't have empty records printing at all, and you won't have to move the records downward either.

    You're welcome.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. data structure design for data aggregation
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 05-20-2008, 06:43 AM
  2. Bitmasking Problem
    By mike_g in forum C++ Programming
    Replies: 13
    Last Post: 11-08-2007, 12:24 AM
  3. Binary Tree, couple questions
    By scoobasean in forum C Programming
    Replies: 3
    Last Post: 03-12-2005, 09:09 PM
  4. compare and delete data from file
    By moby in forum C Programming
    Replies: 9
    Last Post: 02-07-2005, 03:08 AM
  5. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM