在c中将字符串附加到输入文件名

发布于 2024-11-30 12:51:37 字数 286 浏览 1 评论 0原文

我需要使用系统调用编写一个程序来读取文件、反转字符串并将其打印到输出文件。如果输入文件是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

老娘不死你永远是小三 2024-12-07 12:51:37

您不能附加到文字“reverse_”。尝试这样的事情:

char str[ENOUGH] = {0};
snprintf(str, sizeof(str), "reverse_%s", argv[1]);

You can't append to the literal "reverse_". Try something like this:

char str[ENOUGH] = {0};
snprintf(str, sizeof(str), "reverse_%s", argv[1]);
追风人 2024-12-07 12:51:37

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

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

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

您实际上是在说这样的事情:

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

..这是不正确的,因为您无法修改(或者至少不应该)文字“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] 字符串中的字符数,然后添加前缀所需的字符,并保留该数量加一,因为我们还需要存储标记字符串结尾:

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 );

如果您无法使用此类向量,则应保留并使用动态内存。

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 );

希望这有帮助。

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 ]

..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:

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 );

Hope this helps.

听,心雨的声音 2024-12-07 12:51:37

在您的示例中,“reverse_”是一个字符串常量文字,您试图将一些其他内存位置附加到常量文字中,这是不可能的。

您可以执行以下操作:

 char *buffer;

 buffer = malloc (sizeof (char) * BUFSIZ);

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

 /* Work here */

 free (buffer); /* before termination */  

或者您也可以将缓冲区定义为具有 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:

 char *buffer;

 buffer = malloc (sizeof (char) * BUFSIZ);

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

 /* Work here */

 free (buffer); /* before termination */  

Or you can also define the buffer as a static array with BUFSIZ length.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文