How to read,edit a csv file and continue from last log-in?


Hi im working on a program that will do several mathematical calculation and will store the data into a csv file.The one below is just a prototype version of this.In my program,I will store my csv file as (current-date).csv using time.

With this in mind,I want my program to first check does (current-date).csv already exists.If not,the program runs normally and output all the data into the csv file.
However if (current-date).csv already exists,I want my program to read and edit that particular csv file,by skipping part1 in my code(name,id N etc.) and start with prompting the user for numbers straight-away.
In other words, if (current-date).csv already exists,the user should continue outputting the numbers from where the user last left before last termination instead of outputting the name,date & etc into the csv file again.
Eg: if no=22, continue inputting values from no=23 till terminating the program again and storing the values correctly into the csv file.


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

#include <iostream>
#include <fstream>
#include <iomanip>
#include <locale>       
#include <string>        
#include <sstream> 
#include <time.h>

using namespace std;

// Program to read date N store it as currentdate.csv

string get_date();
 
 int main(){
    string date,out;
    string name,id,age;
    int no=0;
    float num1,num2,num3;
   	
    ifstream indata;
	ofstream outdata;  
          
    cout<<"enter name:";
    cin>>name;
    cout<<"enter id:";
    cin>>id;
    cout<<"Enter age:";
    cin>>age;
    

    date=get_date();//storing the date as a .csv file
    out=".csv";
    out=date+out;
 
    cout<<out<<endl;
	outdata.open(out.c_str());

    
    //outputting all the data into a csv file
    
    //Part1:
    outdata<<"Number Test\n"<<endl; 
    outdata<<"Name:"<<","<<name<<endl;
    outdata<<"Id:"<<","<<id<<endl;
    outdata<<"Age:"<<","<<age<<endl;
    outdata<<"\n"<< endl;
    outdata << "No,Num1,Num2,Product" << endl;
   //end of part1
  
  //continuously prompt the user for a number until the user press num1=-11
    while (!(num1 ==-11))
  {		
	cout<<"Enter Num1:";
	cin>>num1;
    if(num1==-11)break;     	
	cout<<"Enter Num2:";
	cin>>num2;
	num3=num1*num2;
	no++;
    outdata<<no<<","<<num1<<","<<num2<<","<<num3<< endl;  
  }
 
    
    system("pause");
    return 0;
}  
      
    string get_date()//function to converts date to string
{
   time_t now;
   char the_date[15];

   the_date[0] = '\0';

   now = time(NULL);

   if (now != -1)
   {
      strftime(the_date,15, "%d.%m.%y", gmtime(&now));
   }

   return string(the_date);
}
Three inferred questions are
1) How to check if the file exists
Ans: Easiest way is to open the file for reading, if it can be opened then it does exist

2) How to continue from last time
Ans: Read the if it exists and go up to last number input (assuming you are going to keep track of the last input number in the CSV) and start taking numbers after that

3) How to read from file
I think you already have ifstream indata; just need to use this to open file and read from it

A suggestion: You are already using C++, instead of using <time.h> use <ctime> reference here http://www.cplusplus.com/reference/chrono/
and http://www.cplusplus.com/reference/iomanip/put_time/?kw=put_time
codewalker: Thank U for ur advise so far..it did help me alot..With that,I added an if-else statement to my program.But im still stuck with the part how to go up to the last number and continue from the last input..how to keep track of the last input ?is my method correct ?

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

#include <iostream>
#include <fstream>
#include <iomanip>
#include <locale>       
#include <string>        
#include <sstream> 
#include <time.h>

using namespace std;


string get_date();
int passnum(int num);
 
 int main(){
    string date,out;
    string name,id,age;
    int no=0,store=0;
    float num1,num2,num3;
    
    date=get_date();
    out=".csv";
    out=date+out;
       	
    ifstream indata;
	ofstream outdata;  
    indata.open(out.c_str()); 
           
   if(!(indata.is_open()))//if the file can be opened,continue prompting the user for numbers
  {       
    outdata.open(out.c_str());  
    cout<<"enter name:";
    cin>>name;
    cout<<"enter id:";
    cin>>id;
    cout<<"Enter age:";
    cin>>age;
                
    //outputting all the data into a csv file
    outdata<<"Number Test\n"<<endl; 
	outdata<<"Name:"<<","<<name<<endl;
	outdata<<"Id:"<<","<<id<<endl;
	outdata<<"Age:"<<","<<age<<endl;
	outdata<<"\n"<< endl;
    outdata << "No,Num1,Num2,Product" << endl;

     while (!(num1 ==-11))
    {		
	  cout<<"Enter Num1:";
	  cin>>num1;
      if(num1==-11)
    { store=no;
      break; 
    }     	
	  cout<<"Enter Num2:";
	  cin>>num2;
	  num3=num1*num2;
	  no++;
      outdata<<no<<","<<num1<<","<<num2<<","<<num3<< endl;  
    }
   }  
     
      else
   {
      outdata.open(out.c_str());                                    
      while (!(num1 ==-11))
    {
      no=store;    		
	  cout<<"Enter Num1:";
	  cin>>num1;
      if(num1==-11)break;     	
	  cout<<"Enter Num2:";
	  cin>>num2;
	  num3=num1*num2;
	  
	  no++;
      outdata<<no<<","<<num1<<","<<num2<<","<<num3<< endl;  
     }
   }
   
    system("pause");
    return 0;
}  
      
    string get_date()//function to converts date to string
{
   time_t now;
   char the_date[15];

   the_date[0] = '\0';

   now = time(NULL);

   if (now != -1)
   {
      strftime(the_date,15, "%d.%m.%y", gmtime(&now));
   }

   return string(the_date);
}

 int passnum(int num)
{  
     return num;
}


Last edited on
Put all input data into the CSV, then put the last input number at the end of the CSV.

Read all the data that has been input then read the number on the last line, which will be the number of last input. Run loop from this number onwards till the total number of inputs

for example
- You want user to enter 10 data sets total
- User enters 5, you put these five sets in CSV, then put "5" at the last line (line 6) of the CSV
- Next time, read the five lines of data, read the number from CSV
- Run loop from 6 to 10 (5-9 indices)

codewalker: sorry i didnt get u..can u pls show it with codes ?
Ok..this code is working fine...But have one problem in it..How do I continue from the last calculated value for No..cz when I restart, No starts from 1 again...I need to keep track of no..but not sure how...Pls do help this or explain this with codes..Thank You

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
#include <iostream>
#include <fstream>
#include <iomanip>
#include <locale>       
#include <string>        
#include <sstream> 
#include <time.h>

using namespace std;


string get_date();

 int main(){
    string date,out;
    string name,id,age,x,y;
    int no=0,store=0;
    float num1,num2,num3;
    
    date=get_date();
    out=".csv";
    out=date+out;
       	
    ifstream indata;
	ofstream outdata;  
    indata.open(out.c_str()); 
           
   if(!(indata.is_open()))//if the file cannot be opened,Enter name & personal details
  {       
    outdata.open(out.c_str(), ios::app);  
    cout<<"enter name:";
    cin>>name;
    cout<<"enter id:";
    cin>>id;
    cout<<"Enter age:";
    cin>>age;
                
    //outputting all the data into a csv file
    outdata<<"Number Test\n"<<endl; 
	outdata<<"Name:"<<","<<name<<endl;
	outdata<<"Id:"<<","<<id<<endl;
	outdata<<"Age:"<<","<<age<<endl;
	outdata<<"\n"<< endl;
    outdata << "No,Num1,Num2,Product" << endl;

     while (!(num1 ==-11))
    {		
	  cout<<"Enter Num1:";
	  cin>>num1;
      if(num1==-11) 
      {
        indata.open(out.c_str());
        string cell1;
	    indata >> cell1;
        break; 
      }  	
	   cout<<"Enter Num2:";
	   cin>>num2;
	   num3=num1*num2;
	   no++;
       outdata<<no<<","<<num1<<","<<num2<<","<<num3<< endl;  
    }
   }  
     
      else //if the file can be opened,continue prompting the user for numbers
  {       
   {  
     while(!indata.eof())
     {
   	  indata >> x;
     } 
     outdata.open(out.c_str(), ios::app);                 
      while (!(num1 ==-11))
    {
       		
	  cout<<"Enter Num1:";
	  cin>>num1;
      if(num1==-11)break;     	
	  cout<<"Enter Num2:";
	  cin>>num2;
	  num3=num1*num2;	  
	  no++;
      outdata<<no<<","<<num1<<","<<num2<<","<<num3<< endl; 
      indata.open(out.c_str());
     }
   }
   
    system("pause");
    return 0;
}  
      
    string get_date()//function to converts date to string
{
   time_t now;
   char the_date[15];

   the_date[0] = '\0';

   now = time(NULL);

   if (now != -1)
   {
      strftime(the_date,15, "%d.%m.%y", gmtime(&now));
   }

   return string(the_date);
}



I got it..In a slightly different way..Thank anyway for ur help :)

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
#include <iostream>
#include <fstream>
#include <iomanip>
#include <locale>       
#include <string>        
#include <sstream> 
#include <time.h>

using namespace std;


string get_date();

 int main(){
    string date,out;
    string name,id,age,x,y;
    int no=0,store=0,a=0,b=0;
    float num1,num2,num3;
    
    date=get_date();
    out=".csv";
    out=date+out;
       	
    ifstream indata;
	ofstream outdata;  
    indata.open(out.c_str()); 
           
   if(!(indata.is_open()))//if the file cannot be opened,Enter name & personal details
  {       
    outdata.open(out.c_str(), ios::app);  
    cout<<"enter name:";
    cin>>name;
    cout<<"enter id:";
    cin>>id;
    cout<<"Enter age:";
    cin>>age;
                
    //outputting all the data into a csv file
    outdata<<"Number Test\n"<<endl; 
	outdata<<"Name:"<<","<<name<<endl;
	outdata<<"Id:"<<","<<id<<endl;
	outdata<<"Age:"<<","<<age<<endl;
	outdata<<"\n"<< endl;
    outdata << "No,Num1,Num2,Product" << endl;

     while (!(num1 ==-11))
    {		
	  cout<<"Enter Num1:";
	  cin>>num1;
      if(num1==-11) 
      {
        indata.open(out.c_str());
        string cell1;
	    indata >> cell1;
        break; 
      }  	
	   cout<<"Enter Num2:";
	   cin>>num2;
	   num3=num1*num2;
	   no++;
       outdata<<no<<","<<num1<<","<<num2<<","<<num3<< endl;  
    }
   }  
     
      else //if the file can be opened,continue prompting the user for numbers       
   {  
     while(!indata.eof())
     {
   	  indata >> x;
   	  a++;
     }
     no=a-7; 
     outdata.open(out.c_str(), ios::app);                 
      while (!(num1 ==-11))
    {		
	  cout<<"Enter Num1:";
	  cin>>num1;
      if(num1==-11)break;     	
	  cout<<"Enter Num2:";
	  cin>>num2;
	  num3=num1*num2;	  
	  no++;
      outdata<<no<<","<<num1<<","<<num2<<","<<num3<< endl; 
      indata.open(out.c_str());
     }
   }
   
    system("pause");
    return 0;
 
}      
    string get_date()//function to converts date to string
{
   time_t now;
   char the_date[15];

   the_date[0] = '\0';

   now = time(NULL);

   if (now != -1)
   {
      strftime(the_date,15, "%d.%m.%y", gmtime(&now));
   }

   return string(the_date);
}
Topic archived. No new replies allowed.