简体   繁体   中英

Problem with reading from csv file after writing to it in C++

I'm quite new to C++, I'm trying to achieve a task whereby i could read from a csv file and write to that same file and vice versa depending on the user's selection. This is what i did:

Here's the data already on file

1,Mickey,23,090,Girne,TRNC,465

2,Charles,23,090,Girne,TRNC,465

3,Species,23,090,Girne,TRNC,465

4,Moody,23,090,Girne,TRNC,465

5,Kpokiyo,23,090,Girne,TRNC,465

6,Sualp,23,090,Girne,TRNC,465

Here's the code i wrote so far

#include <iostream>
#include <sstream>
#include <fstream>
#include <vector>
#include <string>

    using namespace std; 

    bool checkIsFile();
    void addUser(string e[][7], int num);
    void readAllUsers(string x[][7]);
    void breakArray(string x[], string y[][7], int num);
    bool checkAccout(string x[], int y, string search);
    void findUser(string x[], string y[],string z[], string a[],string t[], 
    string c[], string d[]);
    int findRowNumber();

    int main()
    {
        int option;
        int acc;
        string emp[findRowNumber()][7];
        string account[findRowNumber()], age[findRowNumber()],name[findRowNumber()], 
        state[findRowNumber()], city[findRowNumber()], phone[findRowNumber()],
        zip[findRowNumber()];

      //infinite loop
          while(true){
          cout<<"-------------------------\n";
          cout<<"Menu Options\n";
          cout<<"1 Add account\n";
          cout<<"2 List Accounts\n";
          cout<<"3 Delete account\n";
          cout<<"4 Find user\n";
          cout<<"5 Exit program\n";
          cout<<"-------------------------\n";
          cin>>option;

              if(option==1)
              {
                 readAllUsers(emp);
                 addUser(emp, findRowNumber());
              }
              else if(option==2)
              {
                  readAllUsers(emp);
              }
              else if(option==3)
              {
                   //delete a user;
              }
              else if(option==4)
              {
                    //find user
                    readAllUsers(emp);
                    breakArray(account,emp,0);
                    breakArray(name,emp,1);
                    breakArray(age,emp,2);
                    breakArray(phone,emp,3);
                    breakArray(city,emp,4);
                    breakArray(state,emp,5);
                    breakArray(zip,emp,6);

                    findUser(account,name,age,phone,city,state,zip);
              }
              else if(option==5)
              {
                    return 0;
              }
              else
              {
                    cout<<"Invalid option\n";
              }
          }
        }

    void addUser(string e[][7], int num)
    {
         string emp[num + 1][7];

         for(int a=0;a<num;a++)
         {
            for(int b=0;b<7;b++)
            {
                 emp[a][b] = e[a][b];
                 }
                 }
        //just to test if it works 
         emp[num][0] = "10";
         emp[num][1] = "10";
         emp[num][2] = "10";
         emp[num][3] = "10";
         emp[num][4] = "10";
         emp[num][5] = "10";
         emp[num][6] = "10";

         ofstream inFile;
         inFile.open("info.csv");

         for(int a=0;a<num + 1;a++)
         {
            for(int b=0;b<7;b++)
            {
                 cout<<emp[a][b];               
            }
        }

        for(int i=0;i<num +1; i++)
        {
             inFile << emp[i][0] + "," + emp[i][1] + "," + emp[i][2] + "," + emp[i][3] + "," + emp[i][4] + "," + emp[i][5] + "," + emp[i][6]<<endl;                 
        }

        inFile.close();
    }

    void readAllUsers(string x[][7])
    {
        ifstream inFile("info.csv");
        string line;
        int linenum = 0;
        while (getline (inFile, line))
        {
            istringstream linestream(line);
            string item;
            int itemnum = 0;
            while (getline (linestream, item, ','))
            {
                x[linenum][itemnum] = item;
                itemnum++;
            }
        linenum++;
        }
        inFile.close();
   }

   void breakArray(string x[], string y[][7], int num)
   {       
        for(int i=0;i<findRowNumber();i++)
        {
            for(int j=0;j<7;j++)
            {
                 x[i] = y[i][num];
            }
        }
   }

   bool checkAccout(string x[], int y, string search)
   {
        bool check = false;
        for(int i=0;i<y;i++)
        {
            if(x[i]==search)
            {
                check = true;
            }
        }
       return check;  
   }

   void findUser(string x[], string y[],string z[], string a[], 
    string t[], string c[], string d[])
    {
       string search;
       bool check = false;
       cout<<"Enter Account Number to Search: ";
       cin>>search;

       for(int i=0;i<findRowNumber();i++)
        {
            if(x[i]==search)
            {
                cout<<"Account Number: " + x[i]<<endl<<
                "Name:\t\t" + y[i]<<endl<<
                "Age:\t\t" + z[i]<<endl<<
                "Phone:\t\t" + a[i]<<endl<<
                "City:\t\t" + t[i]<<endl<<
                "State:\t\t" + c[i]<<endl<<
                "Zip:\t\t" + d[i]<<endl;
                check = true;
            }
        }

        if(!check)
            cout<<"User does not exist"<<endl;
    }

    int findRowNumber()
    {
        ifstream inFile("info.csv");
        string line;
        int linenum = 0;
        while (getline (inFile, line))
        {
            linenum++;
        }
        inFile.close();
        return linenum;
    }

It seems to work great when i choose to search for a user(selecting 4). However, the problem is when i try to write to file, it's works but when i try to do any other task like searching the user again or re-writing to file, the program crashes. Please what i'm i doing wrong.

Thanks

主函数中的emp字符串数组以文件的初始大小声明,并且在添加更多条目时从不调整大小,因此,当它尝试将更大的文件读入数组的旧大小时会崩溃。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM