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. - #include<stdio.h>
-
int
-
main ()
-
{
-
int i;
-
int a[10] = { 10, -99, 888, 4, 234, 1, 30, 40, 22, 34 };
-
int max = a[0];
-
int min = a[0];
-
-
-
for (i = 0; i < 10; i++)
-
{
-
if (a[i] > max)
-
{
-
max = a[i];
-
}
-
else if (a[i] < min)
-
{
-
min = a[i];
-
}
-
}
-
printf ("Maximum : %d\n", max);
-
printf ("Minimum : %d\n", min);
-
-
return 0;
-
}
-
-
/* This is the section I have been trying to add but I dont no where, can someone help me please. */
-
-
FILE* fp;
-
fp = fopen("mydata.txt", "r");
-
-
FILE* fp;
-
fp = fopen("output.dat", "w");
3 1361
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()?
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 - #include<stdio.h>
-
int main ()
-
{
-
FILE *fi;
-
FILE *fo;
-
-
fi = fopen("infile.dat","r");
-
fo = fopen("outfile.dat","w");
-
{
-
FILE *fi; /* input file pointer */
-
if ( (fi=fopen("infile.dat","r")) == NULL ) {
-
printf("ERROR opening file!\n");
-
return 1; /* terminates program in error */
-
}
-
int i;
-
int a[10] = { fi };
-
int max = a[0];
-
int min = a[0];
-
for (i = 0; i < 10; i++)
-
{
-
if (a[i] > max)
-
{
-
max = a[i];
-
}
-
else if (a[i] < min)
-
{
-
min = a[i];
-
}
-
}
-
fprintf (fo,"Maximum : %d\n", max);
-
fprintf (fo,"Minimum : %d\n", min);
-
fclose(fo);
-
fclose(fi);
-
return 0;
-
}
-
}
I've added a couple comments to the code where I see some errors: - #include<stdio.h>
-
int main ()
-
{
-
FILE *fi;
-
FILE *fo;
-
-
fi = fopen("infile.dat","r");
-
fo = fopen("outfile.dat","w");
-
{
-
FILE *fi; /* input file pointer */
-
if ( (fi=fopen("infile.dat","r")) == NULL ) { // You reopen fi here - it is opened in line 7!
-
printf("ERROR opening file!\n");
-
return 1; /* terminates program in error */
-
}
-
int i;
-
int a[10] = { fi }; // This is not a correct assignment statement.
-
int max = a[0];
-
int min = a[0];
-
for (i = 0; i < 10; i++) // You can loop starting from 1 - you've already checked a[0]
-
{
-
if (a[i] > max)
-
{
-
max = a[i];
-
}
-
else if (a[i] < min)
-
{
-
min = a[i];
-
}
-
}
-
fprintf (fo,"Maximum : %d\n", max);
-
fprintf (fo,"Minimum : %d\n", min);
-
fclose(fo);
-
fclose(fi);
-
return 0;
-
}
-
}
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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 =...
|
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...
|
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...
|
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...
|
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.
...
|
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...
|
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:...
|
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...
|
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,...
|
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$) {
}
...
|
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...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
| | |