将字符串附加到 c 中的输入文件名 [英] Appending string to input file name in c

查看:44
本文介绍了将字符串附加到 c 中的输入文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用系统调用编写一个程序来读取文件、反转字符串并将其打印到输出文件中.如果输入文件是test.txt,则输出应写入文件reverse_test.txt.请让我知道如何将字符串 reverse_ 附加到我将在其中写入结果的输出文件的名称.

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.

推荐答案

strcat() 标准库函数接受两个参数:目标字符串和源字符串.

The strcat() standard library function accepts two parameters: the destination and the source string.

这意味着,当你尝试这样的事情时:

This means that, when you try something like this:

strcat( "reverse_", argv[1] );

你实际上是在说这样的话:

You're actually saying something like this:

"reverse_" = "reverse_" + argv[ 1 ]

..这是不正确的,因为你不能修改(或者,至少,你不应该)文字reverse_".可以肯定的是,您将无法更改其长度,因为在此过程中您会破坏代码中使用的其他文字.

..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 已为您提供了有关如何实现您正在寻求的结果的提示,但如果您想坚持所遵循的过程,这里是解释:

@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) 您需要在内存中保留一个位置,用于存储反向文件名.

a) You need to reserve a place in memory in which you will store the reversed file name.

b) 你需要在那里写上reverse_".

b) You need to write "reverse_" there.

c) 最后,需要在 argv[ 1 ] 中连接文件名

c) Finally, you need to concat the file name in argv[ 1 ]

令人震惊的是,第一步是最困难的一步.需要多少空间?给定的文件名最多可以包含 255 个字符:

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 个字符!...这让我们无处可去.

¡8192 characters!... this is leading us nowhere.

一个更好的方法,导致最好的准确性,是计算 argv[ 1 ] 字符串中的字符数,然后添加前缀所需的字符,并保留该数量加一,因为我们还需要存储字符串结尾的标记:

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:

const char * prefix = "reserve_";
int needed = strlen( argv[ 1 ] ) + strlen( prefix ) + 1;

char store[ needed ];

strcpy( store, prefix );                    // store <- prefix
strcat( store, argv[ 1 ] );                 // store <- store + argv[ 1 ]

printf( "%s\n", store );

如果你不能使用这些类型的向量,你应该保留和使用动态内存.

In case you cannot use these kind of vectors, you should reserve and use dynamic memory.

const char * prefix = "reserve_";
int needed = strlen( argv[ 1 ] ) + strlen( prefix ) + 1;

char * store = (char *) malloc( sizeof( char ) * needed );

strcpy( store, prefix );                    // store <- prefix
strcat( store, argv[ 1 ] );                 // store <- store + argv[ 1 ]

printf( "%s\n", store );
free( store );

希望这会有所帮助.

这篇关于将字符串附加到 c 中的输入文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆