从char *读取数据并转换为char []

时间:2015-02-09 00:54:46

标签: c++ char

我觉得很奇怪,因为问题似乎微不足道。抱歉,但在检查各种StackOverflow问题后,我无法使其工作。

想法是:

  1. 我有一个* .txt配置文件,用户可以在其中更改一些参数。其中一个是“FILE_PATH”,用户必须在其中写入要保存文件的位置。
  2. 因为我不知道将有多少字符写入文件路径。
  3. 我设法将字符保存在char *变量中并且它有效(printf在%s中显示正确的目录)

    if (!strcmp(tmp_str, "FILE_PATH")) {
        audioFileConfig.filePath = token;
        printf("RECORDING_FILEPATH = %s\n", audioFileConfig.filePath);
    }
    
  4. 我尝试从代码中访问audioFileConfig.filePath并从中创建整个文件名:

    // getting time stamp
    char* recStartTimeStamp = createTimeStamp();
    
    //creating file name
    char fileName[20];
    sprintf(fileName, ".audio_%d_%d.wav", (channelId +1), part);
    
    //reading filePath from config
    char path[256];
    strncpy(path, audioFileConfig.filePath, 256);
    
    //merging chars
    char* streamFileName;
    streamFileName = new char[strlen(path) + strlen(recStartTimeStamp) + strlen(fileName) + 1];
    strcpy(streamFileName, path);
    strcat(streamFileName, recStartTimeStamp);
    strcat(streamFileName, fileName);
    
    //check what's inside
    cout<< "\nCreated file: " << streamFileName;
    
  5. 但是我在文件名的开头得到的只是随机的ASCI字符。将它与整个弦连接起来的正确方法是什么?字段文件路径为char*

0 个答案:

没有答案
相关问题