首页 \ 问答 \ 奇怪的ifstream行为(Weird ifstream behaviour)

奇怪的ifstream行为(Weird ifstream behaviour)

编写代码将对象写入文件,读取它们并在屏幕上打印

#include<iostream>
#include<fstream>
#include<cstring>
using namespace std;
////////////////////////////////////////////////////////////
class employee{
    private:
        string name;
        unsigned long ID;
    public:
        employee():name(""),ID(0){}
        void putdata(){cout<<"Enter employee's name: ";cin>>name;cout<<"Enter employee's ID: ";cin>>ID;}
        void getdata(){cout<<"Employee's name is: "<<name<<endl;cout<<"Employee's ID is: "<<ID<<endl;}
        friend ostream& operator << (ostream&,employee&);
        friend istream& operator >> (istream&,employee&);
};

ostream& operator << (ostream& f,employee& emp){ // запись объекта в файл
    f<<emp.ID<<"-"<<emp.name<<"?";
    return f;
}

istream& operator >> (istream& f,employee& empl){ // чтение объекта из файла
    char dummy;
    f>>empl.ID>>dummy;
    f>>dummy;
    empl.name="";
    while(dummy!='?'){
        empl.name+=dummy;
        f>>dummy;       
    }
}
////////////////////////////////////////////////////////////
int main(){
    char ch='y';
    ofstream file1;
    ifstream file2;
    file1.open("TAB.txt");
    employee one;
    while(ch=='y'){ // цикл для записи
        one.putdata();
        file1<<one; // write data into file
        cout<<"Go on?(y,n): ";cin>>ch;
    }
    file1.close();
    file2.open("TAB.txt");
    while(file2.good()){ // цикл для чтения из файла
        file2>>one;
        one.getdata();      
    }
    file2.close();
    system("pause");
    return 0;
}

输入对象后,程序将它们打印出来并挂起。 我想while(file2.good())有问题,但我不确定。 谁有人评论?


Writing code to write object into a file, read them, and print on the screen

#include<iostream>
#include<fstream>
#include<cstring>
using namespace std;
////////////////////////////////////////////////////////////
class employee{
    private:
        string name;
        unsigned long ID;
    public:
        employee():name(""),ID(0){}
        void putdata(){cout<<"Enter employee's name: ";cin>>name;cout<<"Enter employee's ID: ";cin>>ID;}
        void getdata(){cout<<"Employee's name is: "<<name<<endl;cout<<"Employee's ID is: "<<ID<<endl;}
        friend ostream& operator << (ostream&,employee&);
        friend istream& operator >> (istream&,employee&);
};

ostream& operator << (ostream& f,employee& emp){ // запись объекта в файл
    f<<emp.ID<<"-"<<emp.name<<"?";
    return f;
}

istream& operator >> (istream& f,employee& empl){ // чтение объекта из файла
    char dummy;
    f>>empl.ID>>dummy;
    f>>dummy;
    empl.name="";
    while(dummy!='?'){
        empl.name+=dummy;
        f>>dummy;       
    }
}
////////////////////////////////////////////////////////////
int main(){
    char ch='y';
    ofstream file1;
    ifstream file2;
    file1.open("TAB.txt");
    employee one;
    while(ch=='y'){ // цикл для записи
        one.putdata();
        file1<<one; // write data into file
        cout<<"Go on?(y,n): ";cin>>ch;
    }
    file1.close();
    file2.open("TAB.txt");
    while(file2.good()){ // цикл для чтения из файла
        file2>>one;
        one.getdata();      
    }
    file2.close();
    system("pause");
    return 0;
}

After entering objects, th program print them out and hanging. I guess there is a problem with while(file2.good()) but I'm not sure. Can anyone comment?


原文:https://stackoverflow.com/questions/36036223
更新时间:2022-08-21 08:08

最满意答案

我知道这已经晚了但可能对某人有帮助。 实现这一目标的最佳方法是:

  • 用于显示常规连接设置,

    startActivityForResult(new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS), 0);

  • 用于显示移动网络连接设置

    startActivityForResult(new Intent(android.provider.Settings.ACTION_DATA_ROAMING_SETTINGS), 0);

  • 用于显示Wifi设置

    startActivityForResult(new Intent(android.provider.Settings.ACTION_WIFI_SETTINGS), 0);


I know this is late but might help someone. The best way to achieve this is :

  • for showing general connectivity settings,

    startActivityForResult(new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS), 0);

  • for showing Mobile network connection settings

    startActivityForResult(new Intent(android.provider.Settings.ACTION_DATA_ROAMING_SETTINGS), 0);

  • for showing Wifi settings

    startActivityForResult(new Intent(android.provider.Settings.ACTION_WIFI_SETTINGS), 0);

相关问答

更多
  • 启动设置意图: startActivity(new Intent(Settings.ACTION_SETTINGS)); 并在onResume方法中获取当前活动: public void onResume(){ super.onResume(); //Do your work } 从设置屏幕退出后,您的onResume方法将被调用,在这里您可以获取您的位置。 Launch the setting intent: startActivity(new Intent(Settings.ACTION_SETTI ...
  • 没有必要手动创建和格式化ListView - 有一些方法可以创建“首选项”屏幕。 最明显的是PreferenceActivity 。 您可以在XML文件中定义所有首选项,有点像布局,然后使用addPreferencesFromResource()加载它们,也可以在代码中创建一些PreferenceScreen对象,并以Preference对象填充它们。 最好的办法是查看Android API提供的API Demos应用程序。 这包含许多管理偏好的好例子。 以下是从代码创建偏好设置的方法 ,这里是示例首选项X ...
  • 如果我正确理解了你的问题:“启动”你的AVD,之后会出现“启动选项”,然后切换复选框“将显示缩放到实际大小”,设置所需的设置,最后“启动”你的AVD。 If I understood your question correctly: "Start" your AVD, after that "Launch Options" will appear, then toggle checkbox "Scale display to real size", set up needed settings, and f ...
  • 我知道这已经晚了但可能对某人有帮助。 实现这一目标的最佳方法是: 用于显示常规连接设置, startActivityForResult(new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS), 0); 用于显示移动网络连接设置 startActivityForResult(new Intent(android.provider.Settings.ACTION_DATA_ROAMING_SETTINGS), 0); 用于显示Wifi设置 s ...
  • 尝试使用此代码。 它打开日期和时间设置屏幕。 startActivityForResult(new Intent(android.provider.Settings.ACTION_DATE_SETTINGS), 0); Try using this code. It opens the date and time setting screen . startActivityForResult(new Intent(android.provider.Settings.ACTION_DATE_SETTINGS ...
  • 我最终弄清楚了: adb -s $SERIAL shell rm -f /data/system/locksettings.db* 这将删除任何锁定屏幕,在系统设置中将其设置为“无”。 I eventually figured it out: adb -s $SERIAL shell rm -f /data/system/locksettings.db* This will remove any lockscreen, setting it to "None" in the system setting ...
  • 由于您有自定义ROM,自然,您的设置应用程序也是为它定制的。 您可以通过在意图中传递一些值来启动“设置”应用,从而实现您想要的效果。 这些值应该是什么,可以通过查看ROM上的Settings应用程序的源代码来确定。 As you have a custom ROM, naturally, your Settings app is also custom-made for it. It's likely that you can do what you want by passing some values ...
  • 您必须查询CalendarProvider (自API级别14以来可用)的日历表以获取可用日历。 以下代码段将向您展示如何: final String[] EVENT_PROJECTION = new String[]{ CalendarContract.Calendars._ID, CalendarContract.Calendars.CALENDAR_DISPLAY_NAME, CalendarContract.Calendars.CALENDAR_COL ...
  • 是的,因为它不能立即识别您的图像。需要一些时间。 直到那个时候它显示白色屏幕。 我会告诉你如何使用和避免这个问题。 将图像设置为样式并将其用作主题。