C++ write to file, save as..?

Hello everyone!
i just made this --->
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <fstream>

int main()
{
    std::ofstream YourFileName;
    std::string TxtIn;
    std::cout << "Wirting to file...\n";

    YourFileName.open("NewFile.txt");

    std::getline(std::cin, TxtIn);

    YourFileName << TxtIn;
    YourFileName.close();

    std::cout << "Press <Enter> To Exit.\n";
    std::cin.ignore();
    return 0;
}

i wounder how i can save the file anywere i want? like Save as... in notepad
thanks in advance
-VinKol
Ask the user to give a file name.

1
2
3
4
5
6
7
8
9
10
11
// ...

std::string filename;

std::cout << "Save as (give name for file): ";
std::getline(std::cin, filename);

YourFileName.open(filename); // C++11 (may not work if your compiler is old)
// YourFileName.open(filename.c_str()); // C++98 (should work on all compilers)

// ... 


http://www.cplusplus.com/reference/fstream/ofstream/open/
Last edited on
I get error on line 8 > YourFileName.open(filename);
: Edit i saw the YourFileName.open(filename.c_str()); working now.. is there anyway to save it to .txt? without type the filename then .txt ?
Last edited on
Yes. You can add the ".txt" to the filename string.

1
2
3
4
5
6
7
8
9
10
// ...

std::string filename;

std::cout << "Save as (give name for file): ";
std::getline(std::cin, filename);

YourFileName.open((filename + ".txt").c_str());

// ...  


Or, the same effect but changes filename:

1
2
3
4
5
6
7
8
9
10
11
12
// ...

std::string filename;

std::cout << "Save as (give name for file): ";
std::getline(std::cin, filename);

filename += ".txt"; // string is changed

YourFileName.open(filename.c_str());

// ...  
Thanks working!
got a new question..
i ge tthis error
1
2
3
4
5
mingw32-g++.exe -Wall -fexceptions  -g     -c C:\Users\MW\Desktop\Test2\Cout.cpp -o obj\Debug\Cout.o
C:\Users\MW\Desktop\Test2\Cout.cpp: In function 'void main_2()':
C:\Users\MW\Desktop\Test2\Cout.cpp:8:12: error: return-statement with a value, in function returning 'void' [-fpermissive]
Process terminated with status 1 (0 minutes, 0 seconds)
1 errors, 0 warnings (0 minutes, 0 seconds)


whit this code
1
2
3
4
5
6
7
8
9
#include <iostream>
void main_2()
{
    std::string Coutname, Coutw, Coutexit;
    Coutname = "Save as... (Give name for file) :";
    Coutw = "Writing To File...\n";
    Coutexit = "Press <Enter> To Exit. \n";
    return 0;
}
Last edited on
This is because you have a return 0; in a void function.
A function that is void does not return anything.

Simply remove return 0; from the function main_2().
Last edited on
Edit: how can i print this >
1
2
3
4
5
6
7
void main_2()
{
    std::string Coutname, Coutw, Coutexit;
    Coutname = "Save as... (Give name for file) :";
    Coutw = "Writing To File...\n";
    Coutexit = "Press <Enter> To Exit. \n";
}

in here >
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int main()
{
    std::ofstream YourFileName;
    std::string TxtIn;
    std::string FName;

    std::cout << Coutname << std::endl; // something ike this. 

    std::getline(std::cin, FName);
    std::getline(std::cin, TxtIn);

    YourFileName.open((FName + ".txt").c_str());
    YourFileName << TxtIn;
    YourFileName.close();

    std::cin.ignore();
    return 0;
}
Last edited on
Topic archived. No new replies allowed.