C++ How to open an input file with another directory? - c++

My project is being worked in through my school's intranet.
I basically am running the project through Windows Powershell by using ssh login to the intranet.
The data file is located at: /user/name_of_teacher/data/file_name.dat.
I have tried:
ifstream infile;
infile.open("/user/name_of_teacher/data/file_name.dat");
But it doesn't work.

If you want to write in this file:
ofstream file("C:/user/name_of_teacher/data/file_name.dat");
file << text;
If you want to read in this file:
ifstream file("C:/user/name_of_teacher/data/file_name.dat");
file >> text;
Remember to write:
using namespace std;
at the start of the file cpp but after the libraries.
The principle error was that you had to use the absolute location:
C:\Users ecc.

Related

Making Log File

I'm trying to create a log file using the code below, but It's not working and I'm unable to get the logs as the file is not created!
#include <iostream>
#include <fstream>
using namespace std;
ofstream myfile;
int main () {
myfile.open ("example.txt");
if (!myfile.is_open())
{
cerr << "Failed to create Log" << endl;
}
myfile << "Writing this to a file.\n";
myfile.close();
return 0;
}
For me, it is working fine. compiled with g++ on ubuntu.
If your are using linux, try locating example.txt as may be the working directory is different. In windows, you can search for the file in your computer.
I managed to get your code working, as it worked out of the box. Files created adhere to an odd structure, so you'll need to put your assembly version as a prefix either through a define or a variable.
You can find example.txt up a directory from Debug/Release, next to your .vcxproj file, and in the folder next to your .sln file.
Tinkeroonie is the name of my project, yours might be LoggerTest or something like that.
Highlighted is the folder you need.

Why is my fstream not creating data1.txt file?

#include <iostream>
#include <fstream>
using namespace std;
int main()
{
fstream dataFile("data1.txt");
if(dataFile.fail())
{
cout<<"Unable to open file"<<endl;
exit(1);
}
double value = 0;
while(dataFile >> value)
cout<<"Read: "<< value << endl;
dataFile.close();
}
The output of this program is always Unable to open file.
please tell me why im not able to make files. im using a mac book pro (IED: coderunner 2) dont know if that makes a difrence
Depending on your IDE, the program might not be running in the location you think it is. Try putting your text file in different locations in the project hierarchy. It's likely that it's not in the right spot.
You need to verify the location of the file first. For example when running your program from a VS IDE you would not place the text file inside a Debug or Release folder. You would place your text file inside a folder from which your IDE launches an executable. If you ran a standalone executable then you would place your txt file in the same folder with the executable.
Since you are using your file for the input and not the output you could use the std::ifstream instead:
std::ifstream dataFile("data1.txt");
You don't have any code that creates a file.
To create a file:
1) Use std::ofstream
2) Use std::fstream with the correct mode.
Although not necessary, you should write data to the file to help create it.
The code in your post is reading from a file. You open a file, and the open may not be successful. You should have your program tell fstream the path to the file (to remove ambiguities).

file.open() on linux doesn't open my file, how to solve?

#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream file;
file.open("./cp.txt");
if(file.good())
{
cout << "done!";
}
else
{
cout << "fail";
}
return 0;
}
why does my code can't cp.txt file? it is in project folder. i'm using gnu/linux.
as open i mean the program will open it in some text editor i.e:. leafpad
as open i mean the program will open it in some text editor i.e:. leafpad
That's not going to happen.
When you open an ifstream object it means the file is open for reading by that object, it does not mean a text editor is launched and displays the file!
Maybe what you want is:
system("leafpad cp.txt");
The system function runs another command, in this case it runs the command to launch leafpad with your file as an argument.
Note that the file will be searched for in the current working directory of your program, which is not the same as your "project directory". If you don't know what the current working directory is when your program gets run then you will need to provide an absolute path to the file, not a relative path like cp.txt
you have mentioned having a project folder. if you use an IDE,
it might change the current directory of the running executable.
try deleting the file and creating the file within your code eg:
ofstream ofile;
ofile.open("./cp.txt");
ofile.close();
if you get an output done! then search for the file.

Path to file used as environment variable

My C++ code for outputting content to a file is:
fstream myfile;
myfile.open("file.txt", ios::in);
if(!myfile.is_open()) cout << "error";
I want my code to function in the following way:
If file.txt is located in the same directory as the built executable or the cpp file, then my code should open the file.
Currently this is not the case. When I checked the location of the current working directory with getcwd it showed /User/myusername instead of /User/myusername/Desktop where all the files actually are located.

Reading from folder in working directory C++

I want to read from a text file which is saved in a folder in my working directory. But unless the text file is in the working directory, it wont open.
Im using a line like this one. what should it be changed to?
ifstream myfile ("./folder/example.txt");
If you are on Windows, use this format to open the file.
ifstream myfile ("C:\\Book\\file.txt") ;
Supposing your file is in Book folder of C Drive.
First write the Directory and then the subsequent folders with double slashes in between till you reach the text file.
Though it is better if you put in your project directory itself, using this.
ifstream myfile ("file.txt") ;
You can access the directory your program is, by simply writing
ifstream myfile ("example.txt");
If example.txt is in the same directory as your program.
Note that this may not work if you can't save in this directory.