写入文件时出现问题

problem in writing to a file

本文关键字:问题 文件      更新时间:2023-10-16

伙计们,我用c++编写了下面的代码来实现一个电话簿我所做的是首先从包含姓名,地址和电话号码的三个文件中获取输入(您可能不会查看整个代码),只需查看底部

现在我让用户在运行时添加联系人,这些值存储在一个类中。现在我擦除包含姓名、地址和号码的文件,并使用ofstream将新数据写入其中,这些数据将在用户再次运行"phonebook"

时检索到。

然而,我不能看到任何输入到文件后,程序已经运行一次,我在运行时添加了一些值。有人能帮帮我吗?提前感谢

    #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 Directoryn";//Welcome Message
         do{    
              //do-While Loop Starts
              cout<<"Select :n1.Add New Contactn2.Update Existing Contactn3.Display All Contactsn4.Search for a Contactn5.Delete a  Contactn6.Exit PhoneBooknnn";//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 Namen";
                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 Numbern";
                   cin>>phno;
                   d[i1].phonenumber=phno;

               cout<<"nEnter the addressn";

                   cin>>add;
                   d[i1].address=add;
               i1++;
                   i++;
                   break;//Exit Case 1 to the main menu
              }
              case 2:   {
                   cout<<"nEnter the namen";//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<<"nnSearching.........nn";
               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<<"nn";
                     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 namen";
                         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 numbern";
                         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 addressn";
                          cin>>add;
                          d[j].address=add;
                         }
                        }               
                   }
                      }
                      break;
                 }
             case 3 : {
                  cout<<"ntContents of PhoneBook:nntNamestNumberstAddressesn";
                  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<<"nnnn";
                  break;
                 }
             case 4:{
                  cout<<"Enter a name to searchn";
                  cin>>name;
                      cout<<"nnSearching.........nn";
                  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<<"nn";
                   int val=j;                   
                  }
             }
             cout<<"nA total of "<<k<<" contact names were found having the name"<<name;
             break;
            }
            case 6:{
                 cout<<"nnClosing the phonebook...Visit Againn";
             flag=true;
                 break;
            }
            case 5: {
                 cout<<"nEnter the contact-namen";//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<<"nnSearching.........nn";
             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<<"nn";
                                                                                      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错误?精神上的?如果不是,那么错误返回处理程序在哪里?只是askin"。

编辑响应OP的评论:是的,我理解,但通常如果数据没有被写入文件,文件系统返回一个错误代码,试图告诉你为什么它没有被写入。但是您决定忽略文件系统必须显示的内容。我的问题应该是:

"如果你不检查file-write-call返回代码,你将有一个很好的,长时间的尝试调试你的程序。请检查这些错误返回码,并告诉我们它们是什么。这是标准的和必需的编程实践,毕竟,如果您不遵循标准的、必需的实践,那么您唯一的希望就是咨询角落里的吉普赛人算命师。

"SO:每个文件i/O调用返回的代码是什么?"

试试,告诉我们更多。谢谢你的点赞:我需要这个

btw。

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)(根据jonathan的评论,这并不像看起来那么明显。糟糕的是,甚至很难从剩下的烂摊子中判断出真正的垃圾……:)

WTF ?只是不要按'6'退出,否则你会免费消耗你的CPU

它仍然在以危险的速度燃烧100%的CPU和填充/tmp。我猜,它不称为无限循环(因为一旦文件系统满了,它就会退出)。感谢上帝,/tmp是在tmpfs上(大小为4g),我有8g的ram可用:)

将名称,号码和地址存储在单独的文件中…嗯,也许是个好主意。

一个测试运行,我让它写3.7G地址:)("noaddressnoaddressnoaddressnoaddressnoaddressno ....")只是为了使它崩溃,因为在负载时,接收地址的缓冲区(显然称为d)恰好是200条记录。

坦率地说,这段代码应该移到TheDailyWTF。Pronto立刻!

此代码无法修复。期

这个程序有很多问题。让我们先解决几个问题,然后在此基础上继续发展。

首先,名字看起来像什么?它是否有空格,比如"Fred Jones",或者逗号,比如"Jones,Fred"。也许很多地方像"陈空生"。

如果允许使用空格,则不能用"<<"将其读入。也许你可以每行放一个名字,然后用getline()读入。

试着写一个小程序,只读取名字并存储它们。您可以使用编辑器创建该文件。一旦成功了,我们就可以在此基础上进行构建。