Create a file using classes help | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Create a file using classes help

Hello, I am trying to create a program on the cpp droid that will create a csv file and write out onto it. The program runs through just fine, but no file is created which is the ultimate goal. Does anyone see what I could be doing wrong? I would greatly appreciate the help. Thank you P.S. I have looked through various questions and tried loading my program code onto the compiler here, the C++ Shell online, and online GDB, but to no avail. Apologies for the long code below. CODE: #include <iostream> #include <string> #include <iomanip> #include <fstream> using namespace std; class FileCreator { ofstream MyFile; public: FileCreator(string); void WriteFile(string, double, double, double, double, double); void WriteHeader(); void CloseFile(); ~FileCreator(); }; inline FileCreator::FileCreator(string title) { ofstream MyFile; cout << "Please enter a title for the document" << endl; cin >> title; title += ".csv"; MyFile.open("./Internal storage/Documents/title.c_str()", ios::in | ios::out | ios::trunc); } inline void FileCreator::WriteFile(string StoreName, double JanSales, double FebSales, double MarSales, double AprSales, double TotalSales) { ofstream MyFile("./Internal storage/Documents/title.c_str()"); TotalSales = JanSales + FebSales + MarSales + AprSales; MyFile << StoreName << ", " << JanSales << ", " << FebSales << ", " << MarSales << ", " << AprSales << ", " << TotalSales << "\n"; } inline void FileCreator::WriteHeader() { ofstream MyFile("./Internal Storage/Documents/title.c_str()"); MyFile << "Store" << ", " << "January" << ", " << "Feburary" << ", " << "March" << ", " << "April" << ", " << "Total" << "\n"; } inline void FileCreator::CloseFile() { ofstream MyFile("./Internal storage/Documents/title.c_str()"); MyFile.close(); } inline FileCreator::~FileCreator() { } int main() {

27th Apr 2020, 3:47 PM
jcardoza
15 Answers
0
I didn't read your code thoroughly, so just to check, does the app you are using have permission to access the phone's internal storage? it could be a permission issue.
27th Apr 2020, 3:55 PM
Bahhaⵣ
Bahhaⵣ - avatar
0
the path to internal storage is : /sdcard/
27th Apr 2020, 3:56 PM
Bahhaⵣ
Bahhaⵣ - avatar
0
Soory I did not notice the rest of the code did not paste! Here it is and thank you Bahha, but there is actually no sdcard in the tablet. I checked the permissions as well. int main() { string title, storeName, Chicago, Naperville, Evanston, Oakton, Skokie; int b, s; double a, f, j, m, t; b =0; t = 0; cout << "Best Buy Quarterly Earnings Report Generator\n"; cout << "============================================\n\n"; FileCreator BestBuy(title); BestBuy.WriteHeader(); cout << "How many store's data will you be inputting?" << endl; cin >> s; while (b < s) { cout << "Please enter the name of the store: " << endl; cin >> storeName; cout << "Thank you! Please enter the sales for " << storeName << " by the month.\n"; cout << "January: "; cin >> j; cout << "Feburary: "; cin >> f; cout << "March: "; cin >> m; cout << "April: "; cin >> a; cout << "\n"; BestBuy.WriteFile(title, j, f, m, a, t); cout << "Data for " << storeName << " entered. " << (b + 1) << " of " << s << " stores input.\n"; b += 1; } cout << "Thank you for using our application, your file is ready."; BestBuy.CloseFile(); return 0; }
27th Apr 2020, 4:13 PM
jcardoza
0
sdcard refers to internal storage in many devices. install a terminal emulator and try : cd /sdcard ls - l you will see the folders and files of internal storage.
27th Apr 2020, 4:16 PM
Bahhaⵣ
Bahhaⵣ - avatar
0
Of course, sorry I should know that. I updated the code to this: MyFile.open(".cd/sdcard/title.c_str()", ios::in | ios::out | ios::trunc); Am I following you? Just in case I did run it with this and again the program "runs" but does not create a file. Thank you.
27th Apr 2020, 4:24 PM
jcardoza
0
no, not cd, I just wanted you to check in terminal not to write it in your code. the paths should be something like. MyFile.open("/sdcard/"+ title) don't include the function inside quotes.
27th Apr 2020, 4:26 PM
Bahhaⵣ
Bahhaⵣ - avatar
0
Thank you again and I see where I was going wrong there. So I tried that and now I got the "invalid operands of types ‘const char [9]’ and ‘const char*’ to binary ‘operator+’" error. Looking for solutions to work through this now Thank you for the help you have been giving!
27th Apr 2020, 4:51 PM
jcardoza
0
I made it work here by declaring title variable above inline FileCreator and assigned it a value inside. https://code.sololearn.com/cc6f1jBKD16M/?ref=app
27th Apr 2020, 4:52 PM
Bahhaⵣ
Bahhaⵣ - avatar
0
in the above code, the path is "/root/" title.csv in the end I used: system("ls") to show that it created a file named title.csv in the root folder.
27th Apr 2020, 4:59 PM
Bahhaⵣ
Bahhaⵣ - avatar
0
Ok I see. I am going to review this, thank you and I just need to find the right path now frm my end!
27th Apr 2020, 5:10 PM
jcardoza
0
I fixed it, check the new one. and change paths to ("/sdcard/" + title) so it will work on your phone.
27th Apr 2020, 5:15 PM
Bahhaⵣ
Bahhaⵣ - avatar
0
to try it here enter inputs as : store 1 2 4 5 6 then submit that will create a file store.csv put each input in a new line just like above. because your code has 5 cin 1 is for the number of inputs
27th Apr 2020, 5:19 PM
Bahhaⵣ
Bahhaⵣ - avatar
0
Bahha, wow that worked perfectly! Thank you so much. I am just reviewing it to understand it completely.
27th Apr 2020, 5:45 PM
jcardoza
0
you are welcome 😁
27th Apr 2020, 5:47 PM
Bahhaⵣ
Bahhaⵣ - avatar
0
Que programa puedo descargar para programar en Java? Y cuál para hacer app de Android? Descargue Android Studio pero al parecer viene incompleto y no lo he podido terminar de instalar
29th Apr 2020, 1:42 AM
Jesús Rodríguez
Jesús Rodríguez - avatar