Rename file problem

This is a discussion on Rename file problem within the C Programming forums, part of the General Programming Boards category; i am trying to fix a problem with renaming a file function using rename it will open file called "Country.txt" ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    2

    Unhappy Rename file problem

    i am trying to fix a problem with renaming a file function using rename
    it will open file called "Country.txt" then search in deletecode field for "N" then save it in a new file"Temporary file" then have to rename "Country file to Backup file".
    i don't know what the problem is
    could you help me to fix this problem
    Thanks
    Code:
    #include <stdio.h>
    #include<string.h>
    #include<stdlib.h>
    #include <ctype.h>
    #include"rename.h"
    
    void backu();
    
    struct Country
    {
    	 int position;
    	char country[3],name[16];
    	 char intcode[4];
    	 char deletecode[2];
    	
    };
    void main()
    //void DeleteCountry()
    {
    
    int ch,result;
    char ch1;
    int e,i,count;
    
    bool check,test;
    FILE *f;
    struct Country c;
    system("cls");
    
          f=fopen("a:\\Country.txt","r+");//
          i=0;
          count=0;
          while(fread(&c,sizeof(c),1,f)==1)
         {
                        puts(c.deletecode);
    	ch=strcmp(c.deletecode,"N");
    	if (ch==0)
    	{
    	f=fopen("a:\\CountryTemporary.txt","a+");//
    		  
    	struct Country temp[i];
    	 printf("I:%d\n",i);
    	strcpy(temp[i].country,c.country);
    	strcpy(temp[i].name,c.name);
    	strcpy(temp[i].intcode,c.intcode);
    	strcpy(temp[i].deletecode,c.deletecode);
    	temp[i].position=c.position;
    		 		
    	//fwrite(&temp,sizeof(temp),1,f);
    	 //fclose(f);
    	puts(temp[i].country);
    	puts(temp[i].name);
    	i++;
    	}
    	count++;
       }
    
     /* Attempt to rename file: */
       result = rename( "Country.txt", "CountryBackup.txt" );
       if( result != 0 )
          printf( "Could not rename " );
       else
          printf( "File  renamed to " );
    
    }

  2. #2
    Registered User Commander's Avatar
    Join Date
    Sep 2001
    Posts
    804
    I thik it's not renaming the file because the file is still open...I'm not sure though

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >void main ()
    wrong, main returns an int, so use int main(void)

    >f = fopen("a:\\Country.txt", "r+");
    You should check that the open worked before using f.
    If you are reading structs, you should use a binary mode.

    >f = fopen("a:\\CountryTemporary.txt", "a+");
    Why have you opened a new file with f, without first closing the original f?

    >struct Country temp[i];
    You can't declare variables in the middle of a function.
    i is set to zero, so you aren't creating any array indexes anyway.

    And no, you cannot rename a file that is in use.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 05:27 PM
  2. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 05:54 PM
  3. Problem with my file opener
    By kzar in forum C Programming
    Replies: 7
    Last Post: 04-20-2005, 05:20 PM
  4. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 11:51 PM
  5. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 08:01 PM

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