#zsh trace trap - when reading from a file
46 messages · Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.
#include "FileLineReader.h"
#include <fstream>
#include <iostream>
std::string FileLineReader::readLineFromFile(const std::string& fileName){
std::ifstream fileInputStream(fileName);
std::string line;
while(getline(fileInputStream, line)){
std::cout << line << std::endl;
}
}
#include "FileLineReader.h"
#include <iostream>
int main(){
FileLineReader flr;
std::string path = "sample.txt";
flr.readLineFromFile(path);
}
Running this gives zsh: trace trap
Never seen this error before and when I search online I can't find anything that is relevant to reading from a file
Shouldn't this be while( fileInputStream.getline(line) )?
I tried getline(fileInputStream, line) I still get the same error
the first line of readLineFromFile doesn't give any error
the getline(fileInputStream, line) is where the zsh: trace trap error occurs
std::ifstream fileInputStream(fileName);
if( fileInputStream.is_open() ) {
ok
} else { error }
std::string FileLineReader::readLineFromFile(const std::string& fileName){
std::ifstream fileInputStream(fileName);
std::string line;
if (!fileInputStream.is_open()){
while(getline(fileInputStream, line)){
std::cout << line << std::endl;
}
} else {
throw std::runtime_error("Could not open file");
}
}
this also results in zsh: trace trap
Do you need to explicitely set the open mode?
std::ifstream fileInputStream(filename, std::ios::in)
And if (!fileInputStream.is_open()){ should remove the !, is_open is positive on success
trace trap means unhandled exception
this doesn't work either :((
the same code runs fine on an online compiler
crazy
I wonder why the compiler doesn't complain because of missing return statement
it gives a warning
Can you give the complete code, I try here on macOS
Im running it on macOS already
M1 Pro
can u check with the code I already sent?
Works fine here when the file exists
#include <iostream>
#include <fstream>
class FileLineReader {
public:
std::string readLineFromFile(const std::string& fileName){
std::ifstream fileInputStream(fileName);
std::string line;
if (fileInputStream.is_open()){
while(getline(fileInputStream, line)){
std::cout << line << std::endl;
}
} else {
throw std::runtime_error("Could not open file");
}
return "";
}
};
int main(){
FileLineReader flr;
std::string path = "sample.txt";
flr.readLineFromFile(path);
}
what compiler are u using?
File doesn't exist: ```
libc++abi: terminating with uncaught exception of type std::runtime_error: Could not open file
Terminated due to signal: ABORT TRAP (6)
macos clang
I just let it run inside CodeRunner.app (https://coderunnerapp.com/)
Ok I tried running this in a new project and it works
my existing project I get the zsh trace trap error
hmm
omg
I got it
sample.txt was never pushed to the branch
so it actually didn't exist
I just saved a copy of sample.txt on my local device and it works now
🤦♂️
thank you so much for helping!
!solved