This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Failed when reading bam file

Hi all,

I need to read bam file in my project. I met a problem. According to sam file format, the first information of bam file is: char[4] which is BAM\1. But my output is ?. Can you tell my what is wrong of my codes? Thank you!

My codes (C++ or C) is here:

ifstream is(argv[1],ifstream::binary);
char magic[4];
is.read((char *)magic,sizeof(char)*4);
cout<<magic<<endl;

or

FILE *stream;
stream=fopen(argv[1],"r");
char magic[4];
fread(magic,sizeof(char),4,stream);
cout<<magic<<endl;
sequencing
gzFile fp = gzopen(argv[1], "r"); gzread(fp, magic, 4);

It works! Thank you so much!

1 answer

Search google "parsing a bam file c++"

Here is a random post I found. It will help you to understand how to parse a bam file.

Get Bases And Quality From A Bam File [C++]

Thank you! I know many people just includes codes from samtools or picard. But that needs includes many files. I just want to take position, sequence and error information from bamfile. I try to write very simple codes by myself.

I have read the bam format from http://samtools.github.io/hts-specs/SAMv1.pdf

I am confused why I can not read bam file correctly.

Log in to answer this question.