Thread: Help in file Manipulation

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    2

    Help in file Manipulation

    Hi

    I am currently working on file handling to insert or remove data from the same file, depending upon the choice. So i need to do like this

    IP Address (one tab space) MACAddress (one tab space) Starttime (one tab space)Endtime
    192.xxx xxxxx xxxx xxxx

    in that fashion i shld go on adding ip , mac address etc.

    the adding or deleting is upon our choice. while deleting i shld delete the last entry made to the row. and while adding ,its normal and if suppose its after a deletion of that row. i shld add it in the same row i deleted.

    Can anyone help me with this?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    The easiest approach is to simply write a new file
    - copy what you want to keep
    - ignore what you want to delete
    - add what you want to add.

    When you're done, delete the old file and rename the new file.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    For adding a line to the end of a file, just open it in append mode. Your data will be written to the very end of the file, and all the other data will remain as well.

    For removing the last row, the easiest way is to read through the file, writing out the data to a temporary file, but stopping the write out, just before the last row. Then closing the file, and renaming it.

    Why don't you post up a tiny example file - seeing is always better than a description - and I'll code up a small example on it, if you like.

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    2
    thanks...it would be something like this...basically its a log file kind of.

    1. initially the file shld out like this

    IP Address (one tab space) MACAddress (one tab space) Starttime (one tab space) Endtime

    2. when the option is to add it should add the corresponding data into its fields

    IP Address (one tab space) MACAddress (one tab space) Starttime (one tab space) Endtime

    192.168.1.1(one tab space)0123812123712871181(one tab space)21:15 (one tab space) 22:00
    122.54.3.1(one tab space) 09834823309092340996(one tab space)11:25 (one tab space) 12:00
    .
    .
    .
    .
    .
    .
    172.152.12.2(one tab space)0123129239843294239181(one tab space)9:26 (one tab space) 10:00
    so on

    3. now if the option is given as remove then it shld should remove the last line from the file

    IP Address (one tab space) MACAddress (one tab space) Starttime (one tab space) Endtime

    192.168.1.1(one tab space)0123812123712871181(one tab space)21:15 (one tab space) 22:00
    122.54.3.1(one tab space) 09834823309092340996(one tab space)11:25 (one tab space) 12:00
    .
    .
    .
    .
    .
    .
    172.152.12.2(one tab space)0123129239843294239181(one tab space)9:26 (one tab space) 10:00 - removed

    again if the option is remove then the last line before it should be removed...it goes on

    4. if you want to add now then it should be added in the last line of the file..

    192.168.1.1(one tab space)0123812123712871181(one tab space)21:15 (one tab space) 22:00
    122.54.3.1(one tab space) 09834823309092340996(one tab space)11:25 (one tab space) 12:00
    .
    .
    .
    .
    .
    .
    111.24.2.2(one tab space) 098783498341236224(one tab space)1:25 (one tab space) 2:00 - added

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I've got some running around to do, but I'll post something up, tonight.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Adak View Post
    I've got some running around to do, but I'll post something up, tonight.
    Unfortunately, I strained my elbow and can't keyboard further now. This is what I have done, so far. File for data is at bottom of program. filename is log1.txt

    Always should show last log entries, no matter how many there are. Log enyties still need tobe written out to file. only write out rowN entries from log arrasy

    i was going to Windowize it for you, but we'll see.

    Code:
    #include <stdio.h>
    #include <conio.h>
    
    #define ROW 6
    #define COL 100
    
    int loadIt(char log[ROW][COL], FILE *);  
    int menu(char log[ROW][COL], int *);
    void removeIt(char log[ROW][COL], int entryNum);
    void showIt(char log[ROW][COL], int*);
    
    int main() {
      FILE * fp;
      int i, n, rowN; 
      char log[ROW][COL] = { {'\0'}};
    
      rowN = loadIt(log, fp);
      menu(log, &rowN);
      printf("\n\n\t\t\t    press enter when ready");
    
      i = getchar(); ++i;
      return 0;
    }
    /* All loading entries go into ROW-1, first. Then they 
    are popped up, into the lower indexes of the array. 
    This allows the array to be large or small, and the 
    data to be large or small, it doesn't matter. This 
    array will have the last ROW log entries.
    */
    int loadIt(char log[ROW][COL], FILE* fp) {
      int r, j, i;
      fp = fopen("log1.txt", "rt"); 
      r = 0;
      while(fgets(log[ROW-1], COL-1, fp)) {
        ++r;
        for(i = 0; i < ROW; i++) {         //pop all entries, 1 row
          strcpy(log[i], log[i+1]);
          log[i+1][0] = '\0';
        }
      }
      fclose(fp);
      return r;
    }
    int menu(char log[ROW][COL], int *rowN) {
      int choice, entryNum;
      char user[5] = { '\0' };
    
      do {
        clrscr();
        //getchar();
        gotoxy(2,1);
        printf("\n\t\t\t         Main Menu \n");
        printf("\n        1: View or Remove an Entry  ");
        printf("\n        2: Add an Entry  (?)");
        printf("\n\n            Q to Quit");
        printf("\n\n Choose [1/2/q]: ");
        
        choice = 0;
        fgets(user, 3, stdin);
        choice = atoi(user);
        if((user[0] == 'q') || (user[0] == 'Q')) break;
        showIt(log, rowN);
        if(rowN) 
          printf("\n\n Remove which entry [0 for none] ?: ");
        else
          printf("\n\n There are no entries that can be removed");
        scanf("%d", &entryNum);
        getchar();
        if((entryNum < ROW) && (entryNum > 0)) {
          removeIt(log, entryNum);
          (*rowN)--;
          showIt(log, rowN);
        }
      }while(user[0] != 'q');
    
      return 0;
    }
    /* From the entryNum on up, bring all the entries down 1 row,
    overwriting the deleted log entry
    */
    void removeIt(char log[ROW][COL], int entryNum) {
      int i;
      --entryNum;  //for zero based arrays
      for(i = entryNum; i < ROW; i++) {
        strcpy(log[i], log[i+1]);
        if(log[i+i] == "")
          break;
      }
    }
    void showIt(char log[ROW][COL], int *rowN) {
      int r;
      printf("\n          ********* Log Entries You May Delete ********** \n\n");
      for(r = 0; r < *rowN-1; r++) {      //-1 because the first entry is 
        printf("%d\)  %s", r+1, log[r]); //always popped off the array
        if(r % 15 == 0 && r)             //during loadIt().
          getchar(); 
      }
    }
    
    
    /* 
    
    Contents of file named log1.txt:
    192.168.1.1\t0123812123712871181\t21:15 \t 22:00
    122.54.3.1\t 09834823309092340996\t11:25 \t 12:00
    192.168.1.1\t0123812123712871181\t21:15\t22:00
    122.54.3.1\t09834823309092340996\t11:25\t12:00
    172.152.12.2\t0123129239843294239181\t9:26\t10:00
    111.24.2.2\t098783498341236224\t1:25\t2:00
    
    */
    i could barely test any of it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  3. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM

Tags for this Thread