好吧,事情就是这样,我正在大学学习 C++ 课程,但不知道如何更改文件的扩展名。 首先,我们要做的是读取 .txt 文件并计算单词、句子、元音等。我明白了,但下一步是困扰我的问题。 然后,我们假设使用与输入文件相同的文件名创建一个新文件,但扩展名为 .code 而不是 .txt(在该新文件中,我们通过向每个文件的 ASCII 代码添加随机数来对字符串进行编码)如果您有兴趣的话)。 作为编程初学者,我不太确定如何做到这一点。 我使用以下代码首先获取输入文件:
cout << "Enter filename: ";
cin >> filename;
infile.open(filename.c_str());
我假设创建一个新文件,我将使用类似的东西:
outfile.open("test.code");
但我不会知道文件名是什么,直到用户输入它所以我不能说“test.txt”。 因此,如果有人知道在我创建新文件时如何更改该扩展名,我将非常感激!
Alright here's the deal, I'm taking an intro to C++ class at my university and am having trouble figuring out how to change the extension of a file. First, what we are suppose to do is read in a .txt file and count words, sentences, vowels etc. Well I got this but the next step is what's troubling me. We are then suppose to create a new file using the same file name as the input file but with the extension .code instead of .txt (in that new file we are then to encode the string by adding random numbers to the ASCII code of each character if you were interested). Being a beginner in programming, I'm not quite sure how to do this. I'm using the following piece of code to at first get the input file:
cout << "Enter filename: ";
cin >> filename;
infile.open(filename.c_str());
I'm assuming to create a new file I'm going to be using something like:
outfile.open("test.code");
But I won't know what the file name is until the user enters it so I can't say "test.txt". So if anyone knows how to change that extenstion when I create a new file I would very much appreciate it!
发布评论
评论(13)
我偶尔会问自己这个问题并最终出现在这个页面上,因此为了将来的参考,这里是单行语法:
I occasionally ask myself this question and end up on this page, so for future reference, here is the single-line syntax:
当然,如果这不是家庭作业而是一个现实世界的项目,您可能会通过使用 Boost.Filesystem 的
replace_extension()
而不是自己推出。 没有任何功能足够简单,以至于您无法发现错误,至少在某些极端情况下是如此。Of course, if this were not homework but a real-world project, you'd probably do yourself -- as well as other people reading your code -- a favor by using Boost.Filesystem's
replace_extension()
instead of rolling your own. There's just no functionality that is simple enough that you couldn't come up with a bug, at least in some corner case.有几种方法可以实现这一点。
您可以采取超级懒惰的方法,让他们只输入文件名,而不输入 .txt 扩展名。 在这种情况下,您可以将 .txt 附加到其中以打开输入文件。
然后你只需调用
下一个方法是获取整个文件名(包括扩展名),然后将 .code 附加到其中,这样你就得到了 test.txt.code。
这是否可以接受有点模棱两可。
最后,您可以使用
std::string
方法查找
,并替换
获取不带扩展名的文件名,并使用它。There are several approaches to this.
You can take the super lazy approach, and have them enter in just the file name, and not the .txt extension. In which case you can append .txt to it to open the input file.
Then you just call
The next approach would be to take the entire filename including extension, and just append .code to it so you'd have test.txt.code.
It's a bit ambiguous if this is acceptable or not.
Finally, you can use
std::string
methodsfind
, andreplace
to get the filename with no extension, and use that.不要放弃它,因为学习是练习的重点,但这里有一个提示。
您可能需要
find_last_of
< 的组合/a> 和替换
。Not to give it away since learning is the whole point of the exercise, but here's a hint.
You're probably going to want a combination of
find_last_of
andreplace
.这里有一些提示。 您已经输入了一个文件名 - 您想要做的是获取文件名中不包含扩展名的部分:
编写该函数后,您可以使用它来创建新文件的名称:
Here is a few hints. You have a filename already entered - what you want to do is get the part of the filename that doesn't include the extension:
Having written that function, you can use it to create the name of the new file:
伪代码会做类似的事情
For Change outFilename, see strrchr 和 strcpy 作为起点(可能是更合适的方法 - 不过,这对于 char* 来说效果很好)
Pseudo code would be to do something like
For changing outFilename, look at strrchr and strcpy as a starting point (might be more appropriate methods -- that would work great with a char* though)
在 Windows 中(至少),您可以使用 _splitpath 来剖析基础从其余部分中命名,然后使用您最喜欢的字符串格式化程序重新组合它们。
In Windows (at least) you can use _splitpath to dissect the base name from the rest of the pieces, and then reassemble them using your favorite string formatter.
为什么不使用字符串方法
find_last_of()
?why not using the string method
find_last_of()
?我只需将“.code”附加到用户输入的文件名中。 如果他们输入“test.txt”,那么输出文件将为“test.txt.code”。 如果他们输入没有扩展名的文件名,例如“test”,那么输出文件将为“test.code”。
我一直在生成输出文件和某种相关日志记录/诊断输出的程序中使用这种技术。 它实现起来很简单,而且在我看来,它使文件之间的关系更加明确。
I would just append ".code" to the filename the user entered. If they entered "test.txt" then the output file would be "test.txt.code". If they entered a file name with no extension, like "test" then the output file would be "test.code".
I use this technique all the time with programs that generate output files and some sort of related logging/diagnostic output. It's simple to implement and, in my opinion, makes the relationships between files much more explicit.
您需要做的是将原始文件名复制到一个新变量中,您可以在其中更改扩展名。 像这样的事情:
如果您不需要保留原始文件名以供以后使用,则可以使用“文件名”变量。 在这种情况下,您可以使用:
关键是查看 C++ 字符串类的定义并了解每个成员函数的作用。 使用 rfind 将向后搜索字符串,并且您不会意外命中可能是原始文件名一部分的文件夹名称中的任何扩展名(例如“C:\MyStuff.School\MyFile.txt”)。 当使用 find、rfind 等的偏移量时,在将它们作为计数传递给其他方法时,您还需要小心正确使用它们(例如,您是否使用 allocate(filename, 0, extPos-1)、 (文件名,0,extPos),分配(文件名,0,extPos + 1))。
希望有帮助。
What you'll need to do is copy the original filename into a new variable where you can change the extension. Something like this:
It's possible you could use the "filename" variable if you don't need to keep the original filename around for later use. In that case you could just use:
The key is to look at the definition of the C++ string class and understand what each member function does. Using rfind will search backwards through the string and you won't accidentally hit any extensions in folder names that might be part of the original filename (e.g. "C:\MyStuff.School\MyFile.txt"). When working with the offsets from find, rfind, etc., you'll also want to be careful to use them properly when passing them as counts to other methods (e.g. do you use assign(filename, 0, extPos-1), assign(filename, 0, extPos), assign(filename, 0, extPos+1)).
Hope that helps.
使用 strstr 怎么样:
How about using strstr:
非常简单:
结果:
“文件.abc”
Very Easy:
Result:
"file.abc"