reading data from a file till its the end of the file using I/O redirection

This is a discussion on reading data from a file till its the end of the file using I/O redirection within the C Programming forums, part of the General Programming Boards category; Im have trouble reading a file till its the end of a file using I/O redirection with my Quincy 2005 ...

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

    reading data from a file till its the end of the file using I/O redirection

    Im have trouble reading a file till its the end of a file using I/O redirection with my Quincy 2005 compiler. My first while loop is supposed to do this and i dont know how to start it. Can someone please help me.

    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
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Asking the same question in a new thread with a different title is unlikely to make the answer any different, but it at least demonstrates you are interested enough to keep trying.

    As I said last time, you do not open a file in this code, hence the idea of "reading a file till its the end" is meaningless. There is nothing resembling "I/O redirection" in this code either.

    Since you ignored that observation the last time (and several hints as to what you *should* have read up on), let's approach this problem differently:

    1) where, and by what means, do you consider yourself to be opening a file for reading in this code? All you need to do is post those one or two specific lines and say "This is how I opened the file." Then we can determine why you are having difficulty reaching the end.

    2) what do you think the term "I/O redirection" means, and how are you intending to use it to accomplish your goals? Just a few sentences of explanation will do. We need to understand how this program works in order to find the bug.
    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

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    40
    its an assignment for school, the file saved as a textfile, its called data0.txt and your supposed to scan the file till the end of it using Input/Output redirection, it reads the file through the compiler, you dont have to open a file in the program.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,981
    Okay, temporarily put aside what you have done. Write a program that reads from stdin and prints out what is read until EOF is reached.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Apr 2009
    Posts
    40
    thats what im trying to figure out though, i dont know how to do that

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,981
    hmm... okay, maybe I'll change this to venture back to what you have done. Write a program that reads ints from stdin using scanf() and prints out what is read until EOF is reached.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Apr 2009
    Posts
    40
    thats exaclty what im trying to figure out, i dont know how to read data from a file until EOF is reached.

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by youngvito View Post
    thats what im trying to figure out though, i dont know how to do that
    Alright, I understand now (sorry!); you want to use this like "./a.out <data0.txt".

    As laserlight says, write a simple program that will just read in each line and print it out:
    Code:
    char buffer[1024];
    scanf("%s", buffer);
    printf("LINE: %s",buffer);
    When you get that to work, you will either know how to stop at EOF or else be able to ask a more specific question.
    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

  9. #9
    Registered User
    Join Date
    Apr 2009
    Posts
    40
    oh ok , i think you made things a little clearer for me, thanks alot

  10. #10
    Registered User
    Join Date
    Apr 2009
    Posts
    40
    but i dont know how many characters there are in total is there a way to do this without a buffer cause we didnt learn that yet

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,981
    Quote Originally Posted by youngvito
    thats exaclty what im trying to figure out, i dont know how to read data from a file until EOF is reached.
    Okay, maybe an example is in order:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int num;
        while (scanf("%d", &num) == 1)
        {
            printf("%d\n", num);
        }
        return 0;
    }
    In this case, the idea is that as long as scanf() reads the input as expected, the loop keeps looping. When EOF is reached, scanf returns EOF, which is not equal to 1, so the loop terminates. Likewise, if the input fails to be match the format, scanf returns 0, which again terminates the loop.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Apr 2009
    Posts
    40
    in this loop does the loop keep looping until the end of file is reached or does it go on to the other loops after the first 3 numbers have been scaned using scanf.
    [code[

    while (scanf("%d%d%d", &x1, &x2, x3) != EOF)
    {
    while(scanf(%c", &a) != EOF)

  13. #13
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by youngvito View Post
    in this loop does the loop keep looping until the end of file is reached or does it go on to the other loops after the first 3 numbers have been scaned using scanf.
    The best and easiest way to determine that is to put some printf() statements in for "debugging", so that you can observe what is happening, eg:
    Code:
    printf("* %d  -- %lf -- %d *\n",sampleID,num_of_genes,length_exon);
    Notice I used %lf (long float) for the double (num_of_genes), which you should also use in your scanf (if you want a double there at all, maybe you want a long int instead? I'm just guessing the num_of_genes is always an integer and not "3.56" or something.)

    If you put a printf() in some appropriate place inside each loop in your nest, you can observe the flow of execution.
    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

  14. #14
    Registered User
    Join Date
    Apr 2009
    Posts
    40
    i still havent figured out how to initiate my first loop so that it reads the data till the end of the file, can you give any suggestions

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading data from a text file
    By Dark_Phoenix in forum C++ Programming
    Replies: 8
    Last Post: 06-30-2008, 02:30 PM
  2. reading data into a queue from file
    By bcianfrocca in forum C++ Programming
    Replies: 12
    Last Post: 11-17-2005, 10:28 AM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 02:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21