473,387 Members | 1,779 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,387 software developers and data experts.

Can someone help me please, I have to other documents as inputs and outputs

I have written a program that will take the max and min of 10 set values. I have to now change my program to allow for these inputs to be entered in by another document, and print the values into another document.

Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. int
  3. main ()
  4. {
  5.   int i;
  6.   int a[10] = { 10, -99, 888, 4, 234, 1, 30, 40, 22, 34 };
  7.   int max = a[0];
  8.   int min = a[0];
  9.  
  10.  
  11.   for (i = 0; i < 10; i++)
  12.     {
  13.       if (a[i] > max)
  14.         {
  15.           max = a[i];
  16.         }
  17.       else if (a[i] < min)
  18.         {
  19.           min = a[i];
  20.         }
  21.     }
  22.   printf ("Maximum  : %d\n", max);
  23.   printf ("Minimum  : %d\n", min);
  24.  
  25.   return 0;
  26. }
  27.  
  28. /* This is the section I have been trying to add but I dont no where, can someone help me please. */
  29.  
  30.  FILE* fp;
  31.   fp = fopen("mydata.txt", "r");
  32.  
  33.   FILE* fp;
  34.   fp = fopen("output.dat", "w");
Feb 28 '07 #1
3 1361
Ganon11
3,652 Expert 2GB
First, you'll need to name the file pointers something different from each other, rather than fp.

You should put these statements in the beginning of your program. Then, when reading into your array, instead of assigning values to it using the { 1, 2, 3 } notation, read them into the array using a for... loop.

Finally, instead of using printf() to output to the console, use your output file pointer to output to the file - I think it's fprintf()?
Feb 28 '07 #2
I have made a few changes but I dont really understand it. Can anyone tell me were am going wrong this time or point me in the right direction. I have created both these documents but they are coming up as dat files, once I open the input file I cannot type values in, how is this.

Thanks for your time

Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. int main ()
  3. {
  4.    FILE  *fi;
  5.    FILE  *fo;
  6.  
  7.    fi = fopen("infile.dat","r");
  8.    fo = fopen("outfile.dat","w");
  9.    {
  10.       FILE *fi; /* input file pointer */
  11.       if ( (fi=fopen("infile.dat","r")) == NULL ) {
  12.          printf("ERROR opening file!\n");
  13.          return 1;  /* terminates program in error */
  14.       }
  15.       int i;
  16.       int a[10] = { fi };
  17.       int max = a[0];
  18.       int min = a[0];
  19.       for (i = 0; i < 10; i++)
  20.       {
  21.          if (a[i] > max)
  22.          {
  23.             max = a[i];
  24.          }
  25.          else if (a[i] < min)
  26.          {
  27.             min = a[i];
  28.          }
  29.       }
  30.       fprintf (fo,"Maximum  : %d\n", max);
  31.       fprintf (fo,"Minimum  : %d\n", min);
  32.       fclose(fo);
  33.       fclose(fi);
  34.       return 0;
  35.    }
  36. }
Mar 2 '07 #3
Ganon11
3,652 Expert 2GB
I've added a couple comments to the code where I see some errors:

Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. int main ()
  3. {
  4.    FILE  *fi;
  5.    FILE  *fo;
  6.  
  7.    fi = fopen("infile.dat","r");
  8.    fo = fopen("outfile.dat","w");
  9.    {
  10.       FILE *fi; /* input file pointer */
  11.       if ( (fi=fopen("infile.dat","r")) == NULL ) { // You reopen fi here - it is opened in line 7!
  12.          printf("ERROR opening file!\n");
  13.          return 1;  /* terminates program in error */
  14.       }
  15.       int i;
  16.       int a[10] = { fi }; // This is not a correct assignment statement.
  17.       int max = a[0];
  18.       int min = a[0];
  19.       for (i = 0; i < 10; i++) // You can loop starting from 1 - you've already checked a[0]
  20.       {
  21.          if (a[i] > max)
  22.          {
  23.             max = a[i];
  24.          }
  25.          else if (a[i] < min)
  26.          {
  27.             min = a[i];
  28.          }
  29.       }
  30.       fprintf (fo,"Maximum  : %d\n", max);
  31.       fprintf (fo,"Minimum  : %d\n", min);
  32.       fclose(fo);
  33.       fclose(fi);
  34.       return 0;
  35.    }
  36. }
In order to get the integer values from infile.dat, you will have to use fscanf with fi. Using the {} notation won't work, because you are trying to set an integer array to hold a file pointer - incompatible types.
Mar 2 '07 #4

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

Similar topics

2
by: chris | last post by:
Hi, I would like to take two documents and combine them. I can do this but I'm having a little problem with namespaces again. The input documents namespace is xhtml, but how do I tell the...
3
by: MostlyH2O | last post by:
Hi Folks, I am trying to output the content of my ASP page to an MS Word Document with a specific page size (8.5x11) and margins (.25" all around). I have used the Response.ContentType =...
11
by: milkyway | last post by:
Hello, I have an HTML page that I am trying to import 2 .js file (I created) into. These files are: row_functions.js and data_check_functions.js. Whenever I bring the contents of the files into...
10
by: jeff regoord | last post by:
A user inputs a float value. The scanf() function gets the value. However, I need to create an error handler with an if else statement saying invalid input if the input is not a number. Does...
1
by: David Van D | last post by:
Hi there, A few weeks until I begin my journey towards a degree in Computer Science at Canterbury University in New Zealand, Anyway the course tutors are going to be teaching us JAVA wth bluej...
2
by: kustomguitarist | last post by:
This is a strage seg fault...i threw some comments in so you can see whats going on. Thanks! any help would be appreciated. ...
0
by: raa abdullah | last post by:
Overview A conflict-serializbility\recoverability checker, SCR, is a program that takes in a schedule and outputs 2 decisions: Conflict serialzable or Not confilict serializable AND Recoverable or...
4
by: CDZ | last post by:
I don't know a ton about ASP but I do know a little and I can't seem to find any information about 'hyphen' and 'stripit'. It's used to return a last name like this:...
3
by: FaisalAl | last post by:
Write a complete program that inputs 2 integers from "cin" and uses "cout" to print the word "yes" if both numbers are even , otherwise the program outputs "no" #include <iostream> using...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.