Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I'm trying to do some testings for a project that I'm working on. I tried to rename a file and it doesn't work at all, what am I doing wrong?
I already tried to check the close() and remove() functions.
#include <fstream>
#include<iostream>
#include <string>
using namespace std;
int main() {
fstream guy;
guy.open("testing.txt", fstream::out);
guy << "i love halav" << endl;
fstream guy1;
guy1.open("test1.txt", fstream::out);
guy1 << "i love halav" << endl;
guy.close();
guy1.close();
remove("testing.txt");
int result = rename("testing1.txt", "testing.txt");
if (result == 0)
cout << "File successfully renamed" << endl;
else
cout << "Error renaming file" << endl;
guy1.close();
return 0;
}
edit: i edit the code so it will be more cear, again its not working .
the rearult im expecting is that the content of newFile.txt wil be "this is oldFile"
#include <fstream>
#include<iostream>
#include <string>
#include <cstdio>
using namespace std;
int main() {
fstream guy;
guy.open("oldFile.txt", fstream::out );
guy << "this is oldFile" << endl;
fstream guy1;
guy1.open("newFile.txt", fstream::out );
guy1 << "this is newFile" << endl;
guy.close();
guy1.close();
std::rename("oldFile.txt", "newFile.txt");
remove("oldFile.txt");
return 0;
}
With std::rename your first argument is supposed to be the old filename and the second argument is supposed to be the new filename. You've swapped the arguments, so try:
std::rename("testing.txt", "testing1.txt");
... but don't std::remove("testing.txt"); before you rename the file since it'll remove it.
now thats the way to do it, thanks everyone!
#include <fstream>
#include<iostream>
#include <string>
#include <cstdio>
using namespace std;
int main() {
fstream guy;
guy.open("oldFile.txt", fstream::out );
guy << "this is oldFile" << endl;
fstream guy1;
guy1.open("newFile.txt", fstream::out );
guy1 << "this is newFile" << endl;
guy.close();
guy1.close();
remove("newFile.txt");
std::rename("oldFile.txt", "newFile.txt");
return 0;
}
Related
This question already has an answer here:
c++ - Doesn't name a type
(1 answer)
Closed 6 months ago.
I edit the code to clarify the actual code :
#include <fstream>
#include <iostream>
#include <ros/ros.h>
#include <rosbag/bag.h>
#include <std_msgs/Int32.h>
#include <std_msgs/String.h>
#include <nav_msgs/Odometry.h>
std::ofstream runtimeFile("cmg_operations_runtime.txt" , std::ios::out);
void callhandler(const nav_msgs::Odometry::ConstPtr& msg)
{
runtimeFile.open();
if (!runtimeFile)
{
std::cout << "cmg_operations_runtime.txt could not be opened.";
}
runtimeFile << "tempVector[j]" << ";\t";
runtimeFile.close ();
std::cout << "Runtime data stored." << std::endl;
}
int main(int argc, char **argv)
{
ros::init(argc, argv, "main");
ros::NodeHandle nh;
ros::Subscriber Listener = nh.subscribe<nav_msgs::Odometry>("/odom", 100, callhandler);
ros::spin();
return 0;
}
error: `‘runtimeFile’ does not name a type
9 | runtimeFile.open ("cmg_operations_runtime.txt")
The error is the same, I hope someone to help me in this issue?`
In C++ all code must be inside a function. Additionally all C++ programs must have a function called main.
Further your code opens the file twice, once when you declare the runtimeFile variable and once when you call open. Did you not think it strange that you have the file name twice in your code? Don't open files twice. Finally, although it's not an error, there is no need to close the file, that will happen automatically.
Put all that together and you have a legal C++ program.
#include <fstream>
int main()
{
std::fstream runtimeFile("cmg_operations_runtime.txt" , std::ios::out);
runtimeFile << "tempVector[j]" << ";\t";
}
EDIT
Some real code has been posted. Based on that I would remove the global runtimeFile variable and make it local to callHandler like the following
void callhandler(const nav_msgs::Odometry::ConstPtr& msg)
{
std::ofstream runtimeFile("cmg_operations_runtime.txt" , std::ios::out);
if (!runtimeFile)
{
std::cout << "cmg_operations_runtime.txt could not be opened.";
}
runtimeFile << "tempVector[j]" << ";\t";
std::cout << "Runtime data stored." << std::endl;
}
However I can't really see how the latest posted code causes the error described.
I'm currently new to C++ and I've been watching a tutorial series https://www.youtube.com/watch?v=_bYFu9mBnr4, but I'm having a big issue. My C++ code will not open a file no matter what I do, I've looked online and tried renaming it, the full path, everything I can think of. Here's my code,
#include <iostream>
#include <fstream>
#include <cstring>
#include <cerrno>
#include <filesystem>
int main()
{
std::ofstream file;
file.open("hello.txt");
if (!file.is_open())
{
std::cerr << "Error: " << strerror(errno) << '\n';
std::cout << std::filesystem::current_path() << std::endl;
}
file << "hello!";
file.close();
return 0;
}
Sorry about this question, it may have been a dumb issue. Turns out IT WAS my antivirus. Avast kept blocking it, it was just looking out for me. I decided to change my antivirus afterwards and it now works fine!
I'm trying to figure out how to write to a file outside the working directory. This is the code I currently have.
#include <iostream>
#include <fstream>
#include <string>
int main()
{
std::string sp{};
std::fstream ss("C:\\Users\\onion\\AppData\\Roaming\\MetaQuotes\\Terminal\\some numbers\\MQL5\\Files\\testnew.txt", std::ios::in | std::ios::out);
if (!ss.is_open()) std::cout << "Failed" << '\n';
else
{
while (ss.is_open())
{
std::getline(ss, sp);
std::cout << sp << '\n';
ss << "new data";
if (ss.eof())break;
}
}
}
I can read the file perfectly fine, but I cant write to it? Could it be that Metatrader itself is limiting my ability to write to a file or does a file have to be in the working directory to be able to write to it? or am I just doing it wrong?
Premise: I'm using CLion.
As i said in title, when i try to open a file (txt) nothing will be displayed.
i can't explain it, i don't think i made an error, it's pretty easy this code:
#include <iostream>
#include <cstdlib>
using namespace std;
int main() {
FILE *leggi;
leggi = fopen("lorem.txt", "r");
char datiLetti[1000];
while(fgets(datiLetti, 1000, leggi)!=NULL){
cout << datiLetti << endl;
}
fclose(leggi);
system("PAUSE");
return EXIT_SUCCESS;
}
file "lorem.txt" is in the same directory of the project.
Thank you in advance.
EDIT1: file is lorem not lorem_ipsum, my mistake when i typed here.
You want this:
...
FILE *leggi;
leggi = fopen("lorem.txt", "r");
if (leggi == NULL)
{
cout << "Can't open file" << endl;
return 1;
}
...
---FIXED---
Installed cygwig1.dll and cygstdc++-6.dll and put cygwig in glob variables, then my file worked in the same directory of main and exe.
However, thank you guys for your time!
fopen is a C solution for open a file if you want to open a file in c++ use fstream like flowing code.
fopen is deprecated in c++11.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
string line;
fstream myfile;
myfile.open("example.txt");
cerr << "Error: " << strerror(errno);
if (myfile.is_open())
{
while (getline(myfile, line))
{
cout << line << '\n';
}
myfile.close();
}
else cout << "Unable to open file";
return 0;
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
#include <iostream>
#include <iomanip>
#include <cmath>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
int main() {
ifstream input_file("DataStuff.txt", ios::in);
//trying to open the notepad file named DataStuff.txt
if(!input_file){
cerr << "Error" << endl; exit(1);
}
else{
cout << "good 2 go" << endl;
}
}
Try this:
#include <cstring>
#include <fstream>
#include <iostream>
int main() {
std::ifstream input_file("DataStuff.txt", ios::in);
if (input_file) {
std::cout << "ok\n";
} else {
std::cerr << "error: " << strerror(errno) << "\n";
return 1;
}
return 0;
}
The output should give you some idea what is going on. Most likely the file doesn't exist or the permissions are incorrect.
Further explanation: If the file couldn't be opened then the constructor for std::ifstream sets errno to a value indicating what the error was. You can access strings describing the error using the strerror() function (defined in <cstring>).
Good luck!
"Opening a file" is used in the sense that there is a connection established between the file: "DataStuff.txt" and your program, by the input stream, ifstream object named input_file which allows you to read from the file.
One possible cause for the file to not open is if it doesn't exist (including wrong name).
Another cause is if the file permissions do not allow access, for example if you are not owner of the file and its set to private.
To get more information about the actual cause you should probably acquaint yourself with stream rdstates and integrate them in your code to check the state of the stream after you try to open it.