写入文件时出现问题

| 伙计们,我编写了以下代码,以在C ++中实现电话簿 我正在做的是首先从包含名称,地址和电话号码的三个文件中输入(您可能看不到整个代码),只看底部 现在,我给用户添加要在运行时添加的联系人,并且这些值存储在一个类中。 现在,我删除了包含名称,地址和数字的文件,并使用ofstream将新数据写入其中,当用户再次运行“电话簿”时将检索这些数据。 但是,程序运行一次后,我看不到文件的任何输入,并且在运行时添加了一些值。 有人能帮我吗? 提前致谢
    #include<iostream>//Include Header Files   
    #include<cstdlib>
    #include<fstream>
    #include<string>
    using namespace std;

    class contact{

    public:

         string name;//ALL CLASS VARIABLES ARE PUBLIC

         int phonenumber;

         string address;

         contact(){//Constructor

         name= \"Noname\";

         phonenumber= 0;

         address= \"Noaddress\";

    }


    };

    int main(){

         contact *d;

         d= new contact[200];

         string name,add;

         int choice,modchoice,k=0;//Variable for switch statement

         int phno,phno1;

         int i=0;
         int initsize=0, i1=0;//i is declared as a static int variable

         bool flag=false,flag_no_blank=false;


         //TAKE DATA FROM FILES.....
         //We create 3 files names, phone numbers, Address and then abstract the data from these files first!

         fstream f1;
         fstream f2;
         fstream f3;

         string file_input_name; 
         string file_input_address;

         int file_input_number;


         f1.open(\"./names\");

         while(f1>>file_input_name){

              d[i].name=file_input_name;

              i++;

         }
         initsize=i;

         f2.open(\"./numbers\");

         while(f2>>file_input_number){

              d[i1].phonenumber=file_input_number;
              i1++;

         }

         i1=0;


         f3.open(\"./address\");

         while(f3>>file_input_address){

              d[i1].address=file_input_address;

              i1++;

         }





         cout<<\"\\tWelcome to the phone Directory\\n\";//Welcome Message
         do{    
              //do-While Loop Starts
              cout<<\"Select :\\n1.Add New Contact\\n2.Update Existing Contact\\n3.Display All Contacts\\n4.Search for a Contact\\n5.Delete a  Contact\\n6.Exit PhoneBook\\n\\n\\n\";//Display all options


              cin>>choice;//Input Choice from user



              switch(choice){//Switch Loop Starts

              case 1:{

                   i++;//increment i so that values are now taken from the program and stored as different variables 
                   i1++;

               do{

                        cout<<\"\\nEnter The Name\\n\";

                cin>>name;

                if(name==\" \"){cout<<\"Blank Entries are not allowed\";
                                 flag_no_blank=true;

                }

               }while(flag_no_blank==true);


                   flag_no_blank=false;



               d[i].name=name;

               cout<<\"\\nEnter the Phone Number\\n\";

                   cin>>phno;

                   d[i1].phonenumber=phno;



               cout<<\"\\nEnter the address\\n\";


                   cin>>add;

                   d[i1].address=add;

               i1++;

                   i++;

                   break;//Exit Case 1 to the main menu
              }

              case 2:   {

                   cout<<\"\\nEnter the name\\n\";//Here it is assumed that no two contacts can have same contact number or address but may have the same name.

               cin>>name;

               int k=0,val;

               cout<<\"\\n\\nSearching.........\\n\\n\";

               for(int j=0;j<=i;j++){


                    if(d[j].name==name){

                     k++;           

                     cout<<k<<\".\\t\"<<d[j].name<<\"\\t\"<<d[j].phonenumber<<\"\\t\"<<d[j].address<<\"\\n\\n\";

                     val=j;                 

                     }

                 }


                 char ch;

                 cout<<\"\\nTotal of \"<<k<<\" Entries were found....Do you wish to edit?\\n\";

                 string staticname;

                 staticname=d[val].name;

                 cin>>ch;

                 if(ch==\'y\'|| ch==\'Y\'){

                      cout<<\"Which entry do you wish to modify ?(enter the old telephone number)\\n\";

                      cin>>phno;

                  for(int j=0;j<=i;j++){

                       if(d[j].phonenumber==phno && staticname==d[j].name){

                            cout<<\"Do you wish to change the name?\\n\";

                        cin>>ch;

                            if(ch==\'y\'||ch==\'Y\'){

                             cout<<\"Enter new name\\n\";

                         cin>>name;

                         d[j].name=name;

                        }

                        cout<<\"Do you wish to change the number?\\n\";

                            cin>>ch;

                        if(ch==\'y\'||ch==\'Y\'){

                             cout<<\"Enter the new number\\n\";

                         cin>>phno1;

                         d[j].phonenumber=phno1;

                         }

                                     cout<<\"Do you wish to change the address?\\n\";

                         cin>>ch;

                         if(ch==\'y\'||ch==\'Y\'){

                              cout<<\"Enter the new address\\n\";

                          cin>>add;

                          d[j].address=add;

                         }

                        }               

                   }

                      }

                      break;

                 }

             case 3 : {

                  cout<<\"\\n\\tContents of PhoneBook:\\n\\n\\tNames\\tNumbers\\tAddresses\\n\";

                  for(int t=0;t<=i;t++){

                       if(d[t].name==\"Noname\") continue;

                       cout<<\".\\t\"<<d[t].name<<\"\\t\"<<d[t].phonenumber<<\"\\t\"<<d[t].address<<\"\\n\";

                  }

                      cout<<\"\\n\\n\\n\\n\";

                  break;

                 }

             case 4:{
                  cout<<\"Enter a name to search\\n\";

                  cin>>name;

                      cout<<\"\\n\\nSearching.........\\n\\n\";

                  for(int j=0;j<=i;j++){

                      if(d[j].name==name){

                       k++;         

                   cout<<k<<\".\\t\"<<d[j].name<<\"\\t\"<<d[j].phonenumber<<\"\\t\"<<d[j].address<<\"\\n\\n\";

                   int val=j;                   

                  }

             }

             cout<<\"\\nA total of \"<<k<<\" contact names were found having the name\"<<name;

             break;
            }
            case 6:{

                 cout<<\"\\n\\nClosing the phonebook...Visit Again\\n\";

             flag=true;

                 break;

            }

            case 5: {

                 cout<<\"\\nEnter the contact-name\\n\";//Here it is assumed that no two contacts can have same contact number or address but may have the same name.

             cin>>name;

             int k=0,val;

             cout<<\"\\n\\nSearching.........\\n\\n\";

             for(int j=0;j<=i;j++){

                      if(d[j].name==name){
                       k++;         

                   cout<<k<<\".\\t\"<<d[j].name<<\"\\t\"<<d[j].phonenumber<<\"\\t\"<<d[j].address<<\"\\n\\n\";
                                                                                      val=j;                    

                  }

             }


             char ch;

             cout<<\"\\nTotal of \"<<k<<\" Entries were found....Do you wish to delete?\\n\";

             if(k==0) break;

             string staticname;

             staticname=d[val].name;

             cin>>ch;

             if(ch==\'y\'|| ch==\'Y\'){

                  cout<<\"Which entry do you wish to delete ?(enter the old telephone number)\\n\";

                  cin>>phno;

                  for(int j=0;j<=i;j++){

                       if(d[j].phonenumber==phno && staticname==d[j].name){

                                val=j;                  

                   }

                      }

                      for(int j=val;j<=i-1;j++){

                           d[j].name=d[j+1].name;


                           d[j].phonenumber=d[j+1].phonenumber;


                           d[j].address=d[j+1].address;

                      }

                      d[i].name=\"Noname\";


                      d[i].phonenumber=0;


                      d[i].address=\"Noaddress\";

                 }


                 break;

            }

       }
  }


  while(flag==false);

  std::ofstream f4(\"./names\");

  f4.close();

  std::ofstream f5(\"./numbers\");

  f5.close();

  std::ofstream f6(\"./address\");

  f6.close();

  f1.close();

  f2.close();

  f3.close();

  ofstream f7,f8,f9;

  f7.open(\"names\");

  f8.open(\"numbers\");

  f9.open(\"address\");
  int y=0;

  string w;

  w=d[0].name;

  while(f7<<w && y<=i){

  if(w==\"Noname\") y++; continue;

  y++;

  w=d[y].name;

  }

  y=0;
  int v;
  v=d[0].phonenumber;
  while(f8<<v && y<=i){
       if(v==0){y++; continue;}
       y++;
       v=d[y].phonenumber;
  }

  y=0;

  string u;
  u=d[0].address;

  while(f9<<u && y<=i ){

       if(u==\"Noaddress\"){

            continue;
            y++;
       }

       y++;

       u=d[y].address;

  }

  return 0;

  }     
    
已邀请:
C ++是否会自动处理I / O错误?心理上?如果不是,那么您的错误返回处理程序在哪里?就像阿斯金\'。 根据OP的评论进行编辑:是的,我知道,但是通常如果没有将数据写入文件,文件系统会返回错误代码,尝试告诉您为什么未写入文件。但是您决定忽略文件系统必须说的内容。我希望可以给您提示的问题应该是: \“如果您不检查文件写入调用返回码,则将很费劲,很长时间尝试调试程序。请检查这些错误返回码并告诉我们它们是什么。这是标准且必需的毕竟,如果您没有遵循标准的编程实践,那么您唯一的希望就是向拐角处的吉普赛算命先生咨询。 \“ SO:每个文件I / O调用返回的代码是什么?\” 试试看,告诉我们更多。还要感谢你的反对:我需要那个。     
顺便说一句
while(f7<<w && y<=i){
    if(w==\"Noname\") 
        y++;          // <- proper indention is king
    continue;
    y++;              // <- never reached
    w=d[y].name;      // <- never reached
}

while(f8<<v && y<=i){
    if(v==0) {
        y++; 
        continue;
    }
    y++;
    v=d[y].phonenumber;
}

while(f9<<u && y<=i ){
    if(u==\"Noaddress\") {
        continue;
        y++;         // <- never reached
    }
    y++;
    u=d[y].address;
}
    
我可以理解为什么您感到沮丧,编码员。这是令人沮丧的代码。   while(flag == false)(根据乔纳森斯的评论,这并没有看起来那么公然。仍然很难从其他混乱情况中判断出真正的垃圾……:) WTF?只需不要按“ 6”退出,否则您将免费油炸CPU   无论如何,它仍在燃烧100%的CPU并以危险的速度填充/ tmp。我猜这不是无限循环(因为一旦文件系统已满,它就会退出)。谢谢上帝,/ tmp在tmpfs上(大小为4g),我有8g可用的ram :) 将名称,数字和地址存储在单独的文件中... hm可能是个好主意。 我进行了一次测试,将3.7G写入地址:)(\“ NoaddressNoaddressNoaddressNoaddressNo .... \”)使其崩溃非常好,因为在加载时,用于接收地址的缓冲区(显然称为
d
)正好是200条记录。 坦白说,此代码应移至TheDailyWTF。 Pronto Pronto! 此代码无法修复。期     
该程序有很多问题。让我们尝试修复一些问题,然后再以此为基础。 首先,名字是什么样的?它是否有空格,例如\“ Fred Jones \”或逗号,例如\“ Jones,Fred \”。也许很多部分都像“ Chan Kong Sang”。 如果允许空格,则不能使用\“ << \”读入。也许您可以在每行输入一个名称,然后使用getline()读取它。 尝试编写一个仅读取名称并将其存储的小程序。您可以使用编辑器创建文件。一旦成功,我们就可以在此基础上继续前进。     

要回复问题请先登录注册