Thread: How replace a string of a specific person in a file

  1. #1
    Registered User
    Join Date
    Mar 2015
    Posts
    1

    How replace a string of a specific person in a file

    I'm having a bit of problem. I've set the string 'Absent' to all student in a file. However, I want to replace 'Absent' with 'present' when the correct ID assigned to a student is entered. In other words, 'Absent' will only change to 'Present' for a specific person at a time. I'm not sure how to implement this and I'm asking kindly if someone would help. Thanks in advance

  2. #2
    Registered User
    Join Date
    Feb 2012
    Posts
    347
    I think you have to write some logic.
    Last edited by Satya; 03-05-2015 at 11:16 PM.

  3. #3

  4. #4
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    1. This isn't the 80s, stop using conio.h and whatever broke compiler you have and Get a modern compiler
    2. Using text file IO for this is a bad decision, take a look at Binary File IO
    3. The best way to do this is to use a DB, however if you are doing a roll your own format, I would suggest a lookup table at the end or beginning of the file (or a separate file all together) that has the student mapped to a entry number. You would read this in at the beginning, then advance to the appropriate place on the file based on your look up table results, and modify the structure from there.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  5. #5
    Registered User
    Join Date
    Sep 2014
    Posts
    364
    The structure use only pointer to character.
    Code:
    typedef struct record {
      char *fname;
      char *lname;
      int code;
    } information;

    First, the function strdup is unknown for me, but i assume that you want to copy the string (strcpy/strncpy).
    But this will not work, because there are only pointers in the structure. There are no space for characters.


    Second: if the input file isn't open, you return without a value, but the main function should return a integer.


    Third: you don't check the output file.
    Code:
    …
        ufile= fopen("update.txt","w");
    
    
        fprintf(ufile,"First Name     Last Name     ID     Status     Time Arrived\n");
        for (i = 0; i < x; i++) {
            fprintf(ufile,"%-15s%-14s%2d%12s ",(array[i].fname), (array[i].lname), (array[i].code), status);
            fprintf(ufile,"%16s",(buf)); 
        }//end of for loop
        fclose(ufile);
    …

    The indentation is also a little bit broken.
    Update your code and post it here in code-tags.
    Other have classes, we are class

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 13
    Last Post: 04-02-2012, 05:51 PM
  2. Replies: 5
    Last Post: 11-11-2011, 05:49 PM
  3. How to replace external file with internal string Array ?
    By sharris in forum C++ Programming
    Replies: 4
    Last Post: 02-21-2011, 09:34 PM
  4. Replace wildcard text in file with specific text.
    By untz in forum C++ Programming
    Replies: 4
    Last Post: 11-22-2010, 06:35 PM
  5. How to find and replace a string in a file
    By salzouby in forum C Programming
    Replies: 13
    Last Post: 09-14-2010, 08:55 AM

Tags for this Thread