我很惊讶,在C++中没有内置的方法来投射字符串和字符。这是我的问题:我要更新一个文本文件,这样一行:字符串和字符不兼容
#include <stdlib.h>
#include <fstream>
#include <windows.h>
#include <iostream>
#include <stdio.h>
using namespace std;
int main(void) {
char l1 [12];
char l2 [15];
char l3 [15];
char l4 [15];
char md [16];
FILE * myfile2 = fopen("state.txt", "r+");
fgets(l1,12,myfile2);
fgets(l2,12,myfile2);
fgets(l3,12,myfile2);
fgets(l4,12,myfile2);
fseek (myfile2 , 0 , SEEK_SET);
strcpy(md , "detectoroff");
fputs (md,myfile2);
strcpy(md,l2);
fputs (md,myfile2);
strcpy(md,l3);
fputs (md,myfile2);
strcpy(md,l4);
fputs (md,myfile2);
Sleep(1000);
fclose(myfile2);
return EXIT_SUCCESS;
}
Inicially文本文件具有
a
b
c
d
时运行的代码的输出是
detectoroffb
c
d
和如果我更换线
strcpy(md , "detectoroff");
这个
strcpy(md , "detectoroff\n");
现在的结果是正确的,但如果我运行此两次,将出现一个空行下的第一个,另一个,如果是跑三圈等我怎样才能解决这个问题?
无关使用C++和C++ 11(删除此标记)。这个代码是纯粹的C,除了无用的C++包含。 –
这与铸造字符串和字符有什么关系?它与'std :: string'有什么关系? – Barmar
也许我应该使用“转换”而不是“转换”这个词,将字符串转换为字符是我进一步需要的。 – JoeCoolman