Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Problem with ifstream and Qt Creator
Forum Updated to NodeBB v4.3 + New Features

Problem with ifstream and Qt Creator

Scheduled Pinned Locked Moved Solved General and Desktop
23 Posts 6 Posters 13.4k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    BadHombre
    wrote on 7 Mar 2017, 21:48 last edited by
    #1

    Hi Guys :9 I have some Problems with Qt Creator hope that sb can help me.

    my code is looking like that:

    ifstream file("fileName", ios::in | ios::binary | ios::ate);
    
            file.seekg(0, std::ios::end);
            int size = file.tellg();
            char * buffer = new char [size];
    

    At the Part where i am creating the char i get this Error message.
    http://fs5.directupload.net/images/170307/ybo2dpco.png

    i tested the cde with visual studio 2014 and it works perfectly :/ so i'm not sure where the Problem is :/

    For the people who don t understand germen The Error says :

    The debug process were stopped because of an Exception.
    In the Thread 0 .... see picture

    Anyone an idea ?

    1 Reply Last reply
    0
    • B Offline
      B Offline
      BadHombre
      wrote on 8 Mar 2017, 17:24 last edited by
      #22

      Okay Thanks for your Help Guys :) I also found a way to realise it with ifstream for the people who maybe need it one day...

       QStringList filenames = QFileDialog::getOpenFileNames(
              this,
              tr("Choose "),
              "C://",
              "All files (*.*)");
      
          std::string current_locale_text = filenames[0].toLocal8Bit().constData();
      
          ifstream file(current_locale_text.c_str());
      
      1 Reply Last reply
      0
      • B Offline
        B Offline
        BadHombre
        wrote on 7 Mar 2017, 21:53 last edited by
        #2

        What i've done now is that i changed the part of the code and now it works but i get the same Error Message at another point of the code... maybe it hast some relations ...

                file.seekg(0, std::ios::end);
                int size = file.tellg();
                char * buffer = new char [100000000]; // muss geƤndert werden
                file.read(buffer, size);
                file.close();
                double* double_values = (double*)buffer;//reinterpret as doubles
                vector<double> buffer2(double_values, double_values + (size / sizeof(double))); /
        
        

        At the las line i get the error message now.

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 7 Mar 2017, 22:18 last edited by
          #3

          Hi,

          You should run the debugger on your application to see exactly where it fails and get a stack trace.

          That said, you're doing some dangerous assumptions in your code. You are allocating an arbitrary big chunk of memory while reading some dynamically changing number of bytes.

          Then you generate a vector (std::vector ?) from that data by passing the start and end addresses of buffer to the constructor which make think that you wanted to use the iterator based constructor.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1
          • B Offline
            B Offline
            BadHombre
            wrote on 8 Mar 2017, 03:54 last edited by
            #4

            So i debugged the code and tried to step in at the Position where it fails ... but i can't step in so the Problem seems to be

             char * buffer = new char [size];
            

            but how could it be that it works on visual studio and crashed on qt creator ? :/
            Do you have maybe an idea what i can do now ?

            A 1 Reply Last reply 8 Mar 2017, 04:04
            0
            • B Offline
              B Offline
              BadHombre
              wrote on 8 Mar 2017, 03:59 last edited by
              #5

              In the second example where i tried to create the vector it creash at the position

              [[noreturn]] void _Xlen() const
              		{	// report a length_error
              		_Xlength_error("vector<T> too long");
              		}
              
              1 Reply Last reply
              0
              • B BadHombre
                8 Mar 2017, 03:54

                So i debugged the code and tried to step in at the Position where it fails ... but i can't step in so the Problem seems to be

                 char * buffer = new char [size];
                

                but how could it be that it works on visual studio and crashed on qt creator ? :/
                Do you have maybe an idea what i can do now ?

                A Offline
                A Offline
                ambershark
                wrote on 8 Mar 2017, 04:04 last edited by
                #6

                @BadHombre What is the value of size when it crashes?

                Is your Qt Creator using mingw or visual c++ for it's compiler? That could make all the difference. VS may handle weird size cases like -1 or 0 whereas mingw may not. Neither of those are valid and visual studio may just be ok with that. VS tends to be pretty loose in conforming to standards.

                My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

                1 Reply Last reply
                0
                • B Offline
                  B Offline
                  BadHombre
                  wrote on 8 Mar 2017, 04:34 last edited by BadHombre 3 Aug 2017, 04:35
                  #7

                  @ambershark size is an integer and is the number of chars i need to store the elements from the File in the buffer. Its sth like 6000.
                  My Qt Creator is using Desktop QT 5.7.1 MSVC 2015_64 bit.

                  A J 2 Replies Last reply 8 Mar 2017, 05:11
                  0
                  • B BadHombre
                    8 Mar 2017, 04:34

                    @ambershark size is an integer and is the number of chars i need to store the elements from the File in the buffer. Its sth like 6000.
                    My Qt Creator is using Desktop QT 5.7.1 MSVC 2015_64 bit.

                    A Offline
                    A Offline
                    ambershark
                    wrote on 8 Mar 2017, 05:11 last edited by
                    #8

                    @BadHombre Ok let's take std c++ out of it and use Qt.. This is the Qt forum after all.

                    Change your file reading code to:

                    QFile f("myfile");
                    QByteArray fileData;
                    if (f.open(QIODevice::ReadOnly))
                    {
                       fileData = f.readAll();
                    }
                    

                    Another thing you could do is throw it in the debugger and show us the backtrace output.

                    My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

                    1 Reply Last reply
                    1
                    • B BadHombre
                      8 Mar 2017, 04:34

                      @ambershark size is an integer and is the number of chars i need to store the elements from the File in the buffer. Its sth like 6000.
                      My Qt Creator is using Desktop QT 5.7.1 MSVC 2015_64 bit.

                      J Offline
                      J Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on 8 Mar 2017, 05:32 last edited by
                      #9

                      @BadHombre said in Problem with ifstream and Qt Creator:

                      Its sth like 6000

                      Did you really check the value of size? That's what a debugger is for.
                      Are you sure your app crashes exactly at "char * buffer = new char [size];"?
                      Also you do not seek to the beginning of the file before reading from it!

                      file.seekg(0, std::ios::end);
                      int size = file.tellg();
                      char * buffer = new char [100000000]; // muss geƤndert werden
                      file.read(buffer, size);
                      

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      A 1 Reply Last reply 8 Mar 2017, 18:35
                      1
                      • B Offline
                        B Offline
                        BadHombre
                        wrote on 8 Mar 2017, 06:32 last edited by
                        #10

                        @jsulm You're right i get an wrong size ... i used now qDebug()<< size and it prints -1 ... but why ... in Visual Studio its working ....

                        char * buffer;
                        	long size;
                        	file.seekg(0, std::ios::end);
                        	size = file.tellg();
                        	file.seekg(0, std::ios::beg);
                        	buffer = new char[size];
                        	file.read(buffer, size);
                        	file.close();
                        
                        	double* double_values = (double*)buffer;//reinterpret as doubles
                        	vector<double> buffer2(double_values, double_values + (size / sizeof(double)));
                        
                        J 1 Reply Last reply 8 Mar 2017, 06:42
                        0
                        • B BadHombre
                          8 Mar 2017, 06:32

                          @jsulm You're right i get an wrong size ... i used now qDebug()<< size and it prints -1 ... but why ... in Visual Studio its working ....

                          char * buffer;
                          	long size;
                          	file.seekg(0, std::ios::end);
                          	size = file.tellg();
                          	file.seekg(0, std::ios::beg);
                          	buffer = new char[size];
                          	file.read(buffer, size);
                          	file.close();
                          
                          	double* double_values = (double*)buffer;//reinterpret as doubles
                          	vector<double> buffer2(double_values, double_values + (size / sizeof(double)));
                          
                          J Offline
                          J Offline
                          jsulm
                          Lifetime Qt Champion
                          wrote on 8 Mar 2017, 06:42 last edited by
                          #11

                          @BadHombre First, you should check whether file was opened:

                          ifstream file("fileName", ios::in | ios::binary | ios::ate);
                          if (file) {
                          ...
                          }
                          

                          https://forum.qt.io/topic/113070/qt-code-of-conduct

                          1 Reply Last reply
                          2
                          • B Offline
                            B Offline
                            BadHombre
                            wrote on 8 Mar 2017, 07:00 last edited by BadHombre 3 Aug 2017, 07:02
                            #12

                            @jsulm it doesn t open the file ... but i checked also the file name and its correct. The .bin file should also be right because with visual studio it works ...

                            QStringList Filename = QFileDialog::getOpenFileNames(
                                    this,
                                    tr("Choose your File"),
                                    "C://",
                                    "All files (*.*);;BIN (*.bin)");
                            

                            @ambershark sorry i didn 't saw your post i will try it.

                            1 Reply Last reply
                            0
                            • B Offline
                              B Offline
                              BadHombre
                              wrote on 8 Mar 2017, 07:05 last edited by
                              #13

                              @ambershark it doesn ' open the file ... so it should be sth wrong with the path... I printed the Filename which i get with Qstringlist and it looks like that :

                              ("E:/test/file.bin")
                              
                              J 1 Reply Last reply 8 Mar 2017, 07:12
                              0
                              • B BadHombre
                                8 Mar 2017, 07:05

                                @ambershark it doesn ' open the file ... so it should be sth wrong with the path... I printed the Filename which i get with Qstringlist and it looks like that :

                                ("E:/test/file.bin")
                                
                                J Offline
                                J Offline
                                jsulm
                                Lifetime Qt Champion
                                wrote on 8 Mar 2017, 07:12 last edited by jsulm 3 Aug 2017, 07:13
                                #14

                                @BadHombre You should try to remove ios::ate as it is for output but you're opening for reading (ios::in).
                                Also I'm not sure ifstream supports / in paths on Windows (Qt does). Try to replace / with \\.
                                From here: http://www.cplusplus.com/reference/fstream/ifstream/ifstream/
                                "filename
                                A string representing the name of the file to open.
                                Specifics about its format and validity depend on the library implementation and running environment."

                                https://forum.qt.io/topic/113070/qt-code-of-conduct

                                1 Reply Last reply
                                1
                                • thamT Offline
                                  thamT Offline
                                  tham
                                  wrote on 8 Mar 2017, 07:19 last edited by tham 3 Aug 2017, 07:26
                                  #15

                                  Could you show us your .pro file(minimum one please)?Which version of Qt, QtCreator your are using?Are you sure you are using the same compiler to compile your codes?

                                  Besides, you do not need to use raw pointer, there are std::string or std::vector could help you release the memory, in your case std::vector maybe a better choice, because it looks more like a buffer compare with std::string.

                                  if(!file.is_open()){
                                    std::cerr<<"cannot open file\n";
                                    return -1;
                                  }
                                  
                                  auto const size = file.seekg(0, std::ios::end);
                                  std::vector<char> buffer(size );
                                  file.seekg(0, std::ios::beg);
                                  file.read(&buffer[0], size); //same way to feed the data into std::string
                                  file.close();
                                  

                                  You could use std::unique_ptr too

                                  auto buffer = std::make_unique<char[]> p(file.tellg());
                                  file.read(buffer.get(), size); //same way to feed the data into std::string
                                  

                                  However, I would prefer QFile to ease my pain rather than using std::ifstream

                                  Also I'm not sure ifstream supports / in paths on Windows (Qt does). Try to replace / with \

                                  / works on my laptop(windows 10 64bits), using Qt5.7, visual c++ 2015 with update 3.

                                  1 Reply Last reply
                                  1
                                  • B Offline
                                    B Offline
                                    BadHombre
                                    wrote on 8 Mar 2017, 08:18 last edited by
                                    #16

                                    Thanks guys for the Help ! ... so the Problem is that getOpenFileNames gives me the Path like that :

                                    ("E:/test/file.bin")
                                    

                                    to use it in if stream i need zu 2 Slashes after the Drive.. sth like :

                                    ("E://test/file.bin")
                                    

                                    I tried to use :

                                    QFile f("myfile");
                                    Q
                                    if (f.open(QIODevice::ReadOnly))
                                    {
                                    
                                    }
                                    

                                    but it doesn't open the File ... Does sb know how i get a second slash after the without Hardcoding :/ Or why doesn 't qread work ?

                                    J 2 Replies Last reply 8 Mar 2017, 08:20
                                    0
                                    • B BadHombre
                                      8 Mar 2017, 08:18

                                      Thanks guys for the Help ! ... so the Problem is that getOpenFileNames gives me the Path like that :

                                      ("E:/test/file.bin")
                                      

                                      to use it in if stream i need zu 2 Slashes after the Drive.. sth like :

                                      ("E://test/file.bin")
                                      

                                      I tried to use :

                                      QFile f("myfile");
                                      Q
                                      if (f.open(QIODevice::ReadOnly))
                                      {
                                      
                                      }
                                      

                                      but it doesn't open the File ... Does sb know how i get a second slash after the without Hardcoding :/ Or why doesn 't qread work ?

                                      J Offline
                                      J Offline
                                      jsulm
                                      Lifetime Qt Champion
                                      wrote on 8 Mar 2017, 08:20 last edited by
                                      #17

                                      @BadHombre Why do you want to use ifstream? If you're already using Qt why not use QFile,...?

                                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                                      1 Reply Last reply
                                      1
                                      • B BadHombre
                                        8 Mar 2017, 08:18

                                        Thanks guys for the Help ! ... so the Problem is that getOpenFileNames gives me the Path like that :

                                        ("E:/test/file.bin")
                                        

                                        to use it in if stream i need zu 2 Slashes after the Drive.. sth like :

                                        ("E://test/file.bin")
                                        

                                        I tried to use :

                                        QFile f("myfile");
                                        Q
                                        if (f.open(QIODevice::ReadOnly))
                                        {
                                        
                                        }
                                        

                                        but it doesn't open the File ... Does sb know how i get a second slash after the without Hardcoding :/ Or why doesn 't qread work ?

                                        J Offline
                                        J Offline
                                        jsulm
                                        Lifetime Qt Champion
                                        wrote on 8 Mar 2017, 08:32 last edited by
                                        #18

                                        @BadHombre said in Problem with ifstream and Qt Creator:

                                        QFile f("myfile");

                                        Did you check why it doesn't open the file? What is myfile? Absolute path? Relative path?
                                        Did you check http://doc.qt.io/qt-5/qiodevice.html#errorString ?

                                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                                        1 Reply Last reply
                                        2
                                        • B Offline
                                          B Offline
                                          BadHombre
                                          wrote on 8 Mar 2017, 11:01 last edited by BadHombre 3 Aug 2017, 11:02
                                          #19

                                          @jsulm I tried now to use QFile ... and it seems to be working... but do you kno how i get the doubles of the file... For example read the doubles into a vector.. is this possible ?

                                          FlotisableF J 2 Replies Last reply 8 Mar 2017, 11:12
                                          0
                                          • B BadHombre
                                            8 Mar 2017, 11:01

                                            @jsulm I tried now to use QFile ... and it seems to be working... but do you kno how i get the doubles of the file... For example read the doubles into a vector.. is this possible ?

                                            FlotisableF Offline
                                            FlotisableF Offline
                                            Flotisable
                                            wrote on 8 Mar 2017, 11:12 last edited by
                                            #20

                                            @BadHombre
                                            read QDataStream document

                                            1 Reply Last reply
                                            1

                                            1/23

                                            7 Mar 2017, 21:48

                                            • Login

                                            • Login or register to search.
                                            1 out of 23
                                            • First post
                                              1/23
                                              Last post
                                            0
                                            • Categories
                                            • Recent
                                            • Tags
                                            • Popular
                                            • Users
                                            • Groups
                                            • Search
                                            • Get Qt Extensions
                                            • Unsolved