Trasfering FILE* into array struct

  • (2 Pages)
  • +
  • 1
  • 2

25 Replies - 1406 Views - Last Post: 29 April 2014 - 10:37 PM Rate Topic: -----

#1 airjavi07  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 20
  • Joined: 13-April 14

Trasfering FILE* into array struct

Posted 27 April 2014 - 04:33 PM

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct subsequence {
    char *sequence; //the larger sequence being searched
    int sizesequence; //size of the array sequence
    char *subseq; //the subsequence we are looking for
    int size_subseq; //size of the char array subseq
    int *locations;// a pointer to a list of locations where the string in subseq is found in sequence
    int numlocations;//size of the int array locations
    char *newseq;
} Sequence;//named struct Sequence to avoid using struct subsequence as a declaration.

initseq(Sequence *DNAcid){//function to create space for the sequence, read in the  sequence from stdio, and
//return a pointer to the sequence
    char sname[35];//this pointer will be returned to store in our Sequence struct for char sequence.
    int i = 0;
    int ssize = 10000;

    DNAcid->sequence = (char*)malloc(sizeof(ssize));
    FILE* inSeq;

    printf("Enter the file of your sequence is in: ");
    gets(sname);

    inSeq = fopen(sname,"r");

    if( inSeq == NULL){
    printf("Could not open file. Try again.");
    return 1;
    }

    while((DNAcid->sequence[i] = fgetc(inSeq))!= EOF)
    {   if(i >= ssize){
    DNAcid->newseq = (char*)malloc(sizeof(ssize)*2);
    for(i = 0; i <= ssize; i++){
            DNAcid->newseq[i] = DNAcid->sequence[i];}
    free(DNAcid->sequence);
    ssize = ssize * 2;
    printf("%d",ssize);
    DNAcid->sequence = (char*)malloc(sizeof(ssize));
    for(i = 0; i <= ssize; i++){
            DNAcid->sequence[i] = DNAcid->newseq[i];}
    }
    if(DNAcid->sequence != '\n'){
        DNAcid->sequence[i];
   printf("%c",DNAcid->sequence[i]);
           i++;

        }
    }
}

int main(){
Sequence DNAcid;
initseq(&DNAcid);//pointer string is returned to the struct char sequence to be

return 0;
}



Hello. My code is very very sloppy. I apologize for this. I just posted the necessary section which I've been working on for hours.
Anyways, I pretty much am having trouble getting my file into my struct array sequence. We are suppose to iterate to store it in a big enough array big enough for the # char in the file(163000 characters about). I've played around with assigning a bigger ssize to malloc and it does read and iterate my file but I don't think it stores currectly into my struct array because it keeps crasing. Can some please help me?!? Attached is my file that you must enter. It is 'seq.txt' if anyone wants to run it.

Is This A Good Question/Topic? 0
  • +

Replies To: Trasfering FILE* into array struct

#2 tarmizi_adam2005  Icon User is offline

  • جوروترا

Reputation: 287
  • View blog
  • Posts: 984
  • Joined: 18-April 09

Re: Trasfering FILE* into array struct

Posted 27 April 2014 - 05:50 PM

Hi,

I think you are not allocating enough memory for the 163000 characters. This:

int ssize = 10000;

DNAcid->sequence = (char*)malloc(sizeof(ssize));


I think you are only allocating 4 bytes of memory. If you wanted to allocate memory for 163000 char, shouldn't you have something like this ?:

int ssize = 163000;

malloc(ssize*sizeof(char)); // 163000 times the size of a character 

Was This Post Helpful? 1
  • +
  • -

#3 airjavi07  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 20
  • Joined: 13-April 14

Re: Trasfering FILE* into array struct

Posted 27 April 2014 - 06:36 PM

Sorry forgot to mention that I don't know the size of the array based on the assignment so we have to allocate a random size until reaches file size. 163000 wouldn't work because the user can instead open a different file that's 260000 chars.
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5828
  • View blog
  • Posts: 19,868
  • Joined: 05-May 12

Re: Trasfering FILE* into array struct

Posted 27 April 2014 - 07:39 PM

There's two approaches:
First approach is to keep calling realloc() when it's time to grow your data buffer.
The second approach is determine the file size and then allocate adequate space based on the file size.
Was This Post Helpful? 0
  • +
  • -

#5 jimblumberg  Icon User is offline

  • member icon

Reputation: 5304
  • View blog
  • Posts: 16,521
  • Joined: 25-December 09

Re: Trasfering FILE* into array struct

Posted 27 April 2014 - 07:55 PM

Oh and you should never ever use gets(), it's a buffer overrun waiting to happen, think about using fgets() instead. Also in a C program you really shouldn't cast the return value from malloc(), this cast can hide a serious issue.

Jim
Was This Post Helpful? 0
  • +
  • -

#6 airjavi07  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 20
  • Joined: 13-April 14

Re: Trasfering FILE* into array struct

Posted 28 April 2014 - 06:00 PM

#include <stdio.h>
#include <stdlib.h>
#include <string.h>



typedef struct subsequence
{
    char *sequence; //the larger sequence being searched
    int sizesequence; //size of the array sequence
    char **subseq; //the subsequence we are looking for
    int size_subseq; //size of the char array subseq
    int *locations;// a pointer to a list of locations where the string in subseq is found in sequence
    int numlocations;//size of the int array locations
    char *newseq;
    char **newsubseq;
}
Sequence;//named struct Sequence to avoid using struct subsequence as a declaration.

initseq(Sequence *DNAcid){//function to create space for the sequence, read in the  sequence from stdio, and
//return a pointer to the sequence
    char sname[35];//this pointer will be returned to store in our Sequence struct for char sequence.
    int i = 0;
    int j = 0;
    int ssize = 10000;

    DNAcid->sequence = (char*)malloc(sizeof(char*)*10000);
    FILE* inSeq;

    printf("Please enter the name of the file: ");
    gets(sname);

    inSeq = fopen(sname,"r");

    if( inSeq == NULL){
    printf("Could not open file. Try again.");
    return 1;
    }

    while((DNAcid->sequence[i] = fgetc(inSeq))!= EOF)
    {   if(i > ssize){
    DNAcid->newseq = (char*)malloc(sizeof(ssize)*2);

    for(j = 0; j <= ssize; j++){
            DNAcid->newseq[j] = DNAcid->sequence[j];}

    free(DNAcid->sequence);
    ssize *= 2;
    DNAcid->sequence = DNAcid->newseq;
    }
    if(DNAcid->sequence[i] == '\n'){
   i--;
   i++;
    }
       printf("%c",DNAcid->sequence[i]);

    }
}

initsubseq(Sequence *DNAcid){//function to read in the subsequence and store in subseq. DNAcid declared as pointer to access struct
    char subname[35];//this pointer will be returned to store in our Sequence struct for char sequence.
    int i = 0;
    int j = 0;
    int n = 0;
    int SIZE=25;
    int SUBSIZE = 19;
    DNAcid->subseq = (char**)malloc(sizeof(char*)*SIZE);
    DNAcid->subseq[i] = (char*)malloc(sizeof(char*)*SUBSIZE);

    FILE* inSeqsub;

    printf("Enter the file name of your subsequence: ");
    gets(subname);

    inSeqsub = fopen(subname,"r");

    //DNAcid.subseq = (char**)malloc(sizeof(char*)*1000);//malloc enough space for a input of up to 100 char plus NULL
    if( inSeqsub == NULL){
    printf("Could not open file. Try again.");
    return -1;
    }

   while((DNAcid->subseq[n][i] = fgetc(inSeqsub))!= EOF)
    {   if(i > SIZE)
        {
        DNAcid->newsubseq = (char**)malloc(sizeof(SIZE)*2);
        DNAcid->newsubseq[j] = (char*)malloc(sizeof(SUBSIZE)*2);

    for(j = 0; j <= SUBSIZE; j++){ //while loop
    DNAcid->newsubseq[j] = DNAcid->subseq[j];}
    free(DNAcid->subseq);
    SIZE *= 2;
    SUBSIZE*=2;
    DNAcid->subseq = DNAcid->newsubseq;
    }
    if(DNAcid->subseq[n][i] == '\0'){
    n++;
    i = 0;}
    i++;
       printf("%c",DNAcid->subseq[n][i]);
    }

}

search(Sequence *DNAcid){//searches large sequence
    int i = 0;
    char* temp = strstr(DNAcid->sequence,DNAcid->subseq);//strstr searches strings and stores into temp

    //DNAcid->locations = (int*)malloc(sizeof(int)*25);//mallocs location for use of array
    DNAcid->sizesequence = strlen(DNAcid->sequence);//stores int value of how many positions are in sequence

    while(temp != NULL){//the last pos is NULL for string ends while
        DNAcid->size_subseq = strlen(temp);//stores int value of how many positions are in temp
        DNAcid->locations[i] = DNAcid->sizesequence - DNAcid->size_subseq;//starts @ 0 storing position numbers found in sizesequence
            temp++;//moves temp another space to check the next position
            i++;//moves array to next value to store in next element
        temp = strstr(temp,DNAcid->subseq);//strstr searches strings and stores into locations for temp this time to be looped again
    }
    DNAcid->numlocations = i;//stores the size of elements found in i
}

printresults(Sequence *DNAcid){//prints results
    int i, j;
    printf("The subsequence %s is located at starting positions",DNAcid->subseq);

    for(i=0;i < DNAcid->numlocations;i++){//iterates through array to print results on compiler
        printf(", %d",DNAcid->locations[i]);
    }
    printf(" \nfrom the original sequence.\n\n");
}

int main(){
Sequence DNAcid;
//Sequence DNAcid[5];//struct definition declares DNAcid as new type
//initseq(&DNAcid);//pointer string is returned to the struct char sequence to be
initsubseq(&DNAcid);//passing an address of the Sequence structure(pass by reference)

//search(&DNAcid);//searches for requested subsequence
//printresults(&DNAcid);//prints locations of where subsequence is found on larger strand
return 0;
}



I desperately need help. I can't seem to get this to work. So I stored my large text correctly and now I need to allocate my smaller text( subseq) but this is VERY difficult for me since now they made as mess with struct 2d arrays. i don't understand what I am doing wrong. Again my assignment is to increase the size of my array as the characters get larger only this time I have to do it to all of the 2d arrays. Can someone please help me. I know this looks unclear but I'll answer any question if you need more information.
Attached is my sub.txt. My large file is working correctly but I noted off to be more clear it runs perfect and might be similar issue. Issue can be found in init subseq function
Was This Post Helpful? 0
  • +
  • -

#7 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5828
  • View blog
  • Posts: 19,868
  • Joined: 05-May 12

Re: Trasfering FILE* into array struct

Posted 28 April 2014 - 06:57 PM

Do no open duplicate topics. You already have one about this open. Merging...
Was This Post Helpful? 0
  • +
  • -

#8 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1850
  • View blog
  • Posts: 6,646
  • Joined: 19-February 09

Re: Trasfering FILE* into array struct

Posted 28 April 2014 - 07:37 PM

This is not correct for sequence, sequence is a pointer (address) to a character. The character could be a single character or the start of an array of characters.

An array of char pointers is not wanted, as created here :-

    DNAcid->sequence = (char*) malloc(sizeof(char*)*10000);




The C Book - Arrays and Pointers
Was This Post Helpful? 0
  • +
  • -

#9 airjavi07  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 20
  • Joined: 13-April 14

Re: Trasfering FILE* into array struct

Posted 28 April 2014 - 08:23 PM

Yeah but my DNAcid is a pointer. My question is not on sequence. It works I've already checked it. I need help on my subseq. Like I stated in my previous statement. Please don't comment on my seq. if there's an issue I'll fix it later on but right now I need this sub so I can search my seq later on and see if it actually runs correctly.
Was This Post Helpful? 0
  • +
  • -

#10 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5828
  • View blog
  • Posts: 19,868
  • Joined: 05-May 12

Re: Trasfering FILE* into array struct

Posted 28 April 2014 - 09:37 PM

View Postairjavi07, on 28 April 2014 - 09:00 PM, said:

initseq(Sequence *DNAcid){//function to create space for the sequence, read in the  sequence from stdio, and
//return a pointer to the sequence
    char sname[35];//this pointer will be returned to store in our Sequence struct for char sequence.
    int i = 0;
    int j = 0;
    int ssize = 10000;

    DNAcid->sequence = (char*)malloc(sizeof(char*)*10000);
    FILE* inSeq;

    printf("Please enter the name of the file: ");
    gets(sname);

    inSeq = fopen(sname,"r");

    if( inSeq == NULL){
    printf("Could not open file. Try again.");
    return 1;
    }

    while((DNAcid->sequence[i] = fgetc(inSeq))!= EOF)
    {   if(i > ssize){
    DNAcid->newseq = (char*)malloc(sizeof(ssize)*2);

    for(j = 0; j <= ssize; j++){
            DNAcid->newseq[j] = DNAcid->sequence[j];}

    free(DNAcid->sequence);
    ssize *= 2;
    DNAcid->sequence = DNAcid->newseq;
    }
    if(DNAcid->sequence[i] == '\n'){
   i--;
   i++;
    }
       printf("%c",DNAcid->sequence[i]);

    }
}



So I stored my large text correctly


We beg to differ. No you didn't store it correctly. The minute your input exceeds your initial 10000 characters, you will allocate the wrong sized buffer. This is what member='#define'] was trying to point out.

If you are not convinced, try adding this line right after your [il]int ssize = 10000;
:
printf("The sizeof(ssize) is %d\n", sizeof(ssize));



Since you are not allocating sufficient space, your are invoking undefined behavior once you start overrunning your buffer. The computer is free to do whatever it wishes once you invoke undefined behavior, including making it look like your large text is working correctly and making your sub sequence code act flakey.
Was This Post Helpful? 0
  • +
  • -

#11 airjavi07  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 20
  • Joined: 13-April 14

Re: Trasfering FILE* into array struct

Posted 29 April 2014 - 05:14 AM

Yeah but that why I made my array grow in my while loop for every +10000 characters. I have newseq grow and then sequence points to it. Are you saying my while loop is wrong or my initial allocation?
Was This Post Helpful? 0
  • +
  • -

#12 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5828
  • View blog
  • Posts: 19,868
  • Joined: 05-May 12

Re: Trasfering FILE* into array struct

Posted 29 April 2014 - 05:35 AM

The while loop is wrong.
Was This Post Helpful? 0
  • +
  • -

#13 airjavi07  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 20
  • Joined: 13-April 14

Re: Trasfering FILE* into array struct

Posted 29 April 2014 - 05:39 AM

But the other poster said my initial malloc is wrong. So is he wrong? I just wanna know where my error is now and what I have to do to fix it.
Was This Post Helpful? 0
  • +
  • -

#14 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5828
  • View blog
  • Posts: 19,868
  • Joined: 05-May 12

Re: Trasfering FILE* into array struct

Posted 29 April 2014 - 06:28 AM

The issue there is that you are computing the size of a character pointer instead of a character and multiplying by 100000. It's another logical error, but not an error that would cause a buffer overrun.

Consider the following code:
printf("sizeof() of a char: %d", sizeof(char));
printf("sizeof() of a char pointer: %d", sizeof(char *));



You are allocating 2, 4 or 8 times more space than you needed depending on whether you your compiler is using 16-bit, 32-bit, or 64-bit pointers.
Was This Post Helpful? 0
  • +
  • -

#15 airjavi07  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 20
  • Joined: 13-April 14

Re: Trasfering FILE* into array struct

Posted 29 April 2014 - 08:21 AM

okay I think I understand what everyone is saying. It is pretty messed up but I think I fixed it right and now I am working on my while. I got it to system("PAUSE") printf the %i and it correctly grows from:
10k,20k,40k,80k, to 160k. The file I am working on now is 163000, chars so I think now I need to figure out why it crashes here. I think the reason why is that I end up allocating 320,000 chars and my while loop tries to store it in my array. But I guess I need it to reach the NULL and allocate just enough to store. Any ideas if I'm in the right direction?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>



typedef struct subsequence
{
    char *sequence; //the larger sequence being searched
    int sizesequence; //size of the array sequence
    char **subseq; //the subsequence we are looking for
    int size_subseq; //size of the char array subseq
    int *locations;// a pointer to a list of locations where the string in subseq is found in sequence
    int numlocations;//size of the int array locations
    char *newseq;
    char **newsubseq;
}
Sequence;//named struct Sequence to avoid using struct subsequence as a declaration.

initseq(Sequence *DNAcid){//function to create space for the sequence, read in the  sequence from stdio, and
//return a pointer to the sequence
    char sname[35];//this pointer will be returned to store in our Sequence struct for char sequence.
    int i = 0;
    int j = 0;
    int ssize = 10000;

    DNAcid->sequence = (char*)malloc(sizeof(char)*10000);
    printf("size of(): %d",sizeof(char));
    FILE* inSeq;

    printf("Please enter the name of the file: ");
    gets(sname);

    inSeq = fopen(sname,"r");

    if( inSeq == NULL){
    printf("Could not open file. Try again.");
    return 1;
    }

    while((DNAcid->sequence[i] = fgetc(inSeq))!= EOF)
    {   if(i > ssize)
    {   printf("%d",i);
        system("PAUSE");

        DNAcid->newseq = (char*)malloc(sizeof(char)*ssize*2);

        for(j = 0; j < ssize; j++)
            {
            DNAcid->newseq[j] = DNAcid->sequence[j];}

        free(DNAcid->sequence);
        ssize *= 2;
        DNAcid->sequence = DNAcid->newseq;
        free(DNAcid->newseq);
    }
    if(DNAcid->sequence[i] == '\n'){
   i--;
   i++;
    }
        printf("%c",DNAcid->sequence[i]);
        i++;

    }
}
int main(){
Sequence DNAcid;
//Sequence DNAcid[5];//struct definition declares DNAcid as new type
initseq(&DNAcid);//pointer string is returned to the struct char sequence to be
return 0;
}



Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2