Feb 25, 2015 at 2:07am UTC
You didn't specify a buffer size for your array/strings, add a size your code will work.
However there are other things that should be mentioned:
1. You can't use anything with a space in the file name in your code. (eg. c:\\documents and settings\\blahblahblah.txt)
2. You may want to consider using std::string instead of char arrays, but your code=your call
3. You'll start a flame war using goto :)
Feb 25, 2015 at 2:54pm UTC
Can you please be more specific about how to fix the problem.
1. Not using the space, I'm aware of that, it's a char ofc.
2. Tried with strings also, same issue.
3. I have it under control :P
Feb 25, 2015 at 3:55pm UTC
the code is not complete but it works in renaming the 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 87 88 89 90 91 92 93 94 95 96 97
#include <iostream>
#include <cstdio>
#include <fstream>
#include <string>
using namespace std;
int main()
{
std::string address;
std::string newname;
char input;
int action;
char confirm;
int result;
cout << "File Manipulator 1.0" << endl;
cout << "--------------------" << endl << endl;
cout << "Type the full address of a file you wish to manipulate." << endl << endl;
std::getline(std::cin, address);
ifstream myfile(address);
// try to open the file
if (myfile.is_open())
{
cout << "File Good" << std::endl;
myfile.close();
}
else
{
cout << "The selected file does not exist! Would you like to create it? " ;
std::cin >> input;
if (input == 'y' || input == 'Y' )
{
// create the file.
ofstream output(address.c_str());
output.close();
cout << endl << "-----------------------------------" << endl;
cout << "Type 1 to move the selected file." << endl;
cout << "Type 2 to rename the selected file." << endl;
cout << "Type 3 to delete the selected file." << endl;
cout << "-----------------------------------" << endl << endl;
std::cin >> action;
switch (action)
{
case 1:
{
// do nothing.
}
break ;
case 2:
{
// rename file.
std::cout << "Enter the new name" << endl << endl;
cin.ignore();
std::getline(std::cin, newname);
std::cout << "Are you sure you want ot rename the selected file ? Y/N" << endl << endl;
std::cin >> confirm;
if (confirm == 'Y' || confirm == 'y' )
{
rename(address.c_str(), newname.c_str());
}
}
break ;
case 3:
{
// delete file.
std::remove(address.c_str());
}
break ;
default :
{
std::cout << "You typed an invalid command!" << std::endl;
}
break ;
}
}
}
system("pause" );
return 0;
}
Last edited on Feb 25, 2015 at 3:57pm UTC
Feb 26, 2015 at 4:44am UTC
To be more specific, change
1 2
char address[] = "" ;
char newname[] = "" ;
To both have an array size instead of a blank size.
Last edited on Feb 26, 2015 at 4:44am UTC
Feb 26, 2015 at 12:24pm UTC
@rafae11 the code u provided works perfect on an online c++ shell, but still doesn't work for me. Line 23 gives me an error:
[Error] no matching function for call to 'std::basic_ifstream<char>::basic_ifstream(std::string&)'