By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,948 Members | 795 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,948 IT Pros & Developers. It's quick & easy.

how to change a file's extension in C ?

P: 3
i've opened a file with fopen but how do i get the file's name or the path of argv which is the argument (the file) that's used and then change or add ".something" extension to the file's name ?
Jun 18 '17 #1

✓ answered by weaknessforcats

argv is a pointer to an array of char. That is, a pointer to an array of strings. So argv[0] is a string. Ditto for argv[1].

When argv[n] is null you have reached the end of the array.

In your case, I expect argv[0] has your filepath in it.

So do a strlen on argv[0] so see how big it is, then allocate memory for the strlen(argv[0] + 1). Next copy the string into the allocated memory.

From here you have a string manipulation problem.
Once you have the string modified you can use it to rename the file.

The rename function wants you to supply both the original path and the new path.

If you need more help than this please repost.

Share this Question
Share on Google+
3 Replies


P: 3
i did try with :
Expand|Select|Wrap|Line Numbers
  1.   char *extension = ".something";
  2.         char *newFileName ="";
  3.  strcpy(newFileName, *argv);
  4.    strcat(newFileName, extension );
but i get segment fault cuz of this
Jun 18 '17 #2

weaknessforcats
Expert Mod 5K+
P: 9,197
argv is a pointer to an array of char. That is, a pointer to an array of strings. So argv[0] is a string. Ditto for argv[1].

When argv[n] is null you have reached the end of the array.

In your case, I expect argv[0] has your filepath in it.

So do a strlen on argv[0] so see how big it is, then allocate memory for the strlen(argv[0] + 1). Next copy the string into the allocated memory.

From here you have a string manipulation problem.
Once you have the string modified you can use it to rename the file.

The rename function wants you to supply both the original path and the new path.

If you need more help than this please repost.
Jun 18 '17 #3

P: 3
@weaknessforcats Thanks got it!
Jun 19 '17 #4

Post your reply

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