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.
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
char c;
ofstream outFile("/home/gnome/Music/file.txt", ios::out);
while (cin >> c) {
outFile.put(c);
}
outFile.close();
return 0;
}
When I want to stop, I can't interrupt it with a control-D like I do on the the Linux terminal. How do I do this in eclipse. The issue is that, It doesn't write any char to my file. When I cat the file, it's empty, and the past data is gone. Empty. I used this code with g++ on the terminal, and it works.
How do I use this in eclipse?
Console
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
int A;
ifstream file("file.txt"); // there is a single "8" in the file
file >> A;
cout << A; // I get 0 always
return 0;
}
While I'm not new to CodeBlocks, I'm new to CodeBlocks on Mac. I have changed the "Execution working directory" and it still does not work, please help.
Don't change the execution working directory.. When you're reading from file, try writing the full directory where is that file, for example:
// this is your file.txt location
ifstream file("C:\\Desktop\\file.txt"); // this is for Windows
and then run a program.
If it still doesn't work, try watching this tutorial: https://www.youtube.com/watch?v=De6trY8FRYY
#include <iostream>
#include <fstream>
#include <graphics.h>
using namespace std;
int c;
fstream file("happy.txt");
int main()
{
file.open("happy.txt", fstream::out|fstream::in|fstream::trunc );
file.close(); //to clear the file first
initwindow(1000,600);
while (true){
c = getch();
file.open("happy.txt");
file.seekp(0,file.end);
int fileEndCursor = file.tellp();
file << (char)c; //store what the user typed into file
file.close();
}
}
But it turns out that the file is not cleared, every time the code is finished running, the content in the last run is still in the file. Please help.
You are trying to open it twice:
fstream file("happy.txt");
file.open("happy.txt", fstream::out|fstream::in|fstream::trunc );
The first time you try to open it in the constructor without telling it to truncate so it only opens it leaving all the content inside.
Because it's already open the 2nd call won't do anything.
Fix it by changing
fstream file("happy.txt");
to
fstream file;
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();