The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Reading a list of integers from a file
Discuss Reading a list of integers from a file in the C Programming forum on Dev Shed. Reading a list of integers from a file C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

November 26th, 2012, 10:41 PM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 22
  
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);
}
}
|

November 26th, 2012, 11:42 PM
|
|
Contributing User
|
|
Join Date: Oct 2012
Posts: 30
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
}
}
|

November 26th, 2012, 11:48 PM
|
 |
Contributed User
|
|
|
|
> 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]);
|

November 27th, 2012, 02:26 PM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 22
  
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;
}
|

November 28th, 2012, 12:21 AM
|
 |
Contributed User
|
|
|
|
|
> 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)
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|