#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    39
    Rep Power
    6

    Reading a list of integers from a file


    I need to write a program that reads a list of integers from a text file. The list of integers is of arbitrary size(up to 1000) and only one integer is contained on each line. The first integer gives the number of values.

    Then, I need to write the largest, smallest, and the average to a text file.

    Here is part of it where I test the reading in part. What am I doing wrong as far as reading in goes?
    Code:
    #define _CRT_SECURE_NO_WARNINGS
    #include <stdio.h>
    void main(){
    	
    	FILE *fp;
    	char sample_chr;
    	fp = fopen("numbers-inp.txt", "r");
    	int lines;
    	lines=0;
    	sample_chr = getc(fp);
    	while (sample_chr != EOF) 
    	{
    	
    	if (sample_chr == '\n')
    	{
    	
    	lines+=1;
    	}
    	
    	sample_chr = getc(fp);
    	}
    	 
    
    	
    
    	int num[1000],i;
    	 for(i=0; i<lines; i++){
    		 
    		 
    
    		 fscanf(fp,"%d",&num[i]);
    		 printf("%d",num);
    	}
    	 
    	 
    }
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    30
    Rep Power
    3
    Originally Posted by Astrodude
    I need to write a program that reads a list of integers from a text file. The list of integers is of arbitrary size(up to 1000) and only one integer is contained on each line. The first integer gives the number of values.

    Then, I need to write the largest, smallest, and the average to a text file.

    Here is part of it where I test the reading in part. What am I doing wrong as far as reading in goes?
    Code:
    #define _CRT_SECURE_NO_WARNINGS
    #include <stdio.h>
    void main(){
    	
    	FILE *fp;
    	char sample_chr;
    	fp = fopen("numbers-inp.txt", "r");
    	int lines;
    	lines=0;
    	sample_chr = getc(fp);
    	while (sample_chr != EOF) 
    	{
    	
    	if (sample_chr == '\n')
    	{
    	
    	lines+=1;
    	}
    	
    	sample_chr = getc(fp);
    	}
    	 
    
    	
    
    	int num[1000],i;
    	 for(i=0; i<lines; i++){
    		 
    		 
    
    		 fscanf(fp,"%d",&num[i]);
    		 printf("%d",num);
    	}
    	 
    	 
    }

    By the time fscanf happens fp will be pointing to EOF. So it cant read further so reset fp to 0th position.

    Code:
    #define _CRT_SECURE_NO_WARNINGS
    #include <stdio.h>
    void main(){
    
            FILE *fp;
            char sample_chr;
            fp = fopen("numbers-inp.txt", "r");
            int lines;
            lines=0;
            sample_chr = getc(fp);
            while (sample_chr != EOF)
            {
    
            if (sample_chr == '\n')
            {
    
            lines+=1;
            }
    
            sample_chr = getc(fp);
            }
    
             fseek(fp,0, SEEK_SET); // Code change
    
    
            int num[1000],i;
             for(i=0; i<lines; i++){
    
    
    
                     fscanf(fp,"%d",&num[i]);
                     printf("%d",num[i]);   // Code change
            }
    
    
    }
  4. #3
  5. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,413
    Rep Power
    1871
    > void main()
    Change this to int main, and put a return 0; at the end of the function.

    > fp = fopen("numbers-inp.txt", "r");
    > int lines;
    A couple of things.
    1. You don't check that the file was opened successfully.
    2. You can't mix declarations and statements in traditional (C89) C. Did you accept the default of visual studio to create a .cpp (C++) file?

    > while (sample_chr != EOF)
    Is it necessary to have this loop? Given the format description of your file, I don't think so, unless you specifically have to report the number of lines.

    If you DO have to count the lines, then you need to call
    rewind(fp);
    before scanning the file again.

    You can read the file just once by doing something like
    Code:
    while ( i < 1000 && fscanf(fp,"%d",&num[i]) == 1 ) {
        i++;
    }
    > printf("%d",num);
    This should be
    printf("%d",num[i]);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    39
    Rep Power
    6
    So I've changed the code to print out the smallest integer value in a list of integers from a text file.

    The value isn't printing still though.
    Code:
    int main(){
    	
    	
    	FILE *fp;
    	
    	fp = fopen("numbers-inp.txt", "r");
    	int i,small,large;
    	int num[1001];
    	i=0;
    	if(fp!=NULL){
    	while ( i < 1000 && fscanf(fp,"%d",&num[i]) == 1 ) 
    	{
        i++;
    }
    	 	 
    	int j;		 
    	
    	small=num[1];
    	for(j=2;j=num[0];j++){
    		if(small>=num[j]){
    			small=num[j];
    		}
    
    		
    	}
    	
    
    	}
    	fclose(fp);
    	printf("%d",small);
    	return 0;
    	
    }
  8. #5
  9. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,413
    Rep Power
    1871
    > small=num[1];
    > for(j=2;j=num[0];j++)
    Two things.
    1. Arrays start at index 0, not 1
    2. j=num[0] should be something else - possibly involving the value of i (the number of elements read previously)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper

IMN logo majestic logo threadwatch logo seochat tools logo