You are not logged in.

Dear visitor, welcome to QtForum.org. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

1

Sunday, April 8th 2012, 8:53pm

fstream doesn't work

Hello everybody, my english is so bad, sorry, blah-blah-blah...
I'm trying to input from file with "cstdio" library, but this doesn't work. I have several important reasons for using "cstdio".

Source code

1
2
3
4
5
6
    FILE *fin;
    fin = fopen("tst.txt","r");
    if (!fin) {
        int i = 0;
    }
    fclose(fin);

The fin variable is false. What should i do? The "fstream" way also didn't work the same.

2

Sunday, April 8th 2012, 8:59pm

I would be interested to know why cstdio is needed instead of fstream. I assure you that fstream does work.

Probably your program is not run from same location as tst.txt
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

3

Sunday, April 8th 2012, 9:15pm

I use stdio because of i use almost c-code created by "Bison GNU" utility. I try with fstream:

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include "mainwindow.h"
#include <fstream>
using namespace std;
#include <stdio.h>

int main(int argc, char *argv[])
{
 //   FILE *fin;
   // fin = fopen("tst.txt","r");
    ifstream fin ("tst.txt");
    if (!fin) {
        int i = 0;
    }
//    fclose(fin);
    fin.close();

    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    
    return a.exec();
}

this doesn't work. I checked and even recreate tst.txt.
My windows console project works correctly with this code. I also find out that cstring library for VS 2010 and cstring for qt are different :)

4

Sunday, April 8th 2012, 9:32pm

As I said,

Quoted

Probably your program is not run from same location as tst.txt

If your file is there, fstream/fopen will get it => your file isnt where your program is run from.


There is no 'cstring' in Qt.

You are aware that when debugging, the exe is not in same place as the project file, right?

Have you tried passing an absolute path to your fopen/fstream? I bet it will work ...
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

5

Sunday, April 8th 2012, 9:41pm

How can i find out where my program looking for file?

6

Sunday, April 8th 2012, 9:43pm

Source code

1
2
3
4
5
6
#include <iostream>

int main(int argc, char *argv[])
{
  std::cout << argv[0];
}


That will show you where your binary is.
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

7

Sunday, April 8th 2012, 10:00pm

Unexpectedly. "C:/Users/Dude/Documents/Visual Studio 2010/Projects/Qt Projects/TJAP_KP_QT-build-desktop-Qt_4_7_4_for_Desktop_-_MinGW_4_4__Qt_SDK__Debug/debug/TJAP_KP_QT.exe" Thanks.