前田稔(Maeda Minoru)の超初心者のプログラム入門
string str; for(i=1; i<11; i++) { sprintf(buf,"%5d\n",i); str+= buf; } |
hFile= CreateFile("test.txt",GENERIC_WRITE,0,0,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL, NULL); if (hFile==INVALID_HANDLE_VALUE) { printf("test.txt CreateFile Error"); return 0; } |
WriteFile(hFile,str.c_str(),str.size(),&dwBytes,NULL); |
/*★ String をファイルに出力する 前田 稔 ★*/ #include <windows.h> #include <string> using namespace std; HANDLE hFile; DWORD dwBytes; int main() { string str; char buf[12]; int i; for(i=1; i<11; i++) { sprintf(buf,"%5d\n",i); str+= buf; } printf("%s\n", str.data()); hFile= CreateFile("test.txt",GENERIC_WRITE,0,0,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL, NULL); if (hFile==INVALID_HANDLE_VALUE) { printf("test.txt CreateFile Error"); return 0; } WriteFile(hFile,str.c_str(),str.size(),&dwBytes,NULL); CloseHandle(hFile); return 0; } |
string str; str.resize(1000,'.'); |
hFile = CreateFile("test.txt",GENERIC_READ,0,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL); if (hFile==INVALID_HANDLE_VALUE) { printf("test.txt CreateFile Error"); return 0; } |
ReadFile(hFile,(LPVOID)str.data(),1000,&dwBytes,NULL); |
str.resize(dwBytes); |
/*★ ファイルから String に入力する 前田 稔 ★*/ #include <windows.h> #include <string> using namespace std; HANDLE hFile; DWORD dwBytes; int main() { string str; str.resize(1000,'.'); hFile = CreateFile("test.txt",GENERIC_READ,0,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL); if (hFile==INVALID_HANDLE_VALUE) { printf("test.txt CreateFile Error"); return 0; } ReadFile(hFile,(LPVOID)str.data(),1000,&dwBytes,NULL); CloseHandle(hFile); str.resize(dwBytes); printf("LEN=%d\n%s\n",dwBytes, str.data()); return 0; } |