Author |
Message |
JB #1 / 6
|
 PB Creating a file
can anybody please help me ? How can I create a file with a name that is not a const char * but just a char. What I actually want to do is : -I have a file(for example example1.c ) -I take its name (example1) -I create a new file with the same name, changing .c to .txt (example1.txt) , without erasing example1.c or making anychange to it. thank you
|
Fri, 11 Feb 2005 20:21:10 GMT |
|
 |
Richard B #2 / 6
|
 PB Creating a file
Quote:
> How can I create a file with a name that is not a const char * but > just a char.
You cannot, for the obvious reason that a char is not a string. Filenames are strings. What you _can_ do is create a one-character string (two bytes - remember the null terminator!) and put the one character inside it. Quote: > What I actually want to do is : > -I have a file(for example example1.c ) > -I take its name (example1) > -I create a new file with the same name, changing .c to .txt > (example1.txt) , without erasing example1.c or making anychange to it.
That is far from a one char filename. In this case, I suggest you simply make a copy of the string. strcpy() and strchr() may be helpful, as may malloc(), depending on requirements. Richard
|
Fri, 11 Feb 2005 20:42:15 GMT |
|
 |
Mike Wahle #3 / 6
|
 PB Creating a file
Quote: > can anybody please help me ? > How can I create a file with a name that is not a const char * but > just a char. > What I actually want to do is : > -I have a file(for example example1.c ) > -I take its name (example1) > -I create a new file with the same name, changing .c to .txt > (example1.txt) , without erasing example1.c or making anychange to it. > thank you
#include <stdio.h> #include <string.h> int main() { const char ext[] = "txt"; const char dot = '.'; char file1[] = "example1.c"; char file2[sizeof file1 + sizeof ext] = {0}; char *p = strchr(file1, dot); if(p) { strncpy(file2, file1, ++p - file1); file2[p - file1] = 0; strcat(file2, ext); } else printf("file name '%s' does not contain a '%c' character\n", file1, dot); printf("file1 == %s\n", file1); printf("file2 == %s\n", file2); return 0; Quote: }
HTH, -Mike
|
Fri, 11 Feb 2005 22:32:24 GMT |
|
 |
Robert B. Clar #4 / 6
|
 PB Creating a file
Quote: >How can I create a file with a name that is not a const char * but >just a char. >What I actually want to do is : >-I have a file(for example example1.c ) >-I take its name (example1) >-I create a new file with the same name, changing .c to .txt >(example1.txt) , without erasing example1.c or making anychange to it.
Forming the filename string (in order to pass it to fopen, etc.) is independent of the actual file I/O operation. I suspect that what you're actually asking is how to manipulate a string so that you replace one substring with another. In this case, you want to replace a terminal ".ext" string with ".txt". Use strrchr() (string.h) to find the LAST '.' character in the string, then copy your new ".txt" string over it. Take care to allow for the case where there is no ".ext" substring in the original string. -- Robert B. Clark (email ROT13'ed) Visit ClarkWehyr Enterprises On-Line at http://www.3clarks.com/ClarkWehyr/
|
Fri, 11 Feb 2005 23:00:39 GMT |
|
 |
Jack Klei #5 / 6
|
 PB Creating a file
On Mon, 26 Aug 2002 07:32:24 -0700, "Mike Wahler"
Quote:
> > can anybody please help me ? > > How can I create a file with a name that is not a const char * but > > just a char. > > What I actually want to do is : > > -I have a file(for example example1.c ) > > -I take its name (example1) > > -I create a new file with the same name, changing .c to .txt > > (example1.txt) , without erasing example1.c or making anychange to it. > > thank you > #include <stdio.h> > #include <string.h> > int main() > { > const char ext[] = "txt"; > const char dot = '.'; > char file1[] = "example1.c"; > char file2[sizeof file1 + sizeof ext] = {0}; > char *p = strchr(file1, dot);
Much more robust in the general case to use strrchr() here, as there may be more than one '.' character in common file systems that use them (*nix, Windows). Even more robust is to do a strrchr() search for the last '/' (and on Windows only the last '\\',) and then only look for a period after one, if found. Quote: > if(p) > { > strncpy(file2, file1, ++p - file1); > file2[p - file1] = 0; > strcat(file2, ext); > } > else > printf("file name '%s' does not contain a '%c' character\n", > file1, dot); > printf("file1 == %s\n", file1); > printf("file2 == %s\n", file2); > return 0; > } > HTH, > -Mike
-- Jack Klein Home: http://JK-Technology.Com FAQs for comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html comp.lang.c++ http://www.parashift.com/c++-faq-lite/ alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
|
Sat, 12 Feb 2005 11:11:36 GMT |
|
 |
Mike Wahle #6 / 6
|
 PB Creating a file
Quote: > On Mon, 26 Aug 2002 07:32:24 -0700, "Mike Wahler"
> > > can anybody please help me ? > > > How can I create a file with a name that is not a const char * but > > > just a char. > > > What I actually want to do is : > > > -I have a file(for example example1.c ) > > > -I take its name (example1) > > > -I create a new file with the same name, changing .c to .txt > > > (example1.txt) , without erasing example1.c or making anychange to it. > > > thank you > > #include <stdio.h> > > #include <string.h> > > int main() > > { > > const char ext[] = "txt"; > > const char dot = '.'; > > char file1[] = "example1.c"; > > char file2[sizeof file1 + sizeof ext] = {0}; > > char *p = strchr(file1, dot); > Much more robust in the general case to use strrchr() here, as there > may be more than one '.' character in common file systems that use > them (*nix, Windows).
Ack! I meant to use strrchr, just left out the other 'r'. :-) Quote: > Even more robust is to do a strrchr() search for the last '/' (and on > Windows only the last '\\',) and then only look for a period after > one, if found.
Agreed. I didn't even think about that. Thanks for your input. -Mike
|
Sat, 12 Feb 2005 22:24:37 GMT |
|
|
|