Thread: reading a DNA strain from a file

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    40

    reading a DNA strain from a file

    i have a problem to read a DNA strain ( a sequence of the letters A G T C) im confused about my first loop its supposed to read dada from a file and end when the file is done can someone please give me some criticism of my program.
    Code:
    #include<stdio.h>
    
    int main (void)
    {
    	int sampleID;
    	int i, j;
    	double num_of_genes;
    	int length_exon;
    	int intron;
    	int countC = 0;
    	int countA = 0;
    	int countT = 0;
    	int countG = 0;
    	char x, y;
    	double meanA;
    	double meanC;
    	double meanT;
    	double meanG;
    	int type;
    	int counter;
    	
    	printf("\nID    mA     mT      mG     mC    Type");
    	while (counter < 30)
    	{
    		scanf("%d", &sampleID);
    		scanf("%d", &num_of_genes);
    		scanf("%d", &length_exon);
    	
    		intron = 3 * length_exon + 1;
    		for (i = 0; i < num_of_genes; i++)
    		{
    			while(scanf("%c", &x) <= length_exon)
    			{
    				if (x == 'A')
    					countA++;
    				else if (x == 'C')
    					countC++;
    				else if (x == 'T')
    					countT++;
    				else if (x == 'G');
    					countG++;
    				for (j = 0; j <= intron; j++)
    					scanf("%c", &y);
    			}
    			
    		}
    		meanA = countA / num_of_genes;
    		meanC = countC / num_of_genes;
    		meanT = countT / num_of_genes;
    		meanG = countG / num_of_genes;
    		if (meanC <= meanA && meanA <= meanT && meanT <= meanG)
    			type = 1;
    		else if (meanA <= meanC && meanC <= meanT && meanG > 0)
    			type = 2;
    		else
    			type = 3;
    			
    		printf("\n%d    %lf     %lf      %lf     %lf    %d", sampleID, meanA, meanT, meanG, meanC, type);
    		counter++;
    		
    	}
    	return 0;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You say your loop is supposed to read file from a data until the file has been completely read in. But
    1. Your loop is controlled by a simple counter that goes to 30
    2. You don't read from a file anywhere inside the loop
    3. I don't know what you think scanf returns, but the bet that scanf will return something other than 0 or 1 with a format of "%c" isn't good betting
    4. You have a for loop inside the loop that throws away some large number of characters for every one that you actually use

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    40
    how can i make my first loop read from a file until i have reached the end of the file

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Nobody could ever have asked questions about files or end of files before.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by youngvito View Post
    how can i make my first loop read from a file until i have reached the end of the file
    In this program you have yet to reach the *start* of the file. Google "C file I/O", or read the section of your C library documentation that deals with same.

    Here's a quick clue:
    Code:
    FILE *ptr=fopen("myfile.txt", "r");
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Registered User
    Join Date
    Apr 2009
    Posts
    40
    i read through the text but i still havent firgured out how to implement that in my program an you please give me some more advice.

  7. #7
    Registered User
    Join Date
    Apr 2009
    Posts
    40
    i am using I/O redirection through my compiler i dont need to open the

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Your compiler doesn't have I/O redirection. Perhaps you mean through the shell?

    In any rate, given that the text I linked explicitly states how to use it in your program, I don't know how much more help I can be.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  2. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  3. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM