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

发布于 2024-10-10 05:19:28 字数 5945 浏览 1 评论 0原文

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

这是文件中已有的数据

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

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

#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)时,似乎效果很好。但是,问题是当我尝试写入文件时,它可以工作,但是当我尝试执行任何其他任务(例如再次搜索用户或重新写入文件)时,程序崩溃。 请问我做错了什么。

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

千柳 2024-10-17 05:19:28

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

Your emp string array in the main function is declared with the initial size of the file and never resized when you add more entries, hence the crash when it tries to read a bigger file into the old size of array.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文