I'm having a very weird behaviour in Windows 10 enviroment using both VS2008 and VS2015 when I try to read a file using ifstream.
I'm using the following simple code:
#include "stdafx.h"
#include <fstream>
#include <iostream>
#include <string>
int main()
{
std::ifstream ifs("C:\\Users\\dd\\Desktop\\test.txt");
if(ifs.is_open())
{
std::string line;
while(std::getline(ifs,line))
{
std::cout << line << "\n";
}
}
else
{
std::cout << std::strerror(errno) << "\n";
}
return 0;
}
Obviously the file exists.
When I try to read the file .txt I get:
Permission denied
I run Visual Studio as Administrator and I have the permission to read the file because I am able to open it using Notepad++.
If I change the file extension, for instance .test, I am able to read the file content correctly and everything works as expected.
I uninstalled and then reinstalled VS2008, VS2015 and all C++ Redistributable, but nothing changed. Any help is greatly appreciated!
Perhaps your file isn't named test.txt. Did you check that your file is really named test.txt and not test.txt.txt (with a hidden extension)?
Try to list files in command prompt to see what the real file name is:
cmd>
cd C:\Users\dd\Desktop\
dir | findstr test
Onother possibilities:
test.txt could possibly use similarly looking letters from another
language (in your code or in actual file name).
not properly quoted slashes in your code (e.g. C:\Users\\ instead of C:\\Users\\dd\\). Try to change to forward slashes.
Related
I'm on Ubuntu 18.04 with clang-format 9.
I've read the clang-format documentation where it says:
clang-format supports two ways to provide custom style options: directly specify style configuration in the -style= command line option or use -style=file and put style configuration in the .clang-format or _clang-format file in the project directory.
When using -style=file, clang-format for each input file will try to find the .clang-format file located in the closest parent directory of the input file. When the standard input is used, the search is started from the current directory.
No matter how I create the .clang_format file (I've tried with clang-format -style=google -dump-config > .clang_format) or where I put it, if I execute clang-format -style=file <file> it doesn't format anything.
Anyone that have the same issue?
For example, if I have a file hello.cpp:
#include <stdio>
std::string s=" VERY BAD"
"FORMATTING";
int main() {
std::cout<< "Hello world!"<<'\n';
return 0;
}
If I run:
$ clang-format -style=mozilla -dump-config > .clang_format
and even if I don't edit the .clang_format file, then
$ clang-format -style=file hello.cpp
I get the default LLVM formatting style instead of the Mozilla style:
#include <stdio>
std::string s = " VERY BAD"
"FORMATTING";
int main() {
std::cout << "Hello world!" << '\n';
return 0;
}
but if I run $ clang-format -style=mozilla hello.cpp then I get
#include <stdio>
std::string s = " VERY BAD"
"FORMATTING";
int
main()
{
std::cout << "Hello world!" << '\n';
return 0;
}
and the same happens if I move the previously generated .clang_format in the parent directory of the hello.cpp directory.
I've tried anything but it seems that I have to stick with preconfigured styles.
Anyone with the same problem?
Can I get some sort of logging from clang-format?
SOLUTION:
the name of the file must be .clang-format, not .clang_format!
I was creating a configuration file with a wrong name.
It must be named .clang-format, not .clang_format.
clang-format -i <file> is sufficient if you've placed your .clang-format file at the project root. The -i stands for in place. The commands you paste should have spit a formatted file out to standard output. The reason for this is that clang-format won't alter your file by default. Seems weird for a formatter at first, but it's a good safety precaution, in my opinion.
I'm trying to open a .txt file that contains integer values 0-9 each individual number on its own line till the last number is 9. When i run the code i'm using im running into an issue with opening my txt file. Based on the if and else statement provided its failing to open what is causing this and what method can i use to open my notepad .txt file.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
ifstream infile; //Container for the file
infile.open("assignmentG1.txt"); //Name of the file you want to open
string stringFromFile; //string variables to store extracted strings
if(infile.fail()) {//Display error if the file failed to open
cout<<"Input file failed to open";
}
else
{
getline(infile, stringFromFile);
}
cout<<stringFromFile;
}
input file failed to open.
The code works as expected, just make sure you execute the compiled program from the directory where the text file exists.
Assuming the source code is added to a file named main.cpp and running on Linux or macOS with gcc compiler installed, the following works:
echo "Hello" > assignmentG1.txt
g++ main.cpp -o main
./main
Hello
Your file should be at correct path if your project name is suppose project_name so keep the file at /project_name/project_name/assignmentG1.txt .
Assuming you are using visual studio.
I'm using Netbeans 8.2 and I am programming with C++.
I'm trying to read a file. I have put it in the correct project folder of sources and I have used #include <fstream> .
ifstream myfile ("figures.txt");
code...
myfile.close();
I have the file in the correct place. But this message pops up:
Unrecognized file
Then, my other part of code also gives me error, because never detects the file I'm talking about open.
if (myfile.is_open()){
code...
}else{
cout << "file not open" << endl;
}
Auto solved, the .txt must be in the project folder, not in nbproject folder. At least it has worked doing it this way. Any more reccomendations will be accepted!
I know there are a ton of questions pertaining to this subject but I cannot get this to work. The program ran fine on my laptop, but when I try to compile and run it in the the schools Linux lab the program cannot open the file. I have tried defining the absolute file position but nothing has worked. The file name is correct and everything but when I try to run the program it displays "failed". I'm using gedit and compiled the program with bash.
ifstream fin("rainfall.dat"); // If the file cannot open display failed
if(fin.fail()){
cout << "failed" << endl;
return 1;
}
try
#include <errno.h>
if(fin.fail())
perror("open failed ");
this will give you a human readable message for the last error
I have a very simple C++ program with a very simple project setup, but when I run the program I get no output. If I run the program in debug mode, it works perfectly. I am using Eclipse Kepler CDT 32 bit on windows with MinGW. I am somewhat new to eclipse, so it's probably something I did wrong.
The program is:
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
ofstream outfile("testdata.txt");
int main()
{
outfile << "Program Start\n";
cout << "Program Start\n";
return 0;
}
Help!
If the problem is that the program quickly opens and then closes before you can see the output on the screen, then you can just run your program from any shell (CMD on Windows, bash on Linux, etc.). That way, it won't exit once your program ends and you can see the results.
Make sure also that you flush/close your ofstream before your program exits.
The problem is rather not releted to c++ itself. You should check if via "cmd" typying it in "launch menu" after you click start. Find the path of your program, then run it.
For the very beginning it is recommended to spend a few hours with terminal(cmd). To know how things works. After that you will be independent - you will be able to write the code in any IDE. Also simple trick to make it working is to use std::cin.get() . It is prefered to system("pause").
You open the testdata.txt file using the relative path.
And the created file may be created in the project binary output path, where the executable located in.
You can use everything software to check whether a file is created and its created path.
everything
For example, you can type your output file name testdata.txt into everything software to see where output file created.And check if the testdata.txt created in a wrong path or directory.