Problem with reading CSV File

Hello everyone!

I have a small problem, and I would really appreciate any help!
What I am trying to do is a small program that creates accounts (User name, age,...), and stores it on a CSV file. And later I can search for a user and edit some information about him (example, his age).

Here is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#include <fstream>
#include <iostream>
#include <string>
#include <stdio.h>
#include <io.h>
#include <stdlib.h>
#include <string>
#include <cmath>
#include <cstdlib>



using namespace std;

void insert();
void searchaccount();
void testsearch();
void searchaccountold();

int main()
{
// A SMALL MENU!
    
    system ("title Test App");
    system ("cls");
    system ("color 0F"); 
    printf(">> Welcome to the new Test App << \n");
    printf(" \n");
    int choice;
    printf("1) Enter 1 to create a new account \n");
    printf("2) Enter 2 to see ALL the accounts \n");
    printf("3) Test of the search option with user name \n");    
    printf("4) Enter 4 to use the old TXT search \n");    
    printf(" \n");
    printf(" \n");
    
    cin >> choice;
    
switch (choice){
       case 1:
            system ("cls");
            insert();
            break;
       case 2:
            searchaccount();
            break;  
       case 3:
            testsearch();
            break;
        case 4:
            searchaccountold();
            break;  
            
       cin.get();
       }
    
    
}


//THIS WILL CREATE A NEW ACCOUNT (And also create the CSV file if it doesnt exist)
void insert()
{
  FILE *fp;
  char s[80];
  char name[80];
  
  int t;
  int age;


  if((fp=fopen("test.csv", "a+")) == NULL) {
    printf("Cannot open file.\n");
    exit(1);
  }

  printf("Enter The name of the client: ");
  fscanf(stdin, "%s", name); /* cin */
  fprintf(fp, "%s ;", name); /* puts this in the file*/
  
  
    printf("Enter The age of the client: ");
  fscanf(stdin, "%d", &age); 
  fprintf(fp, "%d\n", age); 
  
  
  fclose(fp); 

main();
}
//------------------------


//THIS WILL SHOW ALL THE INFO(text, numbers, etc) THAT IS IN THE FILE
void searchaccount(){
     
 string line;
  ifstream file ("test.csv");
  if (file.is_open())
  {
    while ( file.good() )
    {
      getline (file,line);
      cout << line << endl;
    }
    
    system ("pause");
       cin.get();
       
    file.close();
  }

  else cout << "Unable to open file"; 
 
     
     
}
//----------------------------------


//THIS IS A SMALL TEST USING THE CSV FILE TO SEARCH FOR A SPESIFIC USER
void testsearch(){
     
 string line;
 string str;
 string name;
 int age;
  ifstream myfile ("test.csv");
  
    cout << "Enter the users name:";
     cin >> str;
     
  if (myfile.is_open())
  {
   cout << "Entered the IF ";
    while ( myfile.good() )
    {
    cout << "Entered the WHILE";
      getline (myfile,line);
      
      while (myfile >> name >> age) 
     {
           if (str == name){
             cout << line << endl;
      }
      }
      }
    }
    
    system ("pause");
       cin.get();
       
    myfile.close();
  }

 
     
//-----------------------------

//THIS IS AN OLD PART OF CODE FROM MY PREVIOUS ATTEMPT 
//USING A .TXT FILE. HERE I AM ABLE TO ACTUALY FIND THE 
//USER AND DISPLAY IT ON THE SCREEN
void searchaccountold()
{

ifstream myfile("test.csv");
string name;
string str;

int age;
int money;
system("CLS");
     cout << "Enter the user name:";
     cin >> str; //This will have the name of the user I want to find
     
   
     while (myfile >> name >> age >> money) 
     {
           cout << "you entered the while\n" << endl;
           if (str == name){
                   
                   system("CLS");
                   cout << "User found!" << endl;
                   cout << "Name" << "  " << "Age" << "  " << "Money" << endl;
                   cout << "---------------------" << endl;
                   cout << name << "  " << age << "  " << "$" << money << endl;
                         system ("pause");
                         cin.get();
                         main();
           }
                        if(!(str == name))
                        {
                                 system("cls");
                                 cout << "No user found!" << endl;
                        }
                                 
     }
           system ("pause");
           cin.get();
           main();
                             
}



Looking at the "old code" ("void searchaccountold()"), I am sure the problem is on the "while" part ("while (myfile >> name >> age >> money)").
It should work, but it doesnt. I am almost sure the problem lies on the ";"... Because the CSV uses cells, and to pass to the second cell I would normaly need an ";" and in my case I have ">>". I have tryed to change them, to use getline, etc... and didnt work :(


Can someone help me out?
Can anyone help? :(
Yes, I suggest that you use getline() in order to read an entire line on line 177. Then use stringstream

http://www.cplusplus.com/reference/sstream/stringstream/?kw=stringstream

initialize it with that previously read line and use getline(..., ';') now for the fields
Hi coder777, thank you so much for your reply.

I tryed what you told me, and it "kinda" worked... I am sure I am doing something totally stupid... here is what I am geting now:

When I have while (getline(myfile, name, ';')) the program works, but it only gives me back the first person on my CSV file. Example, if I search for "Tom" it will give me "Tom" and then random numbers(since I dint declared "age" and "money" on the "while"). If I search for "Bob", it says he dint found anyone with that name.

When I use this code while (getline(myfile, name, age, money, ';')) , it says this on the compiler "no matching function for call to `getline(std::ifstream&, std::string&, int&, int&, char)'". I even tryed doing with this while (getline(myfile, name,';', age,';', money, ';')), and nothing. I know I am doing something really stupid, but I cant find the solution. Can you give me another hint please :) ?


This is what my CSV file looks like(";" means going to a different cell):
TOM ; 25 ; 5000
Bob ; 31 ; 7500
Anna ; 63 ; 1200

Thank you!
don't use ';' for the line. What I thought of was this:
1
2
3
4
5
6
7
     std::string line; 
     while (std::getline(myfile, line)) 
     {
           std::stringstream ss(line); // feed the stringstream with the line
           if(getline(ss, name, ';') && getline(ss, age, ';') && ...) // retrieve the data from that line
           ...
           cout << "you entered the while\n" << endl;
it says this on the compiler "no matching function for call to `getline(std::ifstream&, std::string&, int&, int&, char)'"

This is because getline is either
istream& getline (istream& is, string& str, char delim);
or
istream& getline (istream& is, string& str);
it extracts from is and stores in str until the delimiter is reached, or in the case of the second one until the newline character '\n' is reached. Basically the compiler is complaining because getline wasn't designed to take the input parameters that you supplied it. Follow coder777's advice and have your while take std::getline(myfile, line). That way, it will execute as long as there is another line to get, and then deal with what's actually in that line in the body of the while loop.
Topic archived. No new replies allowed.