Hey all, I have a problem, I don't know how to create a file in C++ in a specific place in the PC. For example a file (.txt) in C:\file.txt. Can anybody help me? Thank you :)
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ofstream ofs("c:\\file.txt");
if (ofs) {
ofs << "hello, world!\n";
}
return 0;
}
It's probably fooling you because it's easier than you think. You just open a file for create and give it that path name. Voila.
See, eg,
// fstream::open
#include <fstream>
using namespace std;
int main () {
fstream filestr;
// You need a doubled backslash in a C string
filestr.open ("C:\\file.txt", fstream::out);
// >> i/o operations here <<
filestr.close();
return 0;
}
#include <stdio.h>
....
FILE *file;
file = fopen("c:/file.txt", "w");
Related
I'm failing to open a .txt file using std::ifstream file and file.open(). I want to use an indirect PATH (starting in the folder the .exe file is in - \program_folder), but using a complete PATH (C:\Users\Rafael\Desktop\C++\program_folder\inputs\test.txt) also didn't work.
#include <iostram>
#include <vector>
#include <string>
#include <fstream>
int main(){
char c;
std::vector<std::string> inputs;
inputs.push_back("C:\\inputs\\test.txt");
int a; int b;
std::ifstream file;
file.open(inputs[0]);
if (file.is_open()){
c = file.get();
file.close();
}
else {std::cout << "\nfail to open file";}
}
As output, I'm getting the fail massage.
"C::\\inputs\\test.txt" was not a relative path i had to use "inputs\\test.txt"
thanks to john in the comments.
How can I write Persian text like "خلیج فارس" to a file using a std::wfstream?
I tried following code but it does not work.
#include <iostream>
#include <string>
#include <fstream>
int main()
{
std::wfstream f("D:\\test.txt", std::ios::out);
std::wstring s1(L"خلیج فارس");
f << s1.c_str();
f.close();
return 0;
}
The file is empty after running the program.
You can use a C++11 utf-8 string literal and standard fstream and string:
#include <iostream>
#include <fstream>
int main()
{
std::fstream f("D:\\test.txt", std::ios::out);
std::string s1(u8"خلیج فارس");
f << s1;
f.close();
return 0;
}
First of all you can left
f << s1.c_str();
Just use
f << s1;
To write "خلیج فارس" with std::wstream you must specify imbue for persian locale like:
f.imbue(std::locale("fa_IR"));
before you write to file.
I am experimenting with some code for outputting information to a file. The file address appears to be correct and the code compiles but the file never populates. Can you see a problem?
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
int size = 10;
for(int i=0; i<size; ++i)
{
ofstream outputfile;
outputfile.open("C:MyFolder\outputfile.txt", ios::app);
outputfile << "SYMBOL, STOCK_PRICE" << endl;
outputfile << i << endl;
outputfile.close();
}
}
When fixing the path to be an actual Windows path, it runs ok for me;
outputfile.open("C:\\MyFolder\\outputfile.txt", ios::app);
maybe the path is not right, you did not escape the backslashes. otherwise the code is fine, and worked for me.
Others have given answer to your problem. I also suggest you to open and close file only once (outside loop), and do only file-writing within loop.
As somebody who is new to C++ and coming from a python background, I am trying to translate the code below to C++
f = open('transit_test.py')
s = f.read()
What is the shortest C++ idiom to do something like this?
The C++ STL way to do this is this:
#include <string>
#include <iterator>
#include <fstream>
using namespace std;
wifstream f(L"transit_test.py");
wstring s(istreambuf_iterator<wchar_t>(f), (istreambuf_iterator<wchar_t>()) );
I'm pretty sure I've posted this before, but it's sufficiently short it's probably not worth finding the previous answer:
std::ifstream in("transit_test.py");
std::stringstream buffer;
buffer << in.rdbuf();
Now buffer.str() is an std::string holding the contents of transit_test.py.
You can do file read in C++ as like,
#include <iostream>
#include <fstream>
#include <string>
int main ()
{
string line;
ifstream in("transit_test.py"); //open file handler
if(in.is_open()) //check if file open
{
while (!in.eof() ) //until the end of file
{
getline(in,line); //read each line
// do something with the line
}
in.close(); //close file handler
}
else
{
cout << "Can not open file" << endl;
}
return 0;
}
I want to create a file using C++, but I have no idea how to do it. For example I want to create a text file named Hello.txt.
Can anyone help me?
One way to do this is to create an instance of the ofstream class, and use it to write to your file. Here's a link to a website that has some example code, and some more information about the standard tools available with most implementations of C++:
ofstream reference
For completeness, here's some example code:
// using ofstream constructors.
#include <iostream>
#include <fstream>
std::ofstream outfile ("test.txt");
outfile << "my text here!" << std::endl;
outfile.close();
You want to use std::endl to end your lines. An alternative is using '\n' character. These two things are different, std::endl flushes the buffer and writes your output immediately while '\n' allows the outfile to put all of your output into a buffer and maybe write it later.
Do this with a file stream. When a std::ofstream is closed, the file is created. I prefer the following code, because the OP only asks to create a file, not to write in it:
#include <fstream>
int main()
{
std::ofstream { "Hello.txt" };
// Hello.txt has been created here
}
The stream is destroyed right after its creation, so the stream is closed inside the destructor and thus the file is created.
#include <iostream>
#include <fstream>
int main() {
std::ofstream o("Hello.txt");
o << "Hello, World\n" << std::endl;
return 0;
}
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
string filename = "/tmp/filename.txt";
int main() {
std::ofstream o(filename.c_str());
o << "Hello, World\n" << std::endl;
return 0;
}
This is what I had to do in order to use a variable for the filename instead of a regular string.
Here is my solution:
#include <fstream>
int main()
{
std::ofstream ("Hello.txt");
return 0;
}
File (Hello.txt) is created even without ofstream name, and this is the difference from Mr. Boiethios answer.
If you want to create a file with some content and don't need to deal with the ofstream after that you can simply write:
#include <fstream>
int main() {
std::ofstream("file.txt") << "file content";
}
no need to manually close the file, deal with variables, etc. The file is created, written, and closed in the same line.
/*I am working with turbo c++ compiler so namespace std is not used by me.Also i am familiar with turbo.*/
#include<iostream.h>
#include<iomanip.h>
#include<conio.h>
#include<fstream.h> //required while dealing with files
void main ()
{
clrscr();
ofstream fout; //object created **fout**
fout.open("your desired file name + extension");
fout<<"contents to be written inside the file"<<endl;
fout.close();
getch();
}
After running the program the file will be created inside the bin folder in your compiler folder itself.
use c methods FILE *fp =fopen("filename","mode");
fclose(fp);
mode means a for appending
r for reading ,w for writing
/ / using ofstream constructors.
#include <iostream>
#include <fstream>
std::string input="some text to write"
std::ofstream outfile ("test.txt");
outfile <<input << std::endl;
outfile.close();