我很驚訝,在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