switching between file names in c code

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • wayne jones
    New Member
    • Jan 2012
    • 6

    switching between file names in c code

    I have multiple text files and want to be able to switch between them. they are configuration files and only one at a time needs to have the name myfile.conf. It would be nice if each of the files would change back to a certain file name when they are not the "active" one.

    example: files - bob.conf, jill.conf, sam.conf and an active file myfile.conf (with original name jeff.conf)
    change myfile.conf back to jeff.conf and then make bob.conf into myfile.conf
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Rather than rename the file can you not just open the correct file?

    Comment

    • wayne jones
      New Member
      • Jan 2012
      • 6

      #3
      no - It is a configuration file that has to have a certain name

      I was thinking of adding a comment to the top of each file so that it can be used for the renaming process (ex. - #bob)

      I am not sure if this would be the best way to solve the problem though - so any suggestions would be appreciated.

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        THen I suggest you copy the selected file to the file of the corect name. That will be less hassle than remembering what file you are using.

        Comment

        • wayne jones
          New Member
          • Jan 2012
          • 6

          #5
          That is what I am doing now manually - But I have multiple files that have to be switched sometimes over a dozen times a day.What I am needing is to make a program with a GUI where I can just select which file to use from a list. It will then need to rename the previous file to it's original name and rename the new one to the proper program name ( ex. - myfile.conf). There are over a dozen configuration files for different uses but the program can only read one at any given time which must have an exact name.

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            I still think rnaming is a bad idea. FileA gets renamed to myfile.conf then your program crashes and FileA is gone forever. No one remembers FileA is now myfile.conf.

            Better to copy FileA to myfile.conf.

            That way if the program crahses you are OK. If the copy fails you are OK. If the program forgets to rename due to some programmer forgetting to do that, then you are OK.

            I don't ee the downeside.

            Comment

            • wayne jones
              New Member
              • Jan 2012
              • 6

              #7
              Yes you are right - I had misunderstood what you where saying.I will make each file a menu choice and then have the program open the chosen file and copy to myfile.conf. I will start with a simple program for now and then later add the functions to input the filenames for the menu choices as a separate menu item. I will let you know how everything works out when I am finished. Thanks for the tip.

              Comment

              • wayne jones
                New Member
                • Jan 2012
                • 6

                #8
                Here is a sample program for me to try to get an answer with

                [code]

                #include <stdio.h>

                #include <stdlib.h>

                int main()
                {
                char ch, file1[2000], file2[2000];
                FILE *fs,*ft;


                printf("Enter name of file to copy ");
                gets(file1);

                fs = fopen(file1,"r" );
                if( fs == NULL )
                {
                perror("Error ");
                printf("Press any key to exit...\n");

                exit(EXIT_FAILU RE);
                }

                printf("Enter name of target file ");
                gets(file2);


                ft = fopen(file2,"w" );
                if( ft == NULL )
                {
                perror("Error ");
                fclose(fs);
                printf("Press any key to exit...\n");

                exit(EXIT_FAILU RE);
                }

                while( ( ch = fgetc(fs) ) != EOF )
                fputc(ch,ft);

                printf("File copied successfully.\n ");


                fclose(fs);
                fclose(ft);
                return 0;
                }
                [endcode]

                In a program such as this - what code would I need to add/change to be able to have the destination file name coded into the program instead of having it be entered. I have tried but keep getting errors when I try. Any help would be appreciated.

                Comment

                • donbock
                  Recognized Expert Top Contributor
                  • Mar 2008
                  • 2427

                  #9
                  How about changing the program to take an optional command line argument that specifies the name of the configuration file? The name of the file defaults to myfile.conf if the optional argument is missing. Then your GUI can be used to decide which file to specify on the command line.

                  Another way to do this is to use an environment variable instead of a command line argument. However, I think command line arguments are more portable than environment variables.

                  Comment

                  • wayne jones
                    New Member
                    • Jan 2012
                    • 6

                    #10
                    Thanks for the help - I just went to C++ instead of C. In C++ it was really easy to accomplish what I was wanting to do. If I need anything else in the future (and I probably will) then I know where to come.

                    Comment

                    Working...