Thread: Errors with program to replace string with another

  1. #1
    System-7
    Join Date
    Nov 2005
    Posts
    65

    Errors with program to replace string with another

    Alright I wrote the program the way I thought it should work but I am getting some errors when using subStrBeg, subStrEnd and i together. I understand that they're different data types...but I don't know how exactly to resolve this problem.
    I get these errors:
    Code:
    r.c:33: warning: comparison between pointer and integer
    r.c:35: warning: passing argument 1 of ‘fputs’ makes pointer from integer without a cast
    r.c:41: warning: assignment makes integer from pointer without a cast
    r.c:43: warning: passing argument 1 of ‘fputs’ makes pointer from integer without a cast
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    #define MAX 512
    #define _GNU_SOURCE
    
    int main(int argc, char *argv[])
    {
      int i = 0;
      FILE *file, *file2;
      char line[MAX], *subStrBeg, *subStrEnd;
      int subStrLen, strLength;
    
      if (argc != 4)
      {
        printf("You must enter: ./replace old-string new-string file-name\n");
        exit(1);
      }
    
      file = fopen(argv[3], "r");
      file2 = fopen("temp", "w");
    
      while (fgets(line,sizeof(line),file) != NULL)
      {
        /* Find when string begins, length of string before
           substring, and then the end of the substring */
        subStrBeg = strstr(line, argv[1]);
        subStrLen = subStrBeg-line;
        subStrEnd = subStrBeg+strlen(argv[1]);
        strLength = strlen(line);
    
        /* Write left part of string to file */
        for (i=0; i < subStrBeg; i++)
        {
          fputs(line[i], file2);
        }
        /* Write replaced string to file */
        fputs(argv[2], file2);
        /*Write right part of string to file */
        for (i=subStrEnd; i < strLength; i++)
        {
          fputs(line[i], file2);
        }
      }
      /* Close everything */
      fclose (file);
      fclose (file2);
      return 0;
    }
    If anyone can show me an example of how to fix this or a internet page that explains what to do in this situation...it would be great

    Thanks,
    Dan

  2. #2
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    You are comparing a pointer to an integer:
    Code:
       for (i=0; i < subStrBeg; i++)
        {
          fputs(line[i], file2);
        }
    And you are doing the same in other places.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  3. #3
    System-7
    Join Date
    Nov 2005
    Posts
    65
    I know I'm doing that. The problem is I don't know a way around doing that. I'm not sure how to convert it to something else...if that's the way I should do it.

    Dan

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    for (i=0; i < subStrBeg; i++)
    ->
    Code:
    for (i=0; i < subStrBeg - line; i++)
    Subtracting the start of the string from a pointer to an index will result in the index itself (a variable of type ptrdiff_t).
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  2. Help with string program
    By Duskan in forum C Programming
    Replies: 8
    Last Post: 04-02-2007, 08:27 AM
  3. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  4. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  5. Replies: 5
    Last Post: 05-25-2004, 04:36 PM