cmmap

How to get one character at a time


What the outcome should look like:

G -- C
C -- G
T -- A
T -- A
C -- G
T -- A
T -- A
A -- T
C -- G
C -- G
C -- G
G -- C
T -- A

What it actually looks like:

GCTTCTTACCCGTGCCCCAC -- C
CTTCTTACCCGTGCCCCAC -- G
TTCTTACCCGTGCCCCAC -- A
TCTTACCCGTGCCCCAC -- A
CTTACCCGTGCCCCAC -- G
TTACCCGTGCCCCAC -- A
TACCCGTGCCCCAC -- A
ACCCGTGCCCCAC -- T
CCCGTGCCCCAC -- G
CCGTGCCCCAC -- G
CGTGCCCCAC -- G
GTGCCCCAC -- C
TGCCCCAC -- A
GCCCCAC -- C
CCCCAC -- G
CCCAC -- G
CCAC -- G
CAC -- G
AC -- T
C -- G
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <sys/mman.h>

int main(int argc, char **argv){

    int buff = 100;

    char *ptr = mmap (NULL,buff, PROT_WRITE | PROT_READ ,MAP_PRIVATE| MAP_ANONYMOUS,0,0);
        if(ptr == MAP_FAILED){
            printf("Mapping Failed\n");
            return 1;
        }
    char *oof = mmap (NULL,buff, PROT_WRITE | PROT_READ ,MAP_PRIVATE| MAP_ANONYMOUS,0,0);
        if(oof == MAP_FAILED){
            printf("Mapping Failed\n");
            return 1;
        }
    
    int f1 = open("file.txt", O_RDWR);
    int length = read(f1,ptr,sizeof(char)*buff);
    
    for(int i = 0; i<length;i++){
        if(ptr[i] == 'A'){
            oof[i] = 'T';
        }
        if(ptr[i] == 'T'){
            oof[i] = 'A';
        }
        if(ptr[i] == 'C'){
            oof[i] = 'G';
        }
        if(ptr[i] == 'G'){
            oof[i] = 'C';
        }
        
        printf("%s -- %s\n", &ptr[i],&oof[i]);
    }

}

I messed around a bunch and broke it a couple of times. I feel like im just missing something and its not coming to my head.

If anyone could give me any ideas that would b great.


Solution

  • In your for() loop, just change the printf() statement to print chars, not strings, like so:

    printf("%c -- %c\n", ptr[i],oof[i]);
    

    %c -- is for chars

    %s -- is for strings

    Also, please note that the address operator (&) has been removed. We just want the index of each array for printing chars -- not the address of the array (or the address of the array index) -- which would be useful for printing strings.