在C ++中写入csv文件后读取csv文件时出现问题

时间:2011-01-04 21:12:42

标签: c++ csv

我对C ++很陌生,我正在尝试实现一个任务,我可以从csv文件读取并写入相同的文件,反之亦然,具体取决于用户的选择。这就是我所做的:

以下是已存档的数据

1,米基,23090,吉尔尼,TRNC,465

2,查尔斯,23090,吉尔尼,TRNC,465

3,种类,23090,吉尔尼,TRNC,465

4,穆迪,23090,吉尔尼,TRNC,465

5,Kpokiyo,23090,吉尔尼,TRNC,465

6,Sualp,23090,吉尔尼,TRNC,465

这是我到目前为止编写的代码

#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;
    }

当我选择搜索用户(选择4)时似乎工作得很好。然而,问题是当我尝试写入文件时,它的工作原理,但当我尝试执行任何其他任务,如再次搜索用户或重新写入文件时,程序崩溃。 请问我做错了什么。

由于

1 个答案:

答案 0 :(得分:1)

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