char NAME[256];
cin.getline (NAME,256);
ofstream fout("NAME.txt"); //NAME???????
What i need to do to create file with NAME name?
You could try:
#include <string>
#include <iostream>
#include <fstream>
int main() {
// use a dynamic sized buffer, like std::string
std::string filename;
std::getline(std::cin, filename);
// open file,
// and define the openmode to output and truncate file if it exists before
std::ofstream fout(filename.c_str(), std::ios::out | std::ios::trunc);
// try to write
if (fout) fout << "Hello World!\n";
else std::cout << "failed to open file\n";
}
Some useful references:
http://en.cppreference.com/w/cpp/string/basic_string
http://en.cppreference.com/w/cpp/string/basic_string/getline
http://en.cppreference.com/w/cpp/io/basic_ofstream/basic_ofstream
http://en.cppreference.com/w/cpp/io/basic_filebuf/open
http://en.cppreference.com/w/cpp/io/ios_base/openmode
Like this:
#include <string>
#include <fstream>
std::string filename;
std::getline(std::cin, filename);
std::ofstream fout(filename);
In older versions of C++ the last line needs to be:
std::ofstream fout(filename.c_str());
You can try this.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string fileName;
cout << "Give a name to your file: ";
cin >> fileName;
fileName += ".txt"; // important to create .txt file.
ofstream createFile;
createFile.open(fileName.c_str(), ios::app);
createFile << "This will give you a new file with a name that user input." << endl;
return 0;
}
Related
I starting over a c++ journey and I cannot upload a file and parse it. Could you please tell me what is wrong with my code?
thank you
#include <iostream>
#include <fstream>
#include <string>
int main(int argc, const char * argv[]) {
std::string FileName("upload.txt");
std::ifstream MyFile(FileName);
MyFile.open(FileName);
std::string LastName;
std::string Name;
int Number;
if (!MyFile.is_open()){
std::cout <<"The fils is not opened \n"<< std::endl;
}
std::cout<<"olivier"<< std::endl;
while (MyFile >> LastName >> Name >> Number){
std::cout<<"olivier"<< std::endl;
std::cout<< LastName << std::endl;
}
return 0;
}
You are opening the file twice
std::ifstream MyFile(FileName);
MyFile.open(FileName);
First time you use constructor and then you are trying to open the same file. Just remove the MyFile.open(FileName); and everything will be fine.
I am trying to write and read information from the same file, but I don't know actually how to do that and why it doesn't work.
When I compile the code, the string that I expect to be filled with information from the file, actually doesn't get filled.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string str;
ifstream fin("Asort.txt");
ofstream fout("Asort.txt");
fout << "hello world";
getline(fin, str);
cout << str;
}
The problem is the location of the "cursor" (thus: the marker) after the statement:
fout << "hello world";
Illustration:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string str;
ifstream fin("Asort.txt");
ofstream fout("Asort.txt");
fout << "hello world"; //output to cout this statement(fout.tellp()) to see where the marker is at this point in the stream
fout.seekp(0); //reset the marker in the fout stream to the beginning
getline(fin, str);
cout << str;
}
The cursor is now at the end of the stream. So you have to use:
fout.seekp(0);
to get it to the beginning so that the fin can start reading from the beginning of the stream.
I am trying to write a few lines into a text file. I would like to empty the file before appending to it, on each run. I am able to clear the previous content, but when I do so, for some reason only the last line of my input file is appended to the output file. I also tried using remove() to erase the file and received the same output.
On the other hand without clearing the file or removing it, everything is appended properly to the output file.
I would be happy to find a way to solve this and perhaps understand why this occurs. I am using C++11.
I looked here: How to clear a file in append mode in C++
#include <string>
#include <fstream>
#include <sstream>
#include <iostream>
#include <stdio.h>
int main() {
std::fstream infile;
std::string line;
infile.open("file.txt" , std::ios::in);
while (std::getline(infile, line)) {
std::istringstream line_buffer(line);
std::string word;
std::fstream outfile;
outfile.open("out.txt", std::ios::out);
outfile.close();
outfile.open("out.txt", std::ios::app);
while (line_buffer >> word) {
std::cout << word << " ";
outfile << word << " ";
}
std::cout << std::endl;
outfile << std::endl;
}
return 0;
}
The problem is that you are clearing the file at each iteration of the while loop, you can just open the outfile before the loop like this:
#include <string>
#include <fstream>
#include <sstream>
#include <iostream>
#include <stdio.h>
int main() {
std::fstream infile;
std::string line;
infile.open("file.txt" , std::ios::in);
std::fstream outfile;
outfile.open("out.txt", std::ios::out);
while (std::getline(infile, line)) {
std::istringstream line_buffer(line);
std::string word;
while (line_buffer >> word) {
std::cout << word << " ";
outfile << word << " ";
}
std::cout << std::endl;
outfile << std::endl;
}
outfile.close();
return 0;
}
This is my first project in C++. I took a course using C previously and file I/O seems to differ a little.
The project requires the user to enter a name for saving the output file.
I know I should use ofstream which should look like this:
ofstream myfile;
myfile.open ("example.txt");
myfile << "Writing this to a file.\n";
myfile.close();
I've bolded the snippet that's causing confusion.
How can I name the file from a string entered by the user?
*Note, C type string, so an array of characters.
#include < string > is not allowed
As my other answer has got a negative vote, here's another solution without #include <string>
You can just save the input from the user in a temporary char array and then save it to a string variable std::string.
Includes that are necessary:
#include <iostream>
#include <fstream>
Saving an input from an user into a char array:
char input[260];
cin >> input;
To then save it in a string variable just do this:
string filename = input;
To open a file stream you'll need to use std::ofstream. Please keep in mind, that the file is created in the same folder as the project/application is.
std::ofstream outfile (filename + "." + "file extension");
And as you already know this outfile.open(); opens the file.
With outfile << "hello"; you can write into the file.
To close the file, use outfile.close(); to close the file.
Here you have a little example code:
#include <iostream>
#include <fstream>
using namespace std;
void main()
{
char input[260];
cin >> input;
string filename = input;
ofstream outfile(filename + "." + "txt");
outfile << "hello";
outfile.close();
}
I hope this helps.
Regards.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
string path;
string name;
string h_path;
string text;
void create() {
ofstream file(h_path, ios::app);
if (!file.fail()) {
file << text;
file.close();
}
}
int main() {
cout << "please enter path(c:\\folder\): ";
cin >> path;
cin.ignore();
path = path + "/";
cout << "please enter the name of the file (test.txt): ";
getline(cin, name);
cout << "content of the file: ";
getline(cin, text);
h_path = path + name;
create();
cout << "new file created";
cout << h_path;
}
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
string fileName;
cin >> fileName;
ofstream myfile;
myfile.open(fileName);
myfile << "Writing this to a file.\n";
myfile.close();
}
I want the user to enter the name of a file, and if the file exists, print out all the contents of the file.
At the moment the uncommented code, takes a name of a file that the user inputs, for example. example.txt and prints out most (not the last word?) of the file. I've tried to implement this instead by using string (commented code is attempt) but clearly its incorrect.
I also wondering if i can automatically add .txt to the end of the user input, so that the console could ask - "which subject should we find more information on" user inputs "math" and it will open "math.txt"
Here is what I´ve tried:
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;
int main() {
char filename[50];
//string getcontent;
ifstream name;
cin.getline(filename, 50);
name.open(filename);
if (!name.is_open()) {
exit(EXIT_FAILURE);
}
char word[50];
name >> word;
while (name.good()) {
cout << word << " ";
name >> word;
}
//if (!name.is_open()) {
//while (! filename).eof())
//{
//getline(name, getcontent)
//cout << getcontent << endl;
//}
//exit(EXIT_FAILURE); //comes from cstdlib
//}
//}
system("pause");
return 0;
}
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;
int main() {
string filename;
string getcontent;
ifstream name;
cin >> filename;
filename.append(".txt"); // add extension.
name.open(filename);
if (!name.is_open()) {
exit(EXIT_FAILURE);
}
while (true)
{
getline(name, getcontent);
if (name.eof()) break;
cout << getcontent << endl;
}
return 0;
}
I found this and it helped me with a somewhat different problem and I also thought that I might be able to help. This is coded in windows. (I'm a beginner so forgive me if I made some obvious mistakes)
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
ifstream fin;
int main()
{
//char filename[50],word[50];
string filename,word;
//cin.getline(filename,50);
getline(cin,filename);
//strcat(filename,".txt");
filename.append(".txt");
fin.open(filename);
if(fin.is_open())
while(fin>>word)
cout<<word<<endl;
else
cout<<"No such file"<<endl;
return 0;
}