C++ Can't Open&Read File - c++

I can't open the file in c++. I saw a lot of page that asked same question but i can't figured it out. I opened new project for just open the file (As you can see). -I also tryed it with mingw but nothing changed. I try to put the file everywhere in the folder. Please help.
string sayi;
ifstream oku("sayilar.txt");
if (oku.is_open())
{
cout << "Opened";
}
else
{
cout << "Can't opened";
}
int x; cin >> x;

I can't find the real problem but i think it is about my OS settings (Win10). When i write "sayilar.txt.txt" instead of "sayilar.txt" it works.
I've tryed to uncheck "hide extensions of known file types" setting but it stil doesn't work. (There was no answer about that problem in StackOverflow, for that negative voters) So i'll work with "sayilar.txt.txt". Regards.

I think the problem is in your file name so you are hiding the file extension then the first .txt is considered to be a part of file name not extension so if you show file extension then you'll get your file sayilar.txt.txt so try to rename it correctly so:
either hide file extensions and rename the file to sayilar only without adding .txt because it is hidden.
or show file extensions and then rename the file to sayilar.txt so there's no extra extension.
if the problem persists then delete file and create a text file named sayilar : right click in your current folder or in any void place in the desktop -> New -> Text Document. enter the name sayilar.
I wish this will figure out your problem.

Related

Phantom \.txt file in windows

It seems like it is possible to create a file \.txt in windows that can be read, but I'm not able to access it any other way or see if it exists. This seems to only work for \.txt since I can't create other files with backslashes in it such as a\.txt
string filename = "\\.txt";
// make file
ofstream writer(filename);
writer << "This file exists" << endl;
writer.close();
// read file
ifstream reader(filename);
string line;
getline(reader, line);
cout << line << endl;
reader.close();
When I use ls -lia in bash, this file doesn't show up at all, but the program above reads it fine (I can remove the part that creates the file and run it later so the file does persists), how does this work?
On Windows, ofstream writer("\\.txt") creates a file named .txt in the root of the current drive. It is a perfectly valid file name.
ofstream writer("a\\.txt") tries to create a file named .txt in the a subdirectory of the current directory. The a directory must exist in order for it to succeed. Most likely it does not exist, therefore it fails for you.
To create a directory you can use either the mkdir function (which has compatibility problems with other OSes because Windows is not POSIX compatible), or the CreateDirectory WINAPI function, that is Windows specific. After calling CreateDirectoryA("a"), the "a\\.txt" path should work.

FMX 3D and fopen() function

New to C++ Builder 10.4. Migrating from XE4.
A line of code fopen() that works fine in the old environment
InputFileHandle = fopen(FileName, "rb");
does not return a valid pointer in the FMX 3D environment for a file in the same directory that the project is running.
I'd appreciate if you can give me a solution for that.
Try to for check which error occur
#include <errno.h>
....
FILE *InputFileHandle;
if ((InputFileHandle = fopen(FileName, "rb")) == NULL)
printf(" %s \n", strerror(errno));
I can see only two possible trouble:
Permission error, you'll have to fix the file permission.
file does not exist meaning your new framework change the current directory.
Thank you PHE for the answer and the clue you posted for my question. I checked the permissions on the file and they are accessible as needed. The issue was the "change of current directory" and that was causing the file not to be recognized. One solid answer is to specify the path to the file from the root directory( this could be cumbersome for long file paths!). For windows path to be recognized you shall specify forward-slash twice in the related string to be decoded properly, like this "C://dir1//dir2//....//filename". Regards
Thank you PHE for the answer and the clue you posted for my question. I checked the permissions on the file and they are accessible as needed. The issue was the "change of current directory" and that was causing the file not to be recognized. One solid answer is to specify the path to the file from the root directory( this could be cumbersome for long file paths!). For the windows path to be recognized you shall specify backward-slash twice in the related string to be decoded properly, like this "C:\dir1\dir2\....\filename". Regards

C++ doesn't create text file

I'm new to the StackOverFlow.
I'm using Dev-C++ and I wanted to write a text file with my C++ program. But the problem is my program doesn't create a text file.
Instead it creates a file named "026.Writing-to-Files-With-Ofstream.o". (My cpp file's name is: 026.Writing-to-Files-With-Ofstream.cpp)
That's not what I wanted.
Also Dev-C++ doesn't give me any errors or warnings.
I tried using CodeBlocks and still the same result. It creates a ".o" file and not a text file.
Here is my code:
#include <iostream>
#include <fstream>
int main(){
std::ofstream file ("hello.txt");
file << "Hello There!"; //line 5
file.open("hello.txt"); //line 6
return 0;
}
I tried everything. Nothing in the desktop or in my working directory. I switched the lines (5 and 6). I really need your help.
You do too much.
std::ofstream file ("hello.txt");
This line creates ofstream and opens it for writing. When the stream is opened for writing, it's contents on disk is emptied!
file << "Hello There!";
This like prints something to the ofstream. Usually, it is stored in the buffer not yet saved to disk or displayed on screen. (To actually save something to disk, you need endl, flush, or to close the file. The file is closed when the block where it was opened ends, or when you close it explicitly.)
file.open("hello.txt"); //line 6
You again open the file for output, thus emptying it's contents on disk, and emptying the buffer.
}
whatever is in the buffer, saved to disk. But there is nothing in the buffer, because you opened the file again!
You should remove the line 6.
I see 2 problems here:
First, you just compiled the code so the output is a compiled object file called "026.Writing-to-Files-With-Ofstream.o". You need to run it.
Second, the code is not entirely correct. You already opened the file when you did std::ofstream file("hello.txt"); so you do not need line 6. You need to open the file before writing to it. Also You need to close the file after you finished writting: file.close();
I solved it! I searched all the Windows files/folders with the search option on the start menu. It took a long time (10 mins) but i finally found out where the file was. It was in the inside of a folder named "VTRoot". Thanks for the help tho

Xcode c++ cant access file through code

I have added an image "padimage.png" to my resources folder and set add to target and make copy if needed checked. Then in my c++ code I have the following code to check if it can reach the file
std::ifstream my_file("padimage.png");
if (my_file.good())
{
std::cout << "could read file \n";
} else {
std::cout << "could not read file \n";
}
This fails meaning I can't reach the file. I have checked in the debug build folder and the image is there under the resources folder, I have also tried alternative paths to the file like "resources/padimage.png" || Resources/padimage.png || ../Resources/padimage.png etc. etc.
I am fairly new to c++ still so I don't quite understand how it is suppose to find files or what path it searches relative to. Also I am sure this is quite an easy problem but I somehow can't solve it.
All help is much appreciated.
Just for your own sanity, do the following before anything else.
char wd[1024];
std::cout << getcwd(wd, sizeof(wd)) << std::endl;
You may be surprised at where you are, and thus why you can't open your file. When running from the IDE you can specify the location of your working directory under the Product/Edit Schemes... area of Xcode (among other places).
Thanks to a suggestion from WhozCraig I have managed to get it working by using the root of the project and then creating a standalone file next to the application like so:
./padimage.png
however this is not ideal. This means I would have resources outside of the project.
But after some trial and error I managed to navigate into the programs package contents by using .app to the package name;
./ProjectName.app/Contents/Resources/padimage.png

Replacing a file with another file but keeping the file the same name.

My programming knowlege is very limited so please take this into account when reading this. I am using Visual C++ MFC and I am looking for a basic function that would overwrite the contents of a file but keep the file the same name. I am sure this is probably fairly simple however I can't seem to find anything online. Thanks in advance for any help.
You can use CFile::Open() there is flags to specify to open an existing file without truncating it. For example if you want to create the file if it not exists, or using the alreading existing without truncating you can use CFile::modeCreate|CFile::modeNoTruncate. You can then seet to the needed position by using CFile::Seek()
It's been a while since I've done any MFC work so I'll just give you the general standard on how to do this in C/C++. This will give you a direction on how to work with MFC.
When you're opening a file, you can choose an "open flag" that tells the file system how to open it. it can be "a" for append, "r" for read, "w" for write over (trunacte), and you can add "b" if it's a binary file.
so to do that just do:
FILE *fp = fopen("my_file.whatever", "wb");
if (fp)
{
//now write to
the file... ....
fclose(fp);
}