Deleting something on FILE I/O

This is a discussion on Deleting something on FILE I/O within the C Programming forums, part of the General Programming Boards category; today i had to write a program that change something in txt but apprently it didnt change it but when ...

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    183

    Deleting something on FILE I/O

    today i had to write a program that change something in txt but apprently it didnt change it but when i put it to screen it appears that it did so i dunno whats the problem srry for editing alot i had problem with something.
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <windows.h>
    void changer(char *first,char *exchanger)
    {
    	int i,x;
    	for(i=0,x=0;first[i]=exchanger[x];i++,x++);
    }
    int main(void)
    {
    	char buffer[100];
    	char exchanger[]="http://www.onemanga.com";
    	FILE *fp;
    	int i=0;
    	fp=fopen("D:\\test\\lol.txt","r+w");
    	fp2=fopen("D:\\test\\lchanged.txt","w");
    	while(fgets(buffer,sizeof buffer,fp)!=NULL)
    	{
    		puts(buffer);//for test to see if it changed it or not
    		if(strstr(buffer,"http://www.narutocentral.com"))
    		{
    			changer(buffer,exchanger);
    			puts(buffer);//so here it got changed but in txt it didnt !!! so WTF ???
    		}
    	}
    	return 0;
    }

  2. #2
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Posts
    322
    Code:
    while(fgets(buffer,sizeof buffer,fp)!=NULL)
    	{
    		puts(buffer);//for test to see if it changed it or not
    		if(strstr(buffer,"http://www.narutocentral.com"))
    		{
    			changer(buffer,exchanger);
    			puts(buffer);//so here it got changed but in txt it didnt !!! so WTF ???
    		}
    	}
    I don't see any code here that actually writes to the file...

  3. #3
    Registered User
    Join Date
    Dec 2008
    Posts
    183
    i thought the changer itself will change it in txt itself ?

  4. #4
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Posts
    322
    I am not understanding what txt means? I thought you meant the textfile itself.

    EDIT: I see that buffer is getting changed. Exchanger is not, however, getting changed... if that's what you want. Not clear.
    Last edited by MacNilly; 03-12-2009 at 07:38 PM.

  5. #5
    Registered User
    Join Date
    Dec 2008
    Posts
    183
    yes i want the buffer whish get changed
    i also did overwrite and it still didnt work out
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <windows.h>
    void changer(char *first,char *exchanger)
    {
    	int i,x;
    	for(i=0,x=0;first[i]=exchanger[x];i++,x++);
    }
    int main(void)
    {
    	char buffer[100];
    	char exchanger[]="http://www.onemanga.com";
    	FILE *fp;
    	int i=0;
    	fp=fopen("D:\\test\\lol.txt","rw");
    	while(fgets(buffer,sizeof buffer,fp)!=NULL)
    	{
    		
    		if(strstr(buffer,"http://www.narutocentral.com"))
    		{
    			changer(buffer,exchanger);
    			fprintf(fp,"%s",buffer);
    			puts(buffer);
    		}
    	}
    	return 0;
    }

  6. #6
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Posts
    322

    Thumbs up

    Well, I'm not exactly sure what is supposed to be going on here, but I can point out some (possibly) related problems with the code.

    Code:
    void changer(char *first,char *exchanger)
    {
    	int i,x;
    	for(i=0,x=0;first[i]=exchanger[x];i++,x++);
    }
    That is an odd for loop, with a very odd conditional statement. I like this:

    Code:
    char *temp = first;
    first = exchanger;
    exchanger = first;
    Anyways, are you trying to replace every occurance of some text read in from a file with some other text, then output the results to a new file?

  7. #7
    Registered User
    Join Date
    Dec 2008
    Posts
    183
    im trying to overwrite it in same txt not to another

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 04:02 AM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  4. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 01:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21