Absurd QT File Handling Issue(QFile)
-
@Sabhya-Sood said in Absurd QT File Handling Issue(QFile):
Please let me know if I'm missing on something.
You should open a file before trying to read from it (but that's also true for plain c(++) file handling). And no you don't open the file object before trying to read it - c++ basics are missing here I would guess.
-
@Sabhya-Sood
@Christian-Ehrlicher has just posted ahead of me: in your working cases you callQFile::open()
, in your non-working case you do not.QFile
does not do "absurd" file handling, if you don't open the file it won't behave as you are expecting.... -
Hi @Christian-Ehrlicher @JonB I tried what you told me and added to the
if(! source_file.open( QFile::ReadOnly )){ QMessageBox::warning(this, "Error", "Cannot Open file"); }
to
void MainWindow::on_decode_now_clicked()
before opening QTextStream
but the application still crashes. Sorry if I'm still missing on something but could you help me out? -
Use a debugger to see where and why it crashes.
-
@Christian-Ehrlicher it pops a segmentation fault error
"The inferior stopped because it received a signal from the operating system.Signal name :
SIGSEGV
Signal meaning :
Segmentation fault"The decode() function logic works well on terminal though.
-
Use a debugger ...
-
@Sabhya-Sood and what does the stack trace say?
what was the last line called from your own code? -
Hi @J-Hilk , it says "ReturnHr(2) tid(3e6c) 80010117 Call context cannot be accessed after call completed."
I figured out that perhaps .readAll() function doesn't account for multi line strings and that's why popped a Segmentation error, because it did work when I tried to read a file with no line breaks. -
- Compile for debug.
- Execute by using the Debug button to run application.
- Make it hit the SEGV.
- At this point the debugger will "jump in" and break.
- Find the Stack trace window.
- You see a series of numbered "frames". Look back through the frames from where the SEGV happened till you see a line in your own code.
- Click on that frame line. Look at the statement and your local variables to see what's wrong. In all likelihood, a
nullptr
.
If you really get stuck, post a readable screenshot. Unless you are looking in the right window pane you won't get anywhere.
-
@JonB Thanks for your insight. I did as you suggested and found one of my local variable's address a nullptr(though not sure where it came from). I changed a few lines of code and apparently, my code works fine now. @Christian-Ehrlicher @J-Hilk thanks!