Thread: how can i modify a text in a file

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    3

    how can i modify a text in a file

    Hi,
    I am a novice in C programming.I'm trying to open a file,go looking for a text called "word" and I will have to change that as "WOR_", with the help of pointers.
    I have got a do while loop which will run until the end of file.
    I have got another FOR loop which is set for 4(initial value-file pointer & final value-filepointer+4).the latter will copy the text into a buffer[4].
    I wonder if this is the right way of solving.
    Could some1 pls help me in handling this problem.

    Thanks in advance,
    Marth

  2. #2
    Registered User
    Join Date
    Apr 2004
    Posts
    173
    Post some code up
    The cost of software maintenance increases with the square of the programmer's creativity.

  3. #3
    Registered User
    Join Date
    May 2005
    Posts
    3
    This is the part of the code which will have to do the task.
    Code:
    void charwrite(FILE *fptr)
    { 
     int j;
     char buff[4];
     while(!feof(fptr))
     {
       for(j=&fptr;j=&fptr+4;j++)
            buff[4]= getc(fptr);
       if(buff[4]="WORD")
             buff[4]="WOR ";
       }
    }
    when i try to compile this pgm, it comes up with an error msg "cannot assign struct near* near* to int in function charwrite(struct near*) " twice for the FOR LOOP.
    CAN U PLS HELP.
    THANKS IN ADVANCE
    MARTH

  4. #4
    Registered User
    Join Date
    May 2005
    Posts
    4
    a better way:
    loop till end of file:
    read a word in some varibale, say x
    if x=="word"/strcmp
    x="WOR_"
    write x in file

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    184
    u cant do this
    Code:
    for(j=&fptr;j=&fptr+4;j++)
    as the j is just a variable of type int j should me pointer

    and what is this
    are u checking the strings in the if statment
    Code:
    if(buff[4]="WORD")
             buff[4]="WOR ";
    u cant compare two stirng like that u will have us the strcmp fucniton to check two strings

    s.s.harish

  6. #6
    Registered User
    Join Date
    Apr 2004
    Posts
    173
    Also read this FAQ regarding feof():
    http://faq.cprogramming.com/cgi-bin/...&id=1043284351
    The cost of software maintenance increases with the square of the programmer's creativity.

  7. #7
    Registered User
    Join Date
    May 2005
    Posts
    3
    Hi every1..
    thanx for ur suggestions.
    Marth

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  2. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM