Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   HOW to use "\r" properly? (http://www.go4expert.com/forums/r-properly-t28606/)

jose_peeterson 20Jun2012 22:04

HOW to use "\r" properly?
 
Hello everyone,

I have a problem!. I am writing a code for a library program. i am at the stage of adding names and joined dates of members. The names are stored in an array and written to a file called members.txt. I would like to delete a member in the middle of the text file. After deletion i will load this new list of names back into the structure. Now the problem is

When i delete a name in the MIDDLE. I dont want to leave empty spaces so i tried to use the "\r" to use the name below to overwrite the name i want to delete. It does not seem right to me. because when i print from the file the tab space does not work on the name directly below the deleted name. The relevant code is as follows :


this is the delete function.
Code:

void delete_from_memfile(char members_name[50],struct members member[1000])
{                                          // members_name = MEMBERS NAME ONLY.         
 char members_det[58];                      // this string reads a line from file (here DUMMY)
 int total_members,ctr,i=1;                // ctr is the line number in file.
 char members_del[58];                      // holds name and joined_date together from struct
 
 total_members = load_members(member);                    // load the structure from file
 
 FILE *members;
 
 members = fopen("members.txt","w");                      // write to file the DELETED member
 
 if(members == NULL)
  {
  printf("\n\tWARNING : UNABLE to open members.txt\n");
  return;
  }
 
  for(ctr=0;ctr<total_members;ctr++)
  {
    members_del[0] = '\0';                          // clears the string
                                                                       
    if(!(strcmp(members_name,(member[ctr].name))))  // use ctr as line number to write
    {
                                                    // ctr as member to be deleted.
 /*\r*/ strcpy((member[ctr].name),"");
        strcpy((member[ctr].joined_date),"");           
        fprintf(members,"%s","\r");
        continue;
                                                    /* test printf("\n%s%s\n",(member[ctr].name),(member[ctr].joined_date));*/   
    }
                                                    /* test else printf("\n%s%s\n",(member[ctr].name),members_name); */
                                           
      strcat(members_del,(member[ctr].name));        // here do the string concatanate for ALL strings.
      strcat(members_del," ");
      strcat(members_del,(member[ctr].joined_date));
     
      fprintf(members,"%s",members_del);            // write line by line(ctr as line number) to file
 
                                                    /* test printf("%s\n",members_del); */
      members_del[0] = '\0';                        // clears the string
  }
                                                    /* test printf("%s\n",members_del); */
 fclose(members); 
 
total_members = load_members(member);                // load the structure from file
return;
}

the load function loads the updated text file to the structure.

Show function is to print from the text file.

Code:


void show(void)
{
 char members_det[58];                                      // a line from file             
 FILE *members;             
 
 members = fopen("members.txt","r");
 
 if(members == NULL)
  {
  printf("\n\tWARNING : UNABLE to open members.txt\n");
  return;
  }
 
    printf("\n\n\n\t\tNAME  JOINED-DATE\n");
   
  while(fgets(members_det,58,members))            // load a line into members_det string
  { 
    printf("\t\t%s",members_det);
 
  }
    printf("\n\n");

fclose(members);
return;
}


The entire code is shown below


Code:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

struct borrowed
{
 char member_name[50];                // to access from borrowed.txt file       
 char items_name[150];
 char type;
 char due_date[8];             
};

struct members
{
 char name[50];
 char joined_date[8];
 struct borrowed borrowed_items[5];
};

                                                                                                                       
int security(char username[10],char password[10]);  // fucntion prototypes
void store_in_memfile(char members_init[58]);      // stores name and joined date to file
void delete_from_memfile(char members_name[50],struct members member[1000]);    // deletes the member
int update_total_members(void);
int load_members(struct members member[1000]);    // ONLY loads NAME and JOINED DATE to structure members, RETURNS total number of members
void show(void);

                // START of MAIN
int main()
{
  char username[10],password[10];
  int op=1;
 
  char members_init[58],members_det[58];            // name and joined_date                                                                     
  char members_name[50];                            // name to DELETE
  int total_members;                                // takes the value of line counter
  struct members member[1000];
 
  printf("\n\t\tCalvary Life Assembly Digital Library\n\n\n");
 
  while(1)                                      // security while loop
  {
    printf("Please enter your USERNAME and PASSWORD\n");
    strcpy(username,"x");                      // clear the u_n and p_w   
    strcpy(password,"x");
    scanf("%s%s",username,password);
    if(security(username,password))
    {
    continue;
    }
    else
    {
      printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");  // hide the p_w
      printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
      printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
      printf("\nSECURITY CLEARED!\n\n");
      break;
    }
  }                                            // security while loop
 
 
  printf("\n\t\tCalvary Life Assembly Digital Library\n\n\n\n");
 
  while(1)
  {
    fflush(stdin);                   
    printf("\n\tPress    1  - ADD New MEMBER\n\n");
    printf("\tPress    2  - DELETE a MEMBER\n\n");
    printf("\tPress    3  - VIEW All members\n\n");
    printf("\tPress ctrl c - EXIT\n\n\n");
    printf("You Entered --> : ");
    scanf("%d",&op);   
   
    if(op == 1)
      {                                      // read from keyboard
        printf("\nEnter FULL NAME with _ for spaces AND(after a space) Today's date <dd/mm//yy>.\n");
        fflush(stdin);
        fgets(members_init,58,stdin);         
        fflush(stdin);
                                                             
        store_in_memfile(members_init);      // append to members.txt file 
                                                     
      }
     
    if(op == 2)
      {     
        printf("\n\tWARNING : You are about to DELETE a member.\n");
        printf("\nEnter the FULL NAME with _ for spaces between words.\n");
        fflush(stdin);
        scanf("%s",members_name);
     
        delete_from_memfile(members_name,member);    // delete a member, sending MEMBER NAME ONLY
       
      }
     
    if(op == 3)
      {
      show();
     
      }
    if(op == 4)
      {
     
      }
     
    total_members = update_total_members();      // update total_membters after store or delete members,
                                                  //  printf("\n\ntotal members = %d",total_members);
    load_members(member);                        // load members NAME and JOINED DATE from file into structure
    /* test*/
     
   
 
  }


system("pause > null");
return 0;
}                                                  // END OF MAIN         


int security(char username[10],char password[10]) // 0 is correct and 1 is wrong
{
  if(!(strcmp(username,"1")) && !(strcmp(password,"1")))
  return 0;
  else
  return 1;
}

void store_in_memfile(char members_init[58])
{
 FILE *members;
 
 members = fopen("members.txt","a");
 
 if(members == NULL)
  {
  printf("\n\tWARNING : UNABLE to open members.txt\n");
  return;
  }
 
 if(members_init[0] != '\n')            // capture the ENTER KEY and prevent it written to file.                                                           
  {
  fprintf(members,"%s",members_init);        // add the new member             
  }
 
 fclose(members);                         
return;                           
}

int update_total_members(void)
{
 FILE *members;
 int line_no=0;
 char members_det[58];
 
 members = fopen("members.txt","r");
 
 if(members == NULL)
  {
  printf("\n\tWARNING : UNABLE to open members.txt\n");
  return 0;
  }

 while(fgets(members_det,58,members))      // update total_members
  {
  line_no = line_no + 1; 
  } 
                                                           
 fclose(members);
                         
return line_no;                            // update total_members
}

void delete_from_memfile(char members_name[50],struct members member[1000])
{                                          // members_name = MEMBERS NAME ONLY.         
 char members_det[58];                      // this string reads a line from file (here DUMMY)
 int total_members,ctr,i=1;                // ctr is the line number in file.
 char members_del[58];                      // holds name and joined_date together from struct
 
 total_members = load_members(member);                    // load the structure from file
 
 FILE *members;
 
 members = fopen("members.txt","w");                      // write to file the DELETED member
 
 if(members == NULL)
  {
  printf("\n\tWARNING : UNABLE to open members.txt\n");
  return;
  }
 
  for(ctr=0;ctr<total_members;ctr++)
  {
    members_del[0] = '\0';                          // clears the string
                                                                       
    if(!(strcmp(members_name,(member[ctr].name))))  // use ctr as line number to write
    {
                                                    // ctr as member to be deleted.
 /*\r*/ strcpy((member[ctr].name),"");
        strcpy((member[ctr].joined_date),"");           
        fprintf(members,"%s","\r");
        continue;
                                                    /* test printf("\n%s%s\n",(member[ctr].name),(member[ctr].joined_date));*/   
    }
                                                    /* test else printf("\n%s%s\n",(member[ctr].name),members_name); */
                                           
      strcat(members_del,(member[ctr].name));        // here do the string concatanate for ALL strings.
      strcat(members_del," ");
      strcat(members_del,(member[ctr].joined_date));
     
      fprintf(members,"%s",members_del);            // write line by line(ctr as line number) to file
 
                                                    /* test printf("%s\n",members_del); */
      members_del[0] = '\0';                        // clears the string
  }
                                                    /* test printf("%s\n",members_del); */
 fclose(members); 
 
total_members = load_members(member);                // load the structure from file
return;
}


int load_members(struct members member[1000])              // loads ONLY NAME and JOINED DATE to structure
{
 FILE *members;
 char members_det[58];                                      // a line from file
 char *temp_name,*temp_joined_date;                        // strings to hold PARSE tokens
 int i=0;                                                  // struct counter
 
 members = fopen("members.txt","r");
 
 if(members == NULL)
  {
  printf("\n\tWARNING : UNABLE to open members.txt\n");
  return 0;
  }
 
 while(fgets(members_det,58,members))      // load a line into members_det string
  {
    temp_name = strtok(members_det," ");    // parse the string members_det into tokens, first token is from 1st char to " ".
    temp_joined_date = strtok(NULL,"");    // second token(string) is from char after " " to end of word/line.
   
    if(temp_joined_date == NULL)          // precaution against blank lines in file.
      continue;
   
    strcpy((member[i].name),temp_name);
    strcpy((member[i].joined_date),temp_joined_date);
    i++;
  } 
                                            // printf("\n%s  %s\n",(member[9].name),(member[9].joined_date));
                                                               
 fclose(members);
 
return i;
}

void show(void)
{
 char members_det[58];                                      // a line from file             
 FILE *members;             
 
 members = fopen("members.txt","r");
 
 if(members == NULL)
  {
  printf("\n\tWARNING : UNABLE to open members.txt\n");
  return;
  }
 
    printf("\n\n\n\t\tNAME  JOINED-DATE\n");
   
  while(fgets(members_det,58,members))            // load a line into members_det string
  { 
    printf("\t\t%s",members_det);
 
  }
    printf("\n\n");

fclose(members);
return;
}


sirama 21Jun2012 15:13

Re: HOW to use "\r" properly?
 
Different OS sees new line differently.
/n: Line Feed
/r: Carriage return

Windows: Sees new line as the combination CRLF. Carriage return and line feed. /r/n
Unix: Sees new line as single line feed. /n
Mac os: It knows only /r

Hope this helps.


All times are GMT +5.5. The time now is 21:56.