C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 26th, 2012, 10:41 PM
Astrodude Astrodude is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 22 Astrodude User rank is Corporal (100 - 500 Reputation Level)Astrodude User rank is Corporal (100 - 500 Reputation Level)Astrodude User rank is Corporal (100 - 500 Reputation Level)Astrodude User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 3 h 57 m 34 sec
Reputation Power: 0
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);
	}
	 
	 
}

Reply With Quote
  #2  
Old November 26th, 2012, 11:42 PM
shilpac shilpac is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 30 shilpac User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 3 m 34 sec
Reputation Power: 1
Quote:
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
        }


}

Reply With Quote
  #3  
Old November 26th, 2012, 11:48 PM
salem's Avatar
salem salem is online now
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,839 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 2 Days 19 h 1 m 4 sec
Reputation Power: 1774
> 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

Reply With Quote
  #4  
Old November 27th, 2012, 02:26 PM
Astrodude Astrodude is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 22 Astrodude User rank is Corporal (100 - 500 Reputation Level)Astrodude User rank is Corporal (100 - 500 Reputation Level)Astrodude User rank is Corporal (100 - 500 Reputation Level)Astrodude User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 3 h 57 m 34 sec
Reputation Power: 0
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;
	
}

Reply With Quote
  #5  
Old November 28th, 2012, 12:21 AM
salem's Avatar
salem salem is online now
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,839 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 2 Days 19 h 1 m 4 sec
Reputation Power: 1774
> 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)

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Reading a list of integers from a file

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap