Thread: writing to a file

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    44

    writing to a file

    Hello, in the next piece of code I will read the content of one file and write that to another file, accept for a certrain string which I will replace by another string that the user gave as input parameter. Now my question is; in the loop while( (ptr + lengte) + i < (zin + 18)), why do you check for zin + 18 instead of zin + 19? It does not seem correct to me, but the algoritme works, so I must see something wrong. What's wrong with thinking like this"

    let's say that the fgets buffer was 5 instead of 19, and the zin buffer 6.
    The string you read is: abcde. The substring you want to replace is cd. The begin address of zin is 1000. The length of the substring is 2.

    while(zin + i < ptr)
    #
    {
    #
    putc(*(zin + i), output);
    #
    i++;
    #
    }
    #

    #
    fputs(argv[4], output);

    this goes correctly and you write abNEWSUBSTRING but then you come in the new while and:

    #
    i = 0;
    #

    #
    while( (ptr + lengte) + i < (zin + 4))

    1002 (the beginaddress of the substring) + 2 < 1000 + 4. Not true so you will not write e!

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main (int argc, char *argv[])
    {
    
    	FILE *input;
    	FILE *output;
    
    	char *ptr;
    
    	char zin[20];
    	int lengte, i = 0;
    
    	if (argc != 5)
    	{
    		printf("Aantal bestanden zijn fout\n");
    		exit(0);
    	}
    
    	if ((input = fopen(argv[1], "r")) == NULL)
    	{
    		perror("Fout ");
    		exit(0);
    	}
    
    	if ((output = fopen(argv[2], "w")) == NULL)
    	{
    		perror("Fout ");
    		exit(0);
    	}
    
    
    	while (fgets(zin, 19 ,input) != NULL)
    	{
    		if (strstr(zin, argv[3]) == NULL)
    			fputs(zin, output);
    		else
    		{
    			lengte = strlen(argv[3]);
    		
    			ptr = strstr(zin, argv[3] );	
    				
    	
    			while(zin + i < ptr)
    			{
    				putc(*(zin + i), output);
    				i++;
    			}
    	
    			fputs(argv[4], output);
    			
    			i = 0;
    		
    			while( (ptr + lengte) + i < (zin + 18))
    			{
    				putc(*(ptr + i + lengte), output);
    				
    				i++;
    			}
    		
    		
    		}
    	}
    
    	fclose(input);
    	fclose(output);
    
    	return 0;
    }

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    I do not think +18 or +19 is correct...

    better get rid of the loop and just use

    fputs(ptr+lengte);
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. File Writing Problem
    By polskash in forum C Programming
    Replies: 3
    Last Post: 02-13-2009, 10:47 AM
  3. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  4. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM