@Thomas1965 the code I had written before was much more complicated until i simplified it to this. so I don't think it is complicated. and the conversion from txt to csv works so there is no problem with that part of the code.
I am just having trouble with opening a file in a user inputted location then saving the converted file to another user inputted location.
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
usingnamespace std;
int main() {
string link;
cout << "Enter path for file:";
cin >> link;
//string path = link; useless
ifstream yourfile; //oftream -> ifstream because i have to read the file first
yourfile.open(link.c_str());
if (!yourfile) {
cout << "File Not Found";
return 1;
}
else {
constchar comma = ',';
string line, word;
//ifstream yourfile;
ofstream out("newfile.csv"); //stream class that writes on files
while (getline(yourfile, line)) {
stringstream ss(line);
bool first = true;
while (ss >> word) {
if (!first)out << comma;
out << word;
first = false;
}
out << '\n';
}
string saveLocation;
cout << "enter path you would like to save new file:";
cin >> saveLocation;
//string path2 = link2;
out.open(saveLocation.c_str());
yourfile.close();
out.close();
}
system("PAUSE");
return 0;
}
this is the output im getting in the CMD prompt:
Enter path for file:F:\Visual Studios Projects\practice\file
enter path you would like to save new file:Press any key to continue . . .
Press any key to continue . . .
Didn't I mention earlier that you meed to use getline to read the filenames since >> stops a the first whitespace, also getline will remove trailing '\n'. See my first post in this thread
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
usingnamespace std;
int main()
{
constchar comma = ',';
string link;
string fileName;
string saveLocation;
string line, word;
ifstream sourceFile; //file you open first
ofstream saveFile; //file you create and save to
cout << "Enter path for file (e.g. C:\\Users\\Me\\Documents\\myfile.txt):\n";
cin >> link;
sourceFile.open(link.c_str());
if (!sourceFile)
{
cout << "\nFile Not Found";
return 1;
}
else
{
cout << "\nEnter path you would like to save new file (e.g. C:\\Users\\Me\\Desktop):\n";
cin >> saveLocation;
cout << "\nEnter .csv file name (e.g. file.csv): ";
cin >> fileName;
saveLocation.append("\\");
saveLocation.append(fileName); //These two lines add "\" and fileName to the saveLocation string
//so that you end up with a complete path with the file included in it
saveFile.open(saveLocation.c_str()); //you have to open your save file before you write on it, otherwise it
//doesn't work
while (getline(sourceFile, line))
{
stringstream ss(line);
bool first = true;
while (ss >> word) {
if (!first)saveFile << comma;
saveFile << word;
first = false;
}
saveFile << '\n';
}
cout << "\n\nFile successfully saved!\n\n";
sourceFile.close();
saveFile.close();
}
system("PAUSE");
return 0;
}
Yes, If you want your file name to comprise white spaces too you need to use getline instead of cin:
1 2 3
cout << "\nEnter .csv file name (e.g. file.csv): ";
cin.ignore(50, '\n'); //ignores the '\n' input when you press enter after typing path
getline(cin, fileName); //gets file name until '\n' is encountered (works to get spaces)
what about the path of the file and the path of the new file?
1 2
cout << "Enter path of your File(e.g. C:\\Users\\Me\\Documents\\myfile.txt):\n";
cin >> link;
1 2
cout << "\nenter the path you would like to save the CSV file (e.g. C:\\Users\\Me\\Desktop):\n";
cin >> saveLocation;
I didn't mean spaces in the file name. I meant spaces in the actual path. like if a folder had a white space in its name (e.g New folder). Sorry if i confused you.