File operations

Pages: 12
May 26, 2020 at 1:17pm
Hi friends, my problem is only about file operations. When i type any file operation code, it doesn't create any text file or doesn't edit that i created manually text files...
I searched on web and i tried all solutions but they didn't effect. One of solutions is gave me an error but I didn't look to what was it(it was something like library error)
Between i sent my codes to friends but they worked on their PCs...
Also i am using Dev C++.(tried visual studio too for solve this problem but this didn't change anything for me)
Here is my (creating and) adding to text file function;
1
2
3
4
5
6
7
8
9
void ekleme() {
        cout << "Eklemek istediginiz kullanici adini giriniz: ";
        cin >> kullaniciAdi;
        ofstream kullaniciDos;
        kullaniciDos.open("Kullanicilar.txt", ios::app);
        kullaniciDos << kullaniciAdi;
        kullaniciDos.close();
        cout<<"Kullanici basariyla eklendi!"<<endl;
    }
Last edited on May 26, 2020 at 1:47pm
May 26, 2020 at 1:36pm
Can you show a minimal example that compiles and reproduces the issue?

And please format your code.
[code]

    // { code here }

[/code]
Last edited on May 26, 2020 at 1:38pm
May 26, 2020 at 1:46pm
Oops sorry, I didn't understand the question clearly.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

#include <iostream>
#include <fstream>
#include <stdlib.h>
#include "string"

void adding() {
string userName;
cout << "Enter your nickname: ";
cin >> userName;
ofstream userFile;
userFile.open("users.txt", ios::app);
userFile<< userName;
userFile.close();
cout<<"The user is succesfully added!"<<endl;
}
May 26, 2020 at 1:51pm
I'm asking for code that I can copy and successfully compile, without needing to modify the code myself.

I cannot reproduce your issue with the following code, based off your excerpt:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <string>

using namespace std;

void adding() {
    string userName;
    cout << "Enter your nickname: ";
    cin >> userName;
    ofstream userFile;
    userFile.open("users.txt", ios::app);
    userFile<< userName;
    userFile.close();
    cout<<"The user is succesfully added!"<<endl;
}

int main()
{
    adding();
}


Running this program the first time successfully creates a file, when I entered "hello" as my nickname.
Running the program the second time successfully then continues appending onto the file.

What happens when you run the above code?
Last edited on May 26, 2020 at 1:52pm
May 26, 2020 at 1:57pm
userFile.open("users.txt", ios::app); This opens the file in the current working directory. I suspect that the CWD isn't where you think it is.
May 26, 2020 at 2:05pm
I created a new source code with above codes on Dev C++(also opened it as administrator) on my D disc and I give all rights to all users to which folder i save it for be sure. "Ide or compiler didn't give me any error" but "the codes didn't create a text file."
May 26, 2020 at 2:05pm
Good suggestion. Assuming Windows, you can add system("cd"); to your program to print your current working directory. Just as a quick check for debugging.
(On *nix this is system("pwd");)
May 26, 2020 at 2:25pm
I added it like that;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <string>

using namespace std;

void adding() {
    string userName;
    cout << "Enter your nickname: ";
    cin >> userName;
    ofstream userFile;
    userFile.open("users.txt", ios::app);
    userFile<< userName;
    userFile.close();
    cout<<"The user is succesfully added!"<<endl;
}

int main()
{
	cout<<system("cd");
    adding();
}


and it is same with my .cpp directory. As you saw on the pic https://imgur.com/a/dZfuZve
May 26, 2020 at 2:35pm
I don't know then. The only thing that is odd to me in that picture is that there is a '0' before your "Enter your nickname" text, which suggests that you're running code that doesn't match the above code.

What shows up when you do,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
    ofstream userFile;
    userFile.open("users.txt", ios::app);
    if (!userFile)
    {
        cout << "failure\n";
    }
    else
    {
        cout << "success\n";
    }

    if (userFile<< userName)
    {
        cout << "write success\n";
    }
    else
    {
        cout << "write failure\n";
    }
Last edited on May 26, 2020 at 2:37pm
May 26, 2020 at 2:47pm
I copied and pasted the last codes from my program already so they must be same. Also the you said quick check but it took about one minute, i don't know is that normal because normally it works after a few seconds.

And the output;

D:\TEST
0Enter your nickname: ali
failure
The user is succesfully added!
May 26, 2020 at 2:50pm
After you edit your message i tried the new one and the result a bit changed;

D:\TEST
0Enter your nickname: nick
success
write success
The user is succesfully added!

--------------------------------
Process exited after 46.77 seconds with return value 0
Last edited on May 26, 2020 at 2:51pm
May 26, 2020 at 2:55pm
But still no file exists in D:\TEST? I'm at a dead-end.

Edit:
Just as one more measure.
Add system("dir"); to the end of your program. What files does it show?
Last edited on May 26, 2020 at 2:57pm
May 26, 2020 at 3:09pm
Here;

D:\TEST
0Enter your nickname: ss
success
write success
The user is succesfully added!
Volume in drive D is Depo

Directory of D:\TEST

26.05.2020 18:06 <DIR> .
26.05.2020 18:06 <DIR> ..
26.05.2020 18:06 713 deneme.cpp
26.05.2020 18:06 1.922.352 deneme.exe
26.05.2020 18:07 28 users.txt
3 File(s) 1.923.093 bytes
2 Dir(s) 204.726.333.440 bytes free

--------------------------------
Process exited after 118.6 seconds with return value 0
Last edited on May 26, 2020 at 3:14pm
May 26, 2020 at 3:13pm
After this result, I opened "Show secret file, folder or drivers" from general folder options. Still I can't see the txt here.
May 26, 2020 at 3:15pm
Well, the file exists in your system. So your program is working correctly.

Do you have a screenshot of your File explorer?
Last edited on May 26, 2020 at 3:28pm
May 26, 2020 at 3:31pm
Hi it's me again, i don't know why but "thorunkilici" can't reply or creates new topic. I guess because of i tried to paste imgur link again for show inside of the TEST folder.

There are only two files still, deneme.cpp and deneme.exe . So I can't see still...
May 26, 2020 at 3:43pm
After open "Show secret file, folder or drivers", I can see even secret temp files but i can't see the text file...
May 26, 2020 at 3:48pm
If you open cmd, then cd to D:\TEST, and then do dir, does the file show up in the list?
- If it does, that means it's indeed a display issue with your file explorer
- If it does not show up, it means something else is deleting your file

Last edited on May 26, 2020 at 3:50pm
May 26, 2020 at 4:06pm
Directory of D:\TEST

26.05.2020 18:39 <DIR> .
26.05.2020 18:39 <DIR> ..
26.05.2020 18:06 713 deneme.cpp
26.05.2020 18:39 1.922.352 deneme.exe
2 File(s) 1.923.065 bytes
2 Dir(s) 208.606.879.744 bytes free
May 26, 2020 at 4:12pm
Okay so now my hypothesis is that something else is deleting your users.txt file without your knowledge.

First, temporarily turn off any anti-virus/anti-malware you have, then re-run the program.

If the problem is still happening, you'll have to try some deeper troubleshooting.
There is a program called procmon by sysinternals (owned by Microsoft) that can monitor a variety of events that processes on your computer are doing.
https://docs.microsoft.com/en-us/sysinternals/downloads/procmon

You can filter process monitor to only show file deletions:
https://blog.zensoftware.co.uk/2013/03/06/support-queries-shared-using-process-monitor-to-see-when-files-are-being-deleted/

This should help figure out what process is deleting your file, if that is in fact what's happening (again, just my hypothesis).
Last edited on May 26, 2020 at 4:13pm
Pages: 12