I've been trying to make a c++ program to read and write to a .csv file. However, when i try to return the string inside the read function it somehow loses its value, maybe it is too big? I have tested this by using the cout function and it displays all of the .csv file in question. I am fairly new to c++ so sorry in advance for my code, ( variables i have passed through the functions are as a result of ALOT of testing ) So, mainly the problem is that the variable "lines" seems to be empty when returned. Thanks in advance for all answers
#include <iostream>
#include <string>
#include <fstream>
#include <cstring>
#include <cstdio>
#include <stdio.h>
#include <string.h>
using namespace std;
string read_function(string, string, string, string, int);
void write_function(string, string, string, string, int);
int main()
{
string line = " ", ans = " ", ans2 = " ", data = " ";
int i = 0, j = 0;
cout << "What file do you want to read? : ";
cin >> ans;
cout << "What do you want the new file to be called? : ";
cin >> ans2;
read_function(line, ans, ans2, data, i);
write_function(line, ans, ans2, data, i);
return 0;
}
string read_function(string line, string ans, string ans2, string data, int i)
{
ifstream read(ans.c_str());
for (i = 0; !read.eof(); i++)
{
getline(read, line, ',');
cout << line;
return line;
}
}
void write_function(string line, string ans, string ans2, string data, int i)
{
ofstream write(ans2.c_str(), ios::app);
write << line << ',';
write.close();
}