How to change the extension of a file? - c++

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;
}

Related

How to redirect stderr to file without any buffer?

Does anybody know how to redirect stderr into a file without buffering in? if it is possible could you show me a simple code in c++ language for linux (Centos 6) operating system..?!
In C
#include <stdio.h>
int
main(int argc, char* argv[]) {
freopen("file.txt", "w", stderr);
fprintf(stderr, "output to file\n");
return 0;
}
In C++
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int
main(int argc, char* argv[]) {
ofstream ofs("file.txt");
streambuf* oldrdbuf = cerr.rdbuf(ofs.rdbuf());
cerr << "output to file" << endl;
cerr.rdbuf(oldrdbuf);
return 0;
}
Another way to do this is with the following dup2() call
#include <iostream>
#include <stdexcept>
#include <stdio.h>
#include <unistd.h>
using std::cerr;
using std::endl;
int main() {
auto file_ptr = fopen("out.txt", "w");
if (!file_ptr) {
throw std::runtime_error{"Unable to open file"};
}
dup2(fileno(file_ptr), fileno(stderr));
cerr << "Write to stderr" << endl;
fclose(file_ptr);
}

Error in file reading with ifstream

I'm having issue in the ifstream function, I have tried using the argv[1] as parameter but wont load the map, the map is located in the same folder of main code.
I'm stucked here and can not debug.
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
using namespace std;
int main (int argc, char *argv[]){
int h;
int w;
int var;
string inputLine;
ifstream f;
f.open("map.pgm",ios::in);
if (!f){
cout << "error" << endl;
exit(1);
}
I'm using Visual Studio 2017
Change this line:
if (!f){
by this:
if (!f.is_open()){
BTW you can check current directory path with GetModuleFileName

ofstream doesn't create a file

#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?

Error opening file when debugging

When i try to run the following program:
// ConsoleApplication1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <fstream>
#include <iostream>
int _tmain(int argc, _TCHAR* argv[])
{
std::ifstream inFile("test.txt");
if(!inFile.is_open()){
std::cout << "Doesn't work" << std::endl;
}
inFile.close();
return 0;
}
The program isn't able to open the file, the file exists in the same folder as the executable(I also tried to put in the explicit path of the file: C:\Users\..)
The value of the variable inFile after trying to open the file is:
+ inFile {_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> >
Try this:
// ConsoleApplication1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <fstream>
#include <iostream>
int _tmain(int argc, _TCHAR* argv[])
{
std::ifstream inFile("c:\\test.txt");
if(!inFile.is_open())
{
std::cout << "Doesn't work" << std::endl;
}
inFile.close();
return 0;
}
This should work if you put your file to C:\test.txt. The rest is up to you to figure out. Relative paths work, you need to make sure the file is where your program looks for it (or the other way 'round). Try to print out the current path if you are unsure what's wrong.

C++ std::cout and string printing in wrong order

I have a simple program running on Linux using g++ compiler:
#include <string>
#include <sstream>
#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char **argv){
fstream file;
string s;
file.open("sample/dates.dat", fstream::in);
if(!file.good())
return 0;
getline(file, s);
cout << s << "." << endl;
return 0;
}
Compiled with: g++ -o test test.cpp. When I run this, the fullstop is printed BEFORE the string s, not after. Does anybody know why this is happening? And is it easy to fix?
Thanks.
If there is a carriage return at the end of the string it will move the position of output to the beginning of the console line when printed.
#include <iostream>
int main()
{
std::cout << "some line\r" << "." << std::endl;
// ^^ carriage return
}