473,395 Members | 1,680 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Trouble with File Input/Output concerning loops

I'm writing a C program to read in data from a file. The file contains integer values. The program outputs the minimum and maximum integer values of each line of the file.

Here's the text file:
7
905 177 738 891 912 853 271
607 894 958 848 636 691 309
953 888 153 485 969 483 379
151 196 106 387 115 569 788

The first number denotes the number of columns or elements in each row.

The problem I'm having is that my program prints out the minimum and maximum value of each line (excluding the first line) just fine but then it seems to print out another set of values at the end that I don't need. For example here's my output:

min: 177 max: 912
min: 309 max: 958
min: 153 max: 969
min: 106 max: 788
min: 151 max: 788 <--------- UNNECESSARY OUTPUT

And here's the program:

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. void open_file ();
  3.  
  4. int main()
  5. {
  6.   open_file();
  7.   return 0;
  8. }
  9.  
  10. void open_file()
  11. {
  12.   int first_num, num, max, min, loop, update;
  13.   FILE *fp;
  14.   int columns;
  15.   fp = fopen("stats_data.txt", "r");
  16.   loop = fscanf(fp, "%d", &columns);
  17.  
  18. while (loop != EOF)
  19. {
  20.   update = (columns - 1);
  21.   fscanf(fp, "%d", &first_num);
  22.   max = first_num;
  23.   min = first_num;
  24.  while (update > 0)
  25.   {
  26.    loop = fscanf(fp, "%d", &num);
  27.  
  28.    if (num > max)
  29.     max = num;
  30.  
  31.    if (num < min)
  32.     min = num;
  33.  
  34.    update = update - 1;
  35.   }
  36.  
  37.   printf("min: %d ", min);
  38.   printf("max: %d ", max);
  39.   printf("\n");
  40.  
  41. }
  42.  
  43. }
Any help is greatly appreciated!
Feb 6 '13 #1
2 1548
Rabbit
12,516 Expert Mod 8TB
That is because after it reads the last number, it is not at EOF yet. And so it loops one more time.
Feb 6 '13 #2
divideby0
131 128KB
if you're using fscanf, you might try something like
Expand|Select|Wrap|Line Numbers
  1. int i = 0;
  2. ...
  3.  
  4. while(fscanf(fp, "%d", &num) == 1) {
  5.  
  6.     // if desired, print num in each line
  7.  
  8.     /* compare i to zero; if it is
  9.        assign min, max, to num
  10.     */
  11.  
  12.     // test min, max against num 
  13.  
  14.     // increment i
  15.  
  16.     /* check i against columns
  17.        if it's equal, reset i
  18.        print the low and high values
  19.     */ 
  20. }
  21.  
  22. if(!feof(fp))
  23.    // file read error
  24. ...
  25.  
Feb 8 '13 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Jens Jensen | last post by:
I use StreamReader and StreamWriter to manipulate a text file file . The problem is that my Norgian characters get altered. How can i preserve the character set to ASCI ? Thanks JJ
7
by: lostmountainman | last post by:
I am trying to design this code that will allow me to input a file, but it not be previously "hardwired" into the code, the user needs to input it, and I have almost figured it out, but i keep...
3
by: John Williams | last post by:
I'm writing a stagenography program to experiment with how it works. The algorithm I'm using appears to be producing the correct result...however I'm struggling with the file input. I never...
1
by: sushanttt | last post by:
how can we write the output of one c/c++ file into a text file
17
by: Hypnotik | last post by:
Hey all. I am working with a file input/output program. The program is take input from a file, manipulate the info, and then output the info to another file. The program takes the input just...
5
by: nkomli | last post by:
Okay, I want to allow the user to define their own variables and equations and then use them to run a program. The user picks the option to create a list of variables If the user enters in r d...
2
by: tudyfruity18 | last post by:
I have to write a program that will reformat the data files given to me and create a new data file for each city. The desired output would be the cities name followed by the year( fixed four digits),...
2
by: peterjaybee | last post by:
Hia, I have to write a program in C to calculate the determinant of a matrix. I have mannaged to write a program which achieves for elements input by a user, however the program must take matrix...
3
by: Hodge4ever | last post by:
Just to give you some background, I am trying to write an encryption algorithmn, so the options are -e to encrypt or -d to decrypt. During the encryption process an array of characters is written to...
3
by: Rainy | last post by:
Hello! I'm having some trouble with pyserial package, I'm sending commands and reading responses from a custom pcb, and sometimes I get a proper response, at other times I get nothing, and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.