#zsh trace trap - when reading from a file

46 messages · Page 1 of 1 (latest)

midnight field
#

Running into weird error when trying to read from a file using ifstream

boreal larkBOT
#

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.

midnight field
#
#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

severe walrus
#

Shouldn't this be while( fileInputStream.getline(line) )?

midnight field
#

I tried getline(fileInputStream, line) I still get the same error

severe walrus
#

hmm

#

File exists, ifstream doesn't give an error?

midnight field
#

the first line of readLineFromFile doesn't give any error

#

the getline(fileInputStream, line) is where the zsh: trace trap error occurs

severe walrus
#
std::ifstream fileInputStream(fileName);
if( fileInputStream.is_open() ) {
  ok
} else { error }
midnight field
#
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

severe walrus
#

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

midnight field
#

the same code runs fine on an online compiler

severe walrus
#

crazy

#

I wonder why the compiler doesn't complain because of missing return statement

midnight field
#

it gives a warning

severe walrus
#

Can you give the complete code, I try here on macOS

midnight field
#

Im running it on macOS already

#

M1 Pro

#

can u check with the code I already sent?

severe walrus
#

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);
}
midnight field
#

what compiler are u using?

severe walrus
#

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

midnight field
#

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