-2

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;
Flardryn
  • 457
  • 1
  • 10
  • 25
  • 4
    What's your programs working directory? Is it the same where `sayilar.txt` lives? – πάντα ῥεῖ Dec 07 '16 at 18:18
  • Typically when you do not specify the exact file location it needs to sit next to the exe file or in the directory where the source code is. – NathanOliver Dec 07 '16 at 18:19
  • use the full path of file instead. eg: `"E:/User/desktop/sayilar.txt"` – Raindrop7 Dec 07 '16 at 18:21
  • @πάνταῥεῖ If you're asking about my document structure as i understand simply, My file in the folder (Desktop/MyProject near by Project.sln) and also i copyied it in Desktop/Project/Project near by main.cpp But; if you're asking something complex (about working directory) How can i check it? – Flardryn Dec 07 '16 at 18:23
  • @NathanOliver Yes i tryed to put the txt file near sln-cpp-exe. There were 3 sayilar.txt in solution folder on my last try – Flardryn Dec 07 '16 at 18:25
  • 1
    @FurkanAyık http://stackoverflow.com/questions/143174/how-do-i-get-the-directory-that-a-program-is-running-from – πάντα ῥεῖ Dec 07 '16 at 18:25
  • @Raindrop7 But i must be able to send the project someone else. It must be worked like this. (I know it can be work like this) – Flardryn Dec 07 '16 at 18:25
  • The [`_getcwd` function](https://msdn.microsoft.com/en-us/library/sf98bd4y.aspx) will tell you where the program is going to look for your file. [Be warned that the working directory moves](https://en.wikipedia.org/wiki/Working_directory). – user4581301 Dec 07 '16 at 18:26
  • Negative votes -_- I've been trying to fix this for 2 days. I could found no solution in web, so i don't know what that negative votes for. But i got my advice. I'll keep try. Thanks everyone – Flardryn Dec 07 '16 at 18:45
  • Have you checked the permissions for the text file? – Alessandro Scarlatti Dec 09 '16 at 08:52
  • @AlessandroScarlatti No sir. I'm at work now but if you could say how to do it i can check it after work – Flardryn Dec 09 '16 at 13:12
  • add the full path of file instead of the file's name – Raindrop7 Jan 09 '17 at 22:36

2 Answers2

0

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.

Flardryn
  • 457
  • 1
  • 10
  • 25
0

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.

Raindrop7
  • 3,889
  • 3
  • 16
  • 27
  • 1
    Sir i figured it out somehow. Like i said it was a win10 settings problem. (Using mingw as compiler is an option) Thank you for your answer. I hope it will be helpful someone who will come here. Regards. – Flardryn Jan 10 '17 at 10:17