简体   繁体   English

如何操作字符指针内容?

[英]How can I manipulate char pointer contents?

I am writing a C program that expects a filename as its argument.我正在编写一个需要文件名作为参数的 C 程序。 The program will read the contents of the file, and will output an other file (with a similar name, same extension but with an appended characters at the end of the name).程序将读取文件的内容,并输出另一个文件(名称相似,扩展名相同,但名称末尾附加字符)。

For example, if I run the binary like this:例如,如果我像这样运行二进制文件:

./a.out some_file.txt

Then a new file will be created some_file_out.txt .然后将创建一个新文件some_file_out.txt

 New File Created: [some_file_out.txt]

My code below works, but it is too ugly.我下面的代码有效,但它太丑了。 I am sure there is a better way of doing this.我相信有更好的方法可以做到这一点。 I considered working with std::string and that would simplify things, but I won't be able to use the string value with fopen() since it only accepts char* variables the filename.我考虑过使用std::string并且这会简化事情,但是我无法将字符串值与fopen()一起使用,因为它只接受char*变量作为文件名。

What is a better way of manipulating a char* variable?操作char*变量的更好方法是什么?

#include <stdio.h>
#include <string.h>

int main(int argc, char* argv[]) {
    // validate argc count and that argv[1] file exists
    // assume all validation is done. For simplicity's sake.

    char* input_file = argv[1];
    /* do something with the file*/

    char output_file[strlen(argv[1]) + 4];  // reserve a new char[] with the same size
                                            //  as the input file, plus 4 characters for
                                            //  the output filename variation

    // assuming the extension is 3 characters long in all cases
    int dot_location = strlen(argv[1])-4;

    // copy characters from input filename to output filename
    for(int i=0; i<dot_location ; i++) {
        output_file[i] = input_file[i];
    }

    // add the name variation:
    output_file[dot_location  ] = '_';
    output_file[dot_location+1] = 'o';
    output_file[dot_location+2] = 'u';
    output_file[dot_location+3] = 't';

    // add the extension back
    for(int i=0; i<4; i++) {

        int new_index = dot_location + i + 4;
        int old_index = dot_location + i;

        output_file[new_index] = input_file[old_index];
    }

    // make sure the last character is a null value (not required)
    // output_file[dot_location+8] = '\0';

    // do something with the output file...
    printf("New File Created: [%s]\n", output_file);

}

You could use std::string for this usage, just use str.c_str() .您可以将 std::string 用于此用法,只需使用str.c_str()

But to answer your question about C coding.但是要回答你关于 C 编码的问题。 I do something like this :我做这样的事情:

// find extension position
int ext_pos = strrchr(filename, '.') - filename;
// allocate new string
char newname[strlen(filename) + 4 + 1]; // +1 for null byte

strncpy(newname, filename, ext_pos); // copy first part
strncpy(&newname[ext_pos], "_out", 4); // write _out
strcpy(&newname[ext_pos + 4], &filename[ext_pos]); // copy extension and add null byte

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM