ネットで見つけたコードなのですが、このコードの意図はnull文字を後ろにいれてしまい、strlenとsizeが9,10となるとサイトにはあったのですが、僕が実行すると10,10のなってしまいます。
example.bin には123456 89と打ってあるだけです。
ご指摘よろしくお願いします
コード ifstream ifs("example.bin"); if (ifs.fail()) { cerr << "失敗" << endl; return -1; } int begin = static_cast<int>(ifs.tellg()); ifs.seekg(0, ifs.end); int end = static_cast<int>(ifs.tellg()); int size = end - begin; ifs.clear(); //ここでclearしてEOFフラグを消す ifs.seekg(0, ifs.beg); char *str = new char[size + 1]; str[size] = '\0'; ifs.read(str, size); cout << "[" << str << "]" << endl; cout << "size: " << size << " strlen:" << strlen(str) << endl; delete[] str; return 0; }
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2015/11/21 19:04
2015/11/22 01:35
2015/11/22 12:04