removing a .

This is a discussion on removing a . within the C Programming forums, part of the General Programming Boards category; hey, i did some searches at the forum, but was not quite satisfied with the solution it came up with ...

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    67

    removing a .

    hey, i did some searches at the forum, but was not quite satisfied with the solution it came up with
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    
    #define TO_REMOVE .
    #define TO_REPLACE _ // voorzichtig, een lege character..
    
    int main(int argc, char *argv[])
    {
         // variabelen
        FILE *fFile1;
        FILE *fFile2;
        FILE *tmpFile;
        
        char filenaam[150];
        char toremove;
        int i;
        
        //----------------
        
        if (argc < 2)
        {
            fprintf(stdout, "\nERROR: geen bestand opgegeven\n");
            fprintf(stdout, "\n\nGebruik als volgt: rename <filenaam>\nBijvoorbeeld: rename 101.60.12.dwg");
            exit(EXIT_FAILURE);
        }
        
        // open het bestand (argv[1])
        if (strlen(argv[1]) > 150)
        {
            fprintf(stdout, "\nERROR: filenaam te lang\n");
            exit(EXIT_FAILURE);
        }
        
        // kopieer argument 1 naar de filenaam var,
        strcpy(filenaam, argv[1]); 
        // open het bestand
        fFile1 = fopen(filenaam, "r");
        //bestaat het bestand niet?
        if (fFile1 == NULL)
        {
            fprintf(stdout, "\nERROR: file bestaat niet of kan niet geopend worden");
            exit(EXIT_FAILURE);
        } 
        //open de tmpfile
        tmpFile = fopen("tmp", "w");
        //bestaat ie niet?
        if (tmpFile == NULL)
        {
            fprintf(stdout, "\nERROR: tmp bestaat niet of kan niet geopend worden");
        }
        //schrijf de file-naam naar het bestand
        for (i = 0; i < strlen(filenaam); i++)
        {
            if (filenaam[i] == '.')
            { 
                    
                   /*HERE IS MY BOTTLENECK,
                      HOW DO I REMOVE THE .?
    */
                   strcpy(toremove, filenaam[i]);
                    remove(toremove);
            }
            
            
            
        }
        
        
        
        //sluit alle2 de bestanden voor de zekerheid
        //fclose(tmpFile);
        //fclose(fFile1);
        
        
        
        // geef een goedmelding
        fprintf(stdout, "Klaar, bestand met succes gewijzigd");
        
        
        return 0;
    }
    any help is appreciated
    (i know the coding could be done alot more beautiful but first i need to get this straight :>

  2. #2
    Registered User
    Join Date
    Jun 2003
    Posts
    124
    I haven't understand what your question is, but i tell you that function remove() gets a char * and is used to delete files, not charactes. ( i say about remove because i see it in the title ).
    Loading.....
    ( Trying to be a good C Programmer )

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    67
    I have to remove the .'s from a file

    first it opens up a file
    writes its name into another file
    checks for .'s
    removes the .'s, except for the last one
    and renames the first file to the result of the .'s removing

    follow me :P

    (in case you wonder why, we have an eSafe appliance at work and it filters out double of more dots in a file name (because many virusses use things like .xls.exe and the likes, but project drawings contain alot of them so i decided to make an app to remove em.)

    yes I need some good C lessons (i'm a self-tutoring school boy and it isnt working out the way I want it too )

  4. #4
    Wannabe Coding God
    Join Date
    Mar 2003
    Posts
    259
    Originally posted by TeQno

    (in case you wonder why, we have an eSafe appliance at work and it filters out double of more dots in a file name (because many virusses use things like .xls.exe and the likes, but project drawings contain alot of them so i decided to make an app to remove em.)
    *is confussed* why remove them in the first place?
    They say that if you play a Windows Install CD backwords, you hear satanic messages. That's nothing; play it forward and it installs Windows.

  5. #5
    Registered User moonwalker's Avatar
    Join Date
    Jul 2002
    Posts
    282
    Keep copying the stuff to another array .. when it comes to the ".", don't copy it... use the new array for your purpose.

    I hope that's what you're looking for..

    [EDIT]
    // sheesh.. nevermind.. i'm confused too
    [/EDIT]

  6. #6
    Registered User
    Join Date
    May 2003
    Posts
    67
    @Shogun ? i thought i explained that, but i dont really think it matters WHY, but HOW.

    @moonwalker thanx ill try that

  7. #7
    Registered User
    Join Date
    May 2003
    Posts
    67
    Code:
    if (filenaam[i] != '.')
            { 
                    
                        fprintf(tmpFile, filenaam[i])
             
                  
            }
    allright.. now it says it makes a pointer from integer without a cast, what am i doing wrong

  8. #8
    Registered User
    Join Date
    May 2003
    Posts
    67
    it isnt the last .xxx

    i just want all the dots away, but i think im there thanks

  9. #9
    Registered User
    Join Date
    Jun 2003
    Posts
    124
    If the name of the file is enered by the user, do something like this:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
    	int i, check = 1;
    	char oldName[100], newName[100];
    
    	puts( "File name:" );
    
    	fgets( oldName, sizeof( oldName), stdin );
    	i = strlen( oldName );
    
    	while( i-- >= 0 )
    		if( oldName[i] == '.'){
    			if( check != 1 )      //this sucks a little...make it better
    				newName[i] = '_';
    			else
    				newName[i] = '.';
    			check = 0;
    		}
    		else
    			newName[i] = oldName[i];
    
    	if( !rename( oldName, newName ) )
    		puts( "File renamed" );
    
    	return 0;
    }
    Loading.....
    ( Trying to be a good C Programmer )

  10. #10
    Wannabe Coding God
    Join Date
    Mar 2003
    Posts
    259
    Originally posted by TeQno
    @Shogun ? i thought i explained that, but i dont really think it matters WHY, but HOW.

    @moonwalker thanx ill try that
    yes you did but it didn't refer to that read my PM.
    They say that if you play a Windows Install CD backwords, you hear satanic messages. That's nothing; play it forward and it installs Windows.

  11. #11
    Registered User
    Join Date
    May 2003
    Posts
    67
    thanks money I redid some things and it works like a charm now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Removing Leading & Trailing Whitespace
    By EvilGuru in forum C Programming
    Replies: 11
    Last Post: 12-01-2005, 01:59 PM
  2. Removing from the front of a Vector
    By Zildjian in forum C++ Programming
    Replies: 1
    Last Post: 09-11-2004, 10:49 AM
  3. Removing text between /* */ in a file
    By 0rion in forum C Programming
    Replies: 2
    Last Post: 04-05-2004, 08:54 AM
  4. Removing a header from a file
    By JamesK in forum Windows Programming
    Replies: 3
    Last Post: 05-26-2003, 05:13 PM
  5. removing from array
    By SIKCAR in forum C Programming
    Replies: 1
    Last Post: 10-21-2002, 07:03 AM

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