So, what I have here is a simple snippet of C++ code that merely askes for a filepath, checks if the filepath exists, and if it does, the program closes. If it doesn't, it creates a file and writes text to it. The path I enter is C:\temp.txt, but no matter which path I use the result is the same: No file is created. I am using Visual Studio 2010.
string outFile = "";
cout << "Enter a file path for an output file that does not exist already: ";
cin >> outFile;
ofstream file_writer;
file_writer.open(outFile, ios::out);
if (file_writer.good()) {
cout << "This file exists already!" << endl;
system("PAUSE");
return 255;
}
else
file_writer << "Hello";
file_writer.close();
You could try:
string outFile = "";
cout << "Enter a file path for an output file that does not exist already: ";
cin >> outFile;
ofstream file_writer;
file_writer.open(outFile, ios::in);
if (file_writer.good()) { //This now just checks if the file exists already
cout << "This file exists already!" << endl;
system("PAUSE");
return 255;
}
file_writer.open(outFile, ios::out); //Now since we know the file doesn't exist, we can create it safely
if (!file_writer.good()) {
cout << "Failed to create file!" << endl;
return 254;
}
file_writer << "Hello";
file_writer.close();
Check your fail() error message:
if (outfile.fail())
cout << strerror(errno) << endl;
You cannot write to "C:".
Try with a different path, and it should work.
Nice! ofstream::good() returns true if no error occurs. Use a negation in the if-condition.
if (!file_writer.good()) {
...
}
This response may be a little late, but I just spend two days figuring out why Visual Studio C++ was not creating any text files.
I'm going through Wrox Professional C++ Fourth Edition. In chapter 13 on the section for file streams, I could not get any examples to work. I spent many hours downloading sample code with the same results.
I then looked at my anti-virus software. There are two settings in there that caught my eye: Folder Access, and Exclusions. I'm running Windows Security on Widows 10. I changed Exclusions to prevent the security software from monitoring my vs2019 folder and subfolders. No improvement.
I then looked at the parent folder c:\files. This had folder access enabled. I removed this and everything worked fine. Instead of putting folder access on the entire files folder, I enabled it on the specific subfolders omitting the vs2019 folder. I hope this save somebody many frustrating hours.
Related
Recently, I am trying to write codes to get trained in sequential file access. I learned it well, but the issue is kinda stressing me out. I have a code that work 100%, and its task is "make a code that prints array elements inside a file", the text file name is "numric".
#include <iostream>
#include <fstream>
using namespace std;
int main(){
int a[3]={5000,6000,7000};
ofstream outfile("numric.txt");
if (outfile.is_open()){
outfile << "employee payroll:" << endl;
for (int i=0;i<3;i++)
outfile << a[i] << endl;
outfile.close();
} else
cout << "failed!" << endl;
return 0;
}
I implemented the code in two different program (VS Code, dev++) and it works fine. It found the file, but when I open the text file, there isn't any text inside it. The code should do its work by finding some text inside it if I opened it.
Note:sometimes when the code works, the antivirus pops a message saying it found an item that doesn't look safe in the program and deletes it.
I am using Notepad++ with TDM-GCC. My computer is 2Gb RAM Windows 10 32 bit 3.30 GHz. When I execute my simple program, it shows error.
Access is denied.
An attempt was made to execute the below command.
Command: D:\Deane\Github\CPP_Projects\AnalysisName\main.bat
Arguments:
Error Code: 5
Image of the error
I follow this: ShellExecuteEx function always returning error code 5 (C++)
Program's code (if necessary):
/* AnalysisName Program - written by Vo Tran Nha Linh */
#include <iostream> // Input and Output library.
using namespace std;
int main()
{
string name;
cout << "Hello friend! It's nice to meet you, what is your name?" << endl; // Ask the name.
cin >> name; // Input name.
cout << "Hello " << name << ". Your name is interesting." << endl; // Have a greeting.
cout << "Your name has " << name.length() << "letters." << endl; // Show the name's length.
cout << "It starts with " << name.front() << "letter." << endl; // Show the first letter of the name.
cout << "It ends with " << name.back() << "letter." << endl; // Show the last letter of the name.
return 0;
}
But it doesn't active, please give me a help. Thank you very much!
My problem solved!
I miss Visual C++ Redistributable 2008 and 2010.
Moderators please close my topic. Thank you!
Navigate to C:\Program Files (x86)\Notepad++ Right mouse click the Notpad++.exe file click properties & under the compatability Tab UN-TICK run the program as administrator box DONE.
Refer to this link.
this solved it for me:
right click on the file that won't run (in my case a .cmd file)
check the 'Unblock' checkbox next to the remark "This file came from another computer and might be blocked to help protect this computer"
This question already has answers here:
How to get the current user's home directory in Windows
(4 answers)
Closed 7 years ago.
So I know how to make it, I just want it to open the file without specifying the path.
For example: I have it in
C:\Users\\(me)\Desktop\Projects\BCs\BSCV2\bin\Debug\BSC.exe
but if I give it to a friend, he has a different username, (him) for example, so the command won't be able to execute even if he has it on his desktop because the path isn't valid anymore.
Here's a part of the code:
#include <iostream>
#include <windows.h>
using namespace std;
int main()
{
int a;
cout << endl;
cout << " This window is used for launching the programs." << endl;
cout << " Type in the number of the program you want to use and press Enter." << endl;
cout << endl;
cout << " 1) BSCV2 << endl;
cout << endl;
cout << " "; cin >> a; cout << endl;
cout << endl;
if (a == 1){
system ("start C:\\Users\\(me)\\Desktop\\Projects\\BCs\\BSCV2\\bin\\Debug\\BSCV2.exe");
system ("pause");
}
return 0;
}
How can I make it run on anyone's PC, regardless of where they put it?
Also, if you could re-write my code as an example, I'd appreciate it.
You will need to get the "home" directory for the current user logged in. Reference this post: How to get the current user's home directory in Windows, or this one: How can I find the user's home dir in a cross platform manner, using C++?
However, are you absolutely sure that all users (on their respective machines) running your application will have the exact directory path to the executable you're trying to call (\Desktop\Projects\BCs\BSCV2\bin\Debug\BSCV2.exe)?
You may be better off writing a function to search for the executable, or ask the user to specify where it is.
So if I write a piece of code like this:
string name, feeling;
cout << What is your name?" << endl;
cin >> name;
cout << "Hello, " << name << "!"<<endl;
cout << "So how are you feeling today?" << endl;
cin >> feeling;
I get the output:
What is your name?
James (input from user)
Hello, James!
So how are you feeling today?`
But I want it to remove the first message and the input, so the user will get just this on the console window:
Hello, James!
So how are you feeling today?
As long as you stay on the same line, it's usually pretty easy to use a combination of \b (back-space) and/or \r (carriage return without new-line) and some spaces to go back to the beginning of a line and write over what's displayed there.
If you need to do (much) more than that, you can use the Windows console functions such as SetConsoleCursorPosition and FillConsoleOutputCharacter to write data where you want it, overwrite existing data, etc.
If you care about portability to Linux and such, or already know how to program with curses, you might prefer to use PDCurses instead. This is basically a re-implementation of the ncurses programming interface on top of the Windows console functions.
If you work on windows environment, try this
#include <iostream>
int main()
{
std::cout << "This is the first line";
system("cls");
std::cout << "This is the line on clear console" << std::endl;
return 0;
}
This question already has answers here:
C++: Where does the ofstream class save the files to?
(4 answers)
Closed 8 years ago.
I want to use C++ to create files which I can then export for use elsewhere. The following code seems to create files okay, in the sense that I can write data into the file and then read it again later in a C++ program. When I try to actually find created file so as to use it, however, it is nowhere to be found..
using namespace std;
int main () {
ofstream myfile ("example3.dat");
if (myfile.is_open()){
myfile << 3335 << " " << 64 << " " << 43 << 9 << 5 << 6 << 5 << 4 << 6;
myfile.close();
}
else cout << "Unable to open file";
ifstream myfile2 ("example3.dat");
int b;
myfile2 >> b; cout <<b;
myfile2 >> b; cout <<b;
myfile2.close();
return 0;
}
The files will be created in the process working directory.
In case the process is being run from an IDE (e.g. Visual Studio), the working directory can be different from the executable file path. You should check the project properties to find out the actual path.
Certainly you can search for the file as suggested in comments above, but you might be better off specifying an absolute path in your program, so that you know where it's being written.
The formatting of the path is OS-specific, but maybe
/tmp/example.dat
C:\Windows\Temp\example.dat
for Linux and Windows respectively (but you will need to decide for yourself; these are just examples).