ofstream doesn't create a file - c++

#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream infile("text.txt", ios_base::in); // this works
ofstream outfile("C:/output.txt", ios_base::out); // doesn't
outfile << "hi";
return 0;
}
No file is created. What is the problem?

Related

Problem with file by using fstream library and print on screen?

I am trying to print the data located in the weapon.obj file, but it's not working.
Compiler Error: error: no matching function for call to
'getline(std::ifstream&)'|
Code:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ofstream render_weapon_OBJ ("weapon.obj");
render_weapon_OBJ << ("Weapon Names");
render_weapon_OBJ.close();
ifstream execute_weapon_OBJ ("weapon.obj");
while (getline(execute_weapon_OBJ))
{
cout << execute_weapon_OBJ << '\n';
}
execute_weapon_OBJ.close();
}
You must specify where to read the data and use that for printing.
#include <iostream>
#include <fstream>
#include <string> // add this to use std::string
using namespace std;
int main()
{
ofstream render_weapon_OBJ ("weapon.obj");
render_weapon_OBJ << ("Weapon Names");
render_weapon_OBJ.close();
ifstream execute_weapon_OBJ ("weapon.obj");
string weapon; // add this for read buffer
while (getline(execute_weapon_OBJ, weapon)) // add where to read
{
cout << weapon << '\n'; // print what was read instead of the stream
}
execute_weapon_OBJ.close();
}
May be this is the solution you are looking for
You need a variable to store the line read from the file and you need to print the variable not the variable used to initialize the file stream.
The error is in the while loop[The new code is below]
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
string line;
ofstream render_weapon_OBJ ("weapon.obj");
render_weapon_OBJ << ("Weapon Names");
render_weapon_OBJ.close();
ifstream execute_weapon_OBJ ("weapon.obj");
while(getline(execute_weapon_OBJ,line))
{
cout << line << '\n';
}
execute_weapon_OBJ.close();
}

error read data from binary file in visual studio code

Hi everyone I create a data name data2.bin and trying to run it trough this program,
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
struct Mahasiswa
{
int NIM;
string nama;
string jurusan;
};
int main()
{
fstream myFile;
Mahasiswa dataBaca;
myFile.open("data2.bin", ios::in | ios::binary);
myFile.read(reinterpret_cast<char *>(&dataBaca), sizeof(Mahasiswa));
cout << dataBaca.NIM << endl;
cout << dataBaca.nama << endl;
cout << dataBaca.jurusan << endl;
myFile.close();
return 0;
}
when I run it, the program would stop at myFile.close() line and the output would be question mark symbols like (link added below). Anyone knows where's the problem?
output image from vscode terminal
here's the code to create the data2.bin
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
struct Mahasiswa{
int NIM;
string nama;
string jurusan;
};
int main(){
fstream myFile;
myFile.open ("data2.bin", ios::trunc | ios::out | ios::in | ios::binary);
Mahasiswa mahasiswa1, mahasiswa2, mahasiswa3;
mahasiswa1.NIM = 123;
mahasiswa1.nama = "ucup";
mahasiswa1.jurusan = "memasak";
mahasiswa2.NIM = 124;
mahasiswa2.nama = "otong";
mahasiswa2.jurusan = "menjahit";
mahasiswa3.NIM = 125;
mahasiswa3.nama = "sandra";
mahasiswa3.jurusan = "mesin";
myFile.write(reinterpret_cast<char*>(&mahasiswa1),sizeof(Mahasiswa));
myFile.write(reinterpret_cast<char*>(&mahasiswa2),sizeof(Mahasiswa));
myFile.write(reinterpret_cast<char*>(&mahasiswa3),sizeof(Mahasiswa));
myFile.close();
return 0;
}

How to change the extension of a file?

I am currently working on project where I need to add some message at the end of a file and then I want to change its extension.
I know how to add the message at the end of the file; my code:
_ofstream myfile;
_myfile.open("check.txt", std::ios_base::app);
_myfile << "Thanks for your help.\n";
How can I change the file's extension?
Actualy, it is very simple:
#include <iostream>
#include <fstream>
#include <cstdio>
using namespace std;
int main(int argc, char* argv[])
{
ofstream fout("test.txt", ios_base::app);
fout << "My cool string";
fout.close();
rename("test.txt", "test.txt1");
return 0;
}

ifstream initialization error- Error Reading Characters

#include "Stdafx.h"
#include "FishTracker.h"
#include <string>
#include "Utils.h"
#include <fstream>
using namespace std;
using namespace cv;
int main()
{
std::string videopath;
videopath = "E:\\TUKLNUST\\fishdata2\\Damo\\AR2-6\\Tetraodontidae_Lagocephalus_sceleratus_AD\\00001\\";
ifstream str;
str.open((videopath + DATA_TXT).c_str());
if (str.is_open())
{
cout << "file is open.";
}
}
file is open but str is this.
+ str {_Filebuffer={_Set_eback=0xcccccccc <Error reading characters of string.> _Set_egptr=0xcccccccc <Error reading characters of string.> ...} } std::basic_ifstream<char,std::char_traits<char> >
Configs:
win32, Debug
Visual Studio 2013
#include "Stdafx.h"
#include "FishTracker.h"
#include <string>
#include "Utils.h"
#include <fstream>
using namespace std;
using namespace cv;
int main()
{
std::string videopath;
videopath = "E:\\TUKLNUST\\fishdata2\\Damo\\AR2-6\\Tetraodontidae_Lagocephalus_sceleratus_AD\\00001\\";
ifstream str;
str.open((videopath + "DATA_TXT").c_str());
if (str.is_open())
{
cout << "file is open.";
}
}

c++ combine path of file as a string with file name

I want to read some input files in my c++ code and I want to define the path of input files as a string and then combine it with file names. How can I do this? (Input_path + filename.dat)
#include <filesystem> //C++ 17
#include <iostream>
namespace fs = std::filesystem;
using namespace std;
void main()
{
string dir("c:\\temp");
string fileName("my_file.txt");
fs::path fullPath = dir;
fullPath /= fileName;
cout << fullPath.c_str() << endl;
}
You would use something like:
string path ("yourFilePath");
string filename ("filename");
You could then open the file like this:
ifstream inputFileStream;
inputFileStream.open(path + fileName);
Depending on your requirements, you will have to decide whether to use formatted or unformatted input when reading. I would read this for more information regarding that.
Cocatenation referenced from: C++ string concatenation
Reading referenced from: C++ read and write with files
Try any of these codes:
#include <iostream>
#include <string>
#include <fstream>
int main() {
std::string filepath = "D:/location/";
filepath+= "filename.dat";
std::ifstream fp;
fp.open(filepath.c_str(),std::ios_base::binary);
....PROCESS THE FILE HERE
fp.close();
return 0;
}
or
#include <iostream>
#include <string>
#include <fstream>
int main() {
std::string filepath = "D:/location/";
std::ifstream fp;
fp.open((filepath+"filename.dat").c_str(),std::ios_base::binary);
...............
fp.close();
return 0;
}
or use std::string::append
#include <iostream>
#include <string>
#include <fstream>
int main() {
std::string filepath = "D:/location/";
std::ifstream fp;
fp.open((filepath.append("filename.dat")).c_str(),std::ios_base::binary);
fp.close();
return 0;
}