Can't work with text files (not code's fault I think)

I use CodeBlocks and after many trials I simply downloaded a whole project meant to save data in a text file. It works on my other computer, but on my main it simply doesn't. Haven't changed anything in both cases, just ran it straight from the unpacked folder. There is no error in the console, yet the text file remains intact, no matter what I do. Tried reinstalling CodeBlocks, checking if CodeBlocks has permission for all the stuff and still nothing. I'd be glad if it was answered, even though it's not the kind of problem expected here I guess.
Here is the code if somebody was wondering.

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

using namespace std;

string imie, nazwisko;
int nr_tel;

int main()
{
    cout << "Podaj imie: ";         cin>>imie;
    cout << "Podaj nazwisko: ";     cin>>nazwisko;
    cout << "Podaj nr telefonu: ";  cin>>nr_tel;

    ofstream plik;
    plik.open("wizytowka.txt",ios::out);

    plik<<imie<<endl;
    plik<<nazwisko<<endl;
    plik<<nr_tel<<endl;

    plik.close();

    return 0;
}
The file will be created in the current working directory. I'm not sure which working directory Code::Blocks uses default but it's not necessarily the same as where the executable file is located.
Still, that would not explain why it works on the other computer, under the same conditions, just downloaded and dumped on the desktop. There the file is in the folder with the executable file so I suppose it's not connected to the location of the text file.
that would not explain why it works on the other computer
Are you sure? Are you 100% certain? Have you examined the documentation of Code::Blocks to see if it changes the CWD?

My point is that in programming, "that would not explain..." is often the worst thing that you can think. Throw away your certainty and tell the computer to search the whole disk for the file.

If it's still not there then modify the program as follows:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <fstream>
#include <unistd.h>             // for getcwd()
#include <cstring>              // for strerror()
#include <cerrno>               // for errno

using namespace std;

string imie, nazwisko;
int nr_tel;

int main()
{
    char buf[1000];
    cout << "current directory is " << getcwd(buf, sizeof(buf)) << '\n';

    cout << "Podaj imie: ";         cin>>imie;
    cout << "Podaj nazwisko: ";     cin>>nazwisko;
    cout << "Podaj nr telefonu: ";  cin>>nr_tel;

    ofstream plik;
    plik.open("wizytowka.txt",ios::out);

    plik<<imie<<endl;
    plik<<nazwisko<<endl;
    plik<<nr_tel<<endl;

    if (!plik) cout << "plik is bad\n";
    plik.close();
    cout << "errno " << errno << ": "
         << strerror(errno) << '\n';
    return 0;
}

Followed the advice, still nothing. Supposedly the file should be created (if nonexistent) in the folder of the project, I searched the whole disk and still nothing. Tried creating a file in the location but still it won't be overwritten.
What operating system are you using?
Are you running the program from the IDE or from a terminal/console?
Try this program. What output do you get?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <fstream>
using namespace std;

int main() {
    ofstream f("file.txt");
    if (!f) {
        cerr << "Can't open file.\n";
        return 0;
    }
    f << "hello?\n";
    if (!f) {
        cerr << "Error writing to file.\n";
        return 0;
    }
    cerr << "Everything okay\n";
}

Last edited on
Win 7 64. I run it from the IDE. After running what you wrote it says that everything's okay.
I can't believe I have to ask this: is "file.txt" in the directory?
Last edited on
Of course.
Then the problem's fixed.
Yeah, but the file is empty so the problem with overwriting the file isn't solved. I don't know how this is possible though, as the program should tell me something isn't right. I searched the disk, some other file.txts aren't hidden anywhere.
Well, after switching the PC on and off a few times it gave in. Goddamn should I have refrained from digging into the problem so much.

...Only worked once. No idea what went wrong afterwards, the problem's the same.
Last edited on
That sounds like disk corruption then. Run a disk check.
Might give up on that after all those methods. Guess formatting is an option.
There isn't some other program that uses the file? E.g. you haven't opened it in code::blocks or something like that?
Seems to be cool after cleaning some stuff from the disk...So I'd consider this solved, although before it also seemed alright and then went down again. Anyway, thanks for the help.
Topic archived. No new replies allowed.