#include<iostream.h>
#include<fstream.h>
#include<string.h>
int n = 0, flag = 0,i;
struct bac
{
char name[10];
char amt[5];
} s;
void main()
{
ofstream f("C:\\TC\\1.dat");
for(i=0;i<10;i++)
{
cout << "\nenter the details ";
cin >> s.name >> s.amt;
f.write((char *)&s, sizeof(bac));
}
}
有時代碼工作正常 但在其他時候,當我看到在輸出文件中,它是空的,問題出現了很多次,我螞蟻知道是否有關於文件處理循環的預防措施
例如,在其他程序
.....
while(ch!=4)
{
cout << "\nBANK MANAGEMENT SYSTEM \n";
cout << "enter choice ";
cout << "\n1.add\n2.search\n3.delete and overwrite ";
cin >> ch;
if (ch == 1)
{
cout << "\nenter the details ";
cin >> s.name >> s.amt;
f.write((char *)&s, sizeof(bac));
}
.....
文件是空
如果它接受'iostream.h',你就有一個非常過時的編譯器。使用'',使用'',並使用''(除非你真的要用C字符串函數)。另外,使用'int main'。符合的實現只接受帶'int'返回類型的'main'。除了C風格的文件io,沒有理由將您的對象創建爲全局對象。保持在main()的本地。只要任何用戶決定輸入9或4個以上的字符,您的輸入也會失敗。 'std :: string'可以毫不費力地解決這個問題。 –
chris
2013-03-17 14:56:25
我同意,但它的編譯和主要問題是什麼可能導致這樣不同結果的問題? 偶數代碼#1使用相同的 *** iostream。h ***和*** void main()***但作品 – 2013-03-17 15:02:44
它僅適用於您的編譯器不符合要求並且您使用非標準標頭。如果您希望自己的代碼與其他人的C++實現一起工作,那麼使用標準C++非常重要,所有符合實現的實現都一致。 – chris 2013-03-17 15:10:20