getline() not opening text file - c++

Hi I am currently using CodeBlocks 13.12 on OSX.
I am trying to open the following .txt file
line 1
line 2
line 3
My code is just:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
cout<<'\n';
std::string line;
ifstream myfile("textex.txt");
if(myfile.is_open())
cout << "File is open";
else
cout << "File not open";
cout<<'\n';
return 0;
}
I have also included the file in the project folder and have tried linking it and compiling it.
When I run the code, it displays"File not open" and I'm not sure why?
I'm new to c++, can someone please explain why this isn't working?

Possibly because the project folder is not set as the working directory. Try specifying the full path.

Instead of
ifstream myfile("textex.txt");
Try
ifstream myfile;
myfile.open("/Users/name/Code/textex.txt"); // Use the full path

Try typing the full path of the file instead of just the file name.
Tell me what happens then.
On another note but unrelated, since you are using the "using namespace" directive, you may as well omit std:: from string just like you did with cin and cout. It is not a big deal, just the look of the code.

When you run your program from Code::Blocks it runs on project folder so your text file must be in your project folder, otherwise text file must be in the folder where your executable is.

Related

Why output text file is not being created after the code is deployed in visual studio?

I wrote a simple code to print the square of input number on a text file. I included fstream package in visual studio IDE, the solution gets build, the text is created where my solution is after executing the file in command prompt. But when I deploy my code and then I install it on another computer, it runs and takes the input in command prompt but doesn't create the text file. Where is the text file gone?
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ofstream outfile("out.txt");
int n;
cin >> n;
outfile << n * n;
}
Nothings wrong with your code. Check your file directory, it usually located at C/Users/User/Source/repos/Yourfilename/Yourfilename or where the cpp files located.

unable to open file in visual studio

im learning c++ using visual studio as ide. I'm currently doing io streams, but when i try to open a file, the program doesn't open the file.
here is the code -
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <vector>
int main()
{
std::ifstream file;
file.open("Text.txt");
if (file.is_open())
{
std::cout << "open" << std::endl;
}
else
std::cout << "not open" << std::endl;
}
i get the output as not open.
any help is appreciated, thanks
you need to keep the file in the directory where the executable is generated by default when using Visual Studio. this is typically located in your solution directory under a folder called Debug/Release depending on your configuration. check the project settings to see where the executable will be generated and copy the file there.
I had a similar problem opening the csv file from cpp file and I found a solution by placing my csv file in the same folder where my source files and especially the cpp file(from which I am trying to open it) is placed.
1)Make sure the path you given is correct.
2)Make sure the file you trying to open is already using by other processes.
Make sure the file exists the path given and and try by giving the full path of the file , if it works ,then make your adjustments.
Try to open the file manually and check if their is any problem with the file.
Check your file is protected by read-only or something like that.
Try to work with the below sample code, and reply me what error you are getting.
file reading sample
file writing sample

ifstream can't find file

I'm trying to open a text file in c++ with ifstream but it won't locate the file even though the file is in the same directory as the .cpp file:
#include <fstream>
std::ifstream textInput("words.txt");
if (!textInput) {
return false;
I've triple checked and the file definately exists and is named correctly. I'm not sure if I'm doing something wrong with ifstream or with the path.
EDIT: I put the file in the current working directory of visual studio, it shows the files relative path as "words.txt", but it still can't find the file.
Find out where you application is running (what is know as the "current working directory") using:
TCHAR NPath[MAX_PATH];
GetCurrentDirectory(MAX_PATH, NPath);
std::cout << NPath << std::endl;
Or if you are using C++17, you can do it using the standard library:
std::cout << std::filesystem::current_path().string() << std::endl;
Make sure that the file is located in the same path as the above snippets print.

Reading Text file line by line in QtCreator does not work

I am trying to code c++ in QtCreator 3.4.2. I create Non Qt Project - Plain C++ Project. I have a code, which tries to read content of text file line by line and output to the console screen. But it does not write anything. I run same code in Visual Studio 2013 it works very well, without any problem. So What is the problem. The code is below.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
ifstream fin("input.txt");
string s;
while(getline(fin,s))
{
cout<<s<<endl;
}
fin.close();
return 0;
}
Check that the input.txt file is in the working directory of the program. Note that the working dir is not the same as the folder the program's .exe is in - you can set it from the project properties.

Why my C++ program can only read absolute directory but not file in the same file folder?

I wrote a small VC++ program using VS2012 and tried to read an text file. I put the file in the release folder. However I cannot read the file until using the absolute file directory. I cannot find useful information online though. The code is like this
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main()
{
cout<<"Hello World!"<<endl;
string test;
ifstream myfile;
myfile.open("E:\\Glus\\Project2\\Release\\bunny.txt");
if(myfile.is_open())
{
string s0,s1;
int x0, x1;
myfile>>s0>>x0;
cout<<s0<<x0<<endl;
myfile>>s1>>x1;
cout<<s1<<x1<<endl;
}
else
{
cout<<"Error in reading file!"<<endl;
}
myfile.close();
cin>>test;
return 0;
}
Thank you!
The path is not relative to the executable, but to the current directory (see _getcwd).
In case you are launching the application from VC, try
"..\\Release\\bunny.txt"
For a real life application, I'd suggest to detect the path of the executable and use it to construct the path to the data file. It is more reliable and safer.
Visual studio does not save your program exactly where you run it, actually it saves it in your a folder within your project folder with same name of your project
you can get current working path using this command :
system("cd");
it can solve your problem to locate your files.