在c中将字符串附加到输入文件名
我需要使用系统调用编写一个程序来读取文件、反转字符串并将其打印到输出文件。如果输入文件是 test.txt
,则输出应写入文件 reverse_test.txt
。请告诉我如何将字符串 reverse_
附加到我将在其中写入结果的输出文件的名称中。
我尝试了下面的代码,但它给出了错误。
strcat("reverse_",argv[1]);
我已经编写了其余的代码,它工作正常,但无法解决这部分。
I need to write a program using system calls to read a file, reverse the string and print it out to an output file. If the input file is test.txt
, output should be written to file reverse_test.txt
. Please let me know how I can append the string reverse_
to the name of the output file where I would be writing the results.
I tried the code below but it gives error.
strcat("reverse_",argv[1]);
I have written the rest of the code and it works fine but unable to solve this part.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您不能附加到文字“reverse_”。尝试这样的事情:
You can't append to the literal "reverse_". Try something like this:
strcat() 标准库函数接受两个参数:目标字符串和源字符串。
这意味着,当您尝试这样的事情时:
您实际上是在说这样的事情:
..这是不正确的,因为您无法修改(或者至少不应该)文字“reverse_”。可以肯定的是,您将无法更改其长度,因为在此过程中您将破坏代码中使用的其他文字。
@cnicutar 已经给了你一个关于如何实现你正在寻求的结果的提示,但如果你想坚持你所遵循的过程,这里是解释:
a)你需要在内存中保留一个位置,你可以在其中保留一个位置。将存储反转的文件名。
b) 你需要在那里写“reverse_”。
c) 最后,您需要在 argv[ 1 ] 中连接文件名
第一步是最困难的一步。需要多少空间?给定文件名最多可包含 255 个字符:
http://en.wikipedia.org/wiki/ NTFS
http://en.wikipedia.org/wiki/Ext4
...然而,没有什么可以阻止用户输入绝对或相对路径。这意味着我们可能应该给出命令行可以处理的最大字符长度,即...
http: //support.microsoft.com/kb/830473
¡8192 个字符!...这使我们无处可去。
一种更好的方法是计算 argv[1] 字符串中的字符数,然后添加前缀所需的字符,并保留该数量加一,因为我们还需要存储标记字符串结尾:
如果您无法使用此类向量,则应保留并使用动态内存。
希望这有帮助。
The strcat() standard library function accepts two parameters: the destination and the source string.
This means that, when you try something like this:
You're actually saying something like this:
..which is incorrect, since you cannot modify (or, at least, you shouldn't) the literal "reverse_". It is certain that you won't be able to change its length, since in the process you'd be breaking other literals of use in your code.
@cnicutar has given you a hint about how to achieve the result you are seeking, but in case you would like to adhere to the process you were following, here is the explanation:
a) You need to reserve a place in memory in which you will store the reversed file name.
b) You need to write "reverse_" there.
c) Finally, you need to concat the file name in argv[ 1 ]
The first step is shockingly the most difficult one. How much space is needed? A given file name can be of a maximum of 255 characters:
http://en.wikipedia.org/wiki/NTFS
http://en.wikipedia.org/wiki/Ext4
...however, nothing stops the user of entering an absolute or relative path. This means that probably we should give the maximum character length that the command line can handle, which is...
http://support.microsoft.com/kb/830473
¡8192 characters!... this is leading us nowhere.
A much better method, leading to best accuracy, is to count the number of characters in the argv[ 1 ] string, then add the characters needed for the prefix, and reserve that amount plus one, since we need to also store the mark for the end of string:
In case you cannot use these kind of vectors, you should reserve and use dynamic memory.
Hope this helps.
在您的示例中,“reverse_”是一个字符串常量文字,您试图将一些其他内存位置附加到常量文字中,这是不可能的。
您可以执行以下操作:
或者您也可以将缓冲区定义为具有
BUFSIZ
长度的静态数组。in your example "reverse_" is a string constant literal and you are attempting to append some other memory locations to the constant literal, which is not possible.
You can do the following:
Or you can also define the buffer as a static array with
BUFSIZ
length.