473,466 Members | 1,377 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

switching between file names in c code

6 New Member
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
Jan 6 '12 #1
9 1921
weaknessforcats
9,208 Recognized Expert Moderator Expert
Rather than rename the file can you not just open the correct file?
Jan 6 '12 #2
wayne jones
6 New Member
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.
Jan 7 '12 #3
weaknessforcats
9,208 Recognized Expert Moderator Expert
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.
Jan 7 '12 #4
wayne jones
6 New Member
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.
Jan 8 '12 #5
weaknessforcats
9,208 Recognized Expert Moderator Expert
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.
Jan 9 '12 #6
wayne jones
6 New Member
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.
Jan 9 '12 #7
wayne jones
6 New Member
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_FAILURE);
}

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_FAILURE);
}

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.
Jan 10 '12 #8
donbock
2,426 Recognized Expert Top Contributor
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.
Jan 10 '12 #9
wayne jones
6 New Member
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.
Jan 11 '12 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Chris | last post by:
Hi, In C# I tried to save a file from a generated file name. Just before launching the dialog I check for a valid file name to be sure. There for I used the method ValidateNames from the save...
4
by: Lance | last post by:
Are there any methods that indicate whether a string can be used as a full path for a file? For example, what I'm looking for is a method that would test for things like correct file name format,...
6
by: Mark | last post by:
I want the VB.Net program to read and file names from a directory. I thought the best thing to do was to place these file names into a "temporary" MS Access database for sorting, etc. So I'm...
3
by: cjb | last post by:
Is there a way to get Directory.GetFiles to return multi-language file names? I haven't found any overloads that allow any such parameter. The way I am using it now is: foreach (string d in...
1
by: helraizer1 | last post by:
Hi all, I have a photography page on my website, I have pictures on the page (obviously) but the file names are stored and loaded in Javascript, in an array. var pic = new...
0
by: stainless | last post by:
I know this is not necessarily a C# question but the code I am wrinting is C# and I wasn't sure whereelse to ask the question. I have taken some web code (from codeproject.com) that performs FTP...
3
by: =?ISO-2022-JP?B?Ik1hcnRpbiB2LiBMbyJ3aXMi?= | last post by:
Step 4: Either wait for Python 2.7 or apply the patch to your own copy Actually, this is released in Python 2.6, see r62724. Regards, Martin
1
by: mujunshan | last post by:
Yesterday, I installed PythonCE on my cellphone whose OS is Windows Mobile 5.I wanted to use numpy as calculation tool.But after I copy numpy module in my desktop computer into my phone,I find many...
1
by: Ormazd | last post by:
Hello, I was wondering if anyone might be able to help me with a little PERL script? I'm very new and I have been given a task to write a simple Perl script that prints out the file names and...
1
Coldfire
by: Coldfire | last post by:
Hi, The strange problem i am having is, the input element of type='file' not reading file names after 20 file elements. It simple returns null on reading the 'name' of file. The code is...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.