File I/O help

0 Katie Fields · December 9, 2014
Hi, I am trying to read in a file and save all of the even lines to one array and all of the odd lines to another array. I have tried a bunch things but nothing is working. Here are some of my attempts:

1)
int main(){
  char header[5];
  char seq[5];
  int i=0;

  FILE *dna_file;
  dna_file = fopen("input.fasta", "r");

  for(i=0; i<5; i++){
    if(i%2 == 0){
      fscanf(dna_file,"%s", &header[i/2]);
    }
    else{
      fscanf(dna_file,"%s", &seq[i/2]);
    }
  }

2)
int main(){
  int i = 0;

  int BUFSIZE = 1000;
  char* seq[20];
  FILE *dna_file = fopen("inabc.fasta", "r");
  if (dna_file == 0){
    printf("Error");
    exit(1);
  }

  seq = malloc(BUFSIZE);
  while (fgets(seq, BUFSIZE, dna_file)){
    i++;
    seq = malloc(BUFSIZE);
  }

Any help would be greatly appreciated! 

Post a Reply

Replies

Oldest  Newest  Rating
0 c student · December 10, 2014
have you implemented some kind of pseudo code so that you know exactly what to do, how to do it and in what order?  instead of diving right into coding, it might be best to plan ahead or you'll become lost easily.
  • 1

C

107,230 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator