C/C++ :: Read Numbers From A File Into Another Function?
Nov 2, 2014ok so I have three numbers in a file that I called in a function and I want to use all three numbers from that file in another function how can I??
View 9 Repliesok so I have three numbers in a file that I called in a function and I want to use all three numbers from that file in another function how can I??
View 9 RepliesWrite a program which reads a stream of numbers from a file, and writes only the positive numbers to a second file. The user should be prompted to enter the names of both the input file and output file in main(), and then main() will open both files. Another function named process() must then be called to read all the numbers from the input file and write the positive numbers to the output file. Note that you must pass the open stream variables for each file as arguments to the process() function, and that you need to (always) double check that the files opened successfully before using them.
This is what I have so far but its not working out!
#include <iostream>
#include <fstream>
#include <stdlib.h>
using namespace std;
int process(ifstream &inf, ofstream &outf);
[Code] ....
I am attempting to read a file with 2 numbers in it. The first indicates the number of rows the second, the number of columns. So for a file (Data.txt) that contains the numbers 5 7, I want to display
0 0 0 0 0 0 0
1 1 1 1 1 1 1
0 0 0 0 0 0 0
1 1 1 1 1 1 1
0 0 0 0 0 0 0
and write that output to a file.I can display the correct number of rows and columns but I can't figure out how to display alternating rows of 0's and 1's.
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ifstream inFile;//declare file
[code]....
I want to write a c++ code, which will read in a file like this:
A: 10
B: 20
C: 30
while in the code, I would need the value of A B C, how can I let the cin ignore the labels like A:?
of course, I can write a file as
10
20
30
.
.
.
and use cin>>A>>B>>C; to read in those
but in this way, when the number of values increase, it is really hard to remember who is who?
I'm having trouble with how to find the count and sum of all numbers.
The file, called question1.txt, contains numbers:
1
2
3
4
5
0
0
0
-2
-2
Here's my code:
Code:
#include <stdio.h>
#include <stdlib.h>
int main (void)
{
FILE* spIn;
FILE* spOut; //for output file
int numIn;
[Code]...
My project's goal is to read some numbers from input file,then create a grid.my input file is:
2 3
3 4 5
1 2 4
first line is rows and colums. second and third line the grid location values.My code is
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include<vector>
using namespace std;
[code].....
1. Construct a class diagram that can be used to represent food items. A type of food can be classified as basic or prepared. Basic food items can be further classified as meat, fruit, veg or Grain. The services provide by the class should be the ability to enter data for new food, to change data for food and to display existing data about food.
using this class definition write a main program to include a simple menu that offers the following choices:
1. Add food
2. Modify Food
3. Delete Food
4. Exit this menu
2. Read a list of numbers from a file and then print them out in reverse order. State whether the list is palindromic or not.
I need my program to read a list of numbers from and input file, random.txt, and calculate the following statistics on those numbers:
A. The number of numbers in the file.
B. The sum of all the numbers in the file.
C. The average of all the numbers in the file.
D. The largest number in the file.
E. The smallest number in the file.
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
[Code]......
This is what I have so far but I'm not sure if it is right. I can't get the input file to open.
called object 'fptr_in' is not a function or function pointer
called object 'fptr_out' is not a function or function pointer
what can i do for the errors?(i mustn't use loop and arrays for the code)
Code:
#include <stdio.h>
void sum_of_2nd_and_3nd( FILE* fptr_in, FILE* fptr_out);
int main()
return 0;
}
void sum_of_2nd_and_3nd( FILE* fptr_in, FILE* fptr_out){
}
[code].....
code:
/part of main function
int *A=NULL;
//load from file
load_from_file(A, &next);
printf("next=%d
",next);
getchar();
printf("A[%d]=%d
",0,A[0]);getchar();//here program crashes
}
[code]....
I initialize array A with NULL in main, and I "load" it with elements from a file. The code without function is working. But when I tried to use a function as above, my array crashes!!!
I am writing a program which compresses files into .zip files.
Here's my problem: Whenever I want to compress an executable file, my readFile function does not read the entire file. When I extract the .exe I get a very tiny and incomplete file.
Here's the function I use to read files:
std::string miniz_wrapper::readFile(FILE* f, int MAX_FILEBUFFER)
//MAX_FILEBUFFER has a default value of 65536 {
char* tmp;
std::string tmp_s;
int count = 0;
[Code] .....
Prior to reading, every file is opened using fopen with the mode "rb".
So for a project, my professor sent out two pages of code containing functions to read a text file (since we do not know how to write this on our own yet). I've got the code working on Orwell IDE and it gives me 2 warnings saying
"Passing argument 1 of 'readFromFile' from incompatible pointer type"
"Passing argument 2 of 'option2Print' makes integer from pointer without a cast"
The Orwell IDE seems to just bypass these warnings and compiles the code correctly. However, when I transferred my files over to my desktop using BloodShed (what the professor uses), instead of getting a warning I get an error and the code won't compile.
I assume it will not compile on his computer either since he uses the BloodShed IDE.
I don't know how to put the code directly into the text neatly, so a attached a .zip file with my code. The "storms.txt" file is also included. (the file that will be read).
I try to use passing function as argument but I'm stuck. I have two questions: First, I try to call uppercase and open .txt in tfm Second, How can I read characters from in.txt as string and assign to char content[] ?
#include <stdio.h>
void tfm( char str_filename[], void(*pf_convertion)( char content[]));
void uppercase(char content[]); //converts all letters to uppercase
int main(){
puts("-------------------------------");
printf("tfm:
");
tfm("in.txt", uppercase);
[Code] ....
Is possible read text form keyboard using read() function? or which is best way in ansi creplace input command form basic language. for numbers and text...
View 3 Replies View RelatedI want to read the contents of a file block (512 bytes) by block using low I/O read statements. Each record is 64 bytes long and has a pre-defined structure. The first 4 bytes are an unsigned integer; the next 20 bytes are ascii text, etc.
I have a buffer which I can access with buf[0] to buf[63] to read the first record and then buf[64] to buf[127] for the second, etc. However, I was wondering how to map a record so that I can refer to an integer as an integer and a float as float, etc. I can't create a struct and move the 64 bytes to it, as I will have alllignment/padding problems.
What is the standard way to deal with records in C?
Write a program that reads numbers from cin and then sums them, stopping when 0 has been entered. Add the following to your program:
-A counter within the while loop that keeps track of the number of inputs entered.
-A comment that determines if the total value is less than 100 or greater than 100, as in the sample output
-Switch statements to determine the total number of inputs
Sample output:
Enter numbers, one per line. Enter 0 to end:
7
8
6
5
5
9
8
0
The total was 48.
The total number of inputs read is 8.
The total is less than 100.
The code that I've got so far lets me keep track of the inputs entered, and displays the total of the values, but I'm not sure how to do the part where it determines if the value is greater or less than 100.
#include <iostream>
using namespace std;
int main() {
int val, total=0;
int i=0;
cout << "Enter numbers, one per line. Enter 0 to end:
";
[Code] .....
1): write program to c++ to read three numbers from keyboard and find the average of the three numbers?
2): write program c++ to read two numbers from keyboard and swap between numbers like x=4; y=6; the result x=6; y=4;?
I think that int can do that job. but im not sure.
3): must be the result of positive example: (x+(-y)=z)?
with abs ?
4): values for each of : X= ;y= ;z= ;
I am trying to read user input for recipe ingredients which must include a ingredient name, and may include a quantity and a unit. Example: Stone ground flour 2 cups or Sugar 1 Tbsp or Milk. The problem I am having is that the string gets cut off after a space when multiple words are used for the ingredient name.
cin.ignore();
string line;
getline(cin, line);
istringstream record(line);
string name;
string name2;
int quantity = 0;
string unit;
record>>name>>quantity>>unit;
Program to read in 15 numbers and display them as follows //each number on a separate line
Code:
# include <stdio.h>
# define MY_ARRAY 15
int main(){
int i ;
printf("please enter 15 numbers
[Code] ....
Error that i get from compiler:
Error E2062 array1.c 14: Invalid indirection in function main()
Error E2062 array1.c 19: Invalid indirection in function main()
both pointing to the separate "scanf"
How to write a program that will prompt and read 5 numbers. Then the program should print numbers that are larger than 80.
View 1 Replies View RelatedI want to write a program to read in 15 integers from the user, and then store them in a 5x3 array, and print out the numbers as a 3x5 array.
View 7 Replies View RelatedWhat data_type will read in a string of letters mixed with numbers using fstream :);
View 1 Replies View RelatedI have to complete a project that i want to read float numbers from keyboard and store them into an array of 10 elements.! Every time that a number stored into array i want to compare with previous one if they have +-10 difference .. I want to keep only 10 elements into my array so every time that i give value a[0] replace a[1], a[1] replace a[2],a[2] replace a[3]. . . .and a[10] deleted.. So when all elements of the array are similar with +-10 values print out the array.!
View 1 Replies View Relatedso basically my project goes like this:
Write a C++ program that reads in two positive integers that are 20 or fewer digits in length and outputs the sum of the two numbers.
Your program will read the digits as values of type char so that the number 1234 is read as four characters '1', '2', '3' and '4'. After they are read into the program, the characters are changed to values of type int. The digits will be read into a partially filled array and you might find it useful to reverse the order of the elements in the array after array is filled with data from the keyboard.
Your program will perform the addition by implementing the usual pencil and paper addition algorithm. The result of the addition is stored in an array of size 20 and the result is written to screen. if the result of the addition is an integer with more than maximum number of digits(that is more than 20 digits) then your program should issue a message saying that it has encountered "integer overflow".
You should be able to change the maximum length of the integers by changing only one globally defined constant. Include the loop that allows the user to continue to do more additions until the user says the program should end. What I have so far is
#include <iostream>
#include <cstdlib>
using namespace std;
void reverseArr(int a[], int liu);
void addLargeInt(int a1[], int liu1, int a2[], int liu2, int sum[], int& liu_sum);
int main() {
cin.get(next);
[Code]...
My C programming class wants us to write a program to read integers into an array, sort the numbers and print out the occurrence of each number. I've tried everything that I can think of but the core dump is still occurring.
void countValues ( FILE *inf, int list[], int size ); /* Function prototypes. */
void printFrequencies( const int list[], int size );
int main (void) {
int status = EXIT_SUCCESS; /* defaulting status to success. */
FILE *inf = fopen( "numbers.txt", "r" ); /* input data file */
[Code] .....
The Objective Of This Program Is To Create A File To Write Text And Read Back The File Content. To Do That I Have Made Two Function writeFile() To Write And readFile() To Read.The readFile() function works just fine but writeFile() doesn't.
How writeFile() function Works? when writeFile() function Execute It Takes Characters User Type And When Hit Enter(ASC|| 10) It Ask "More?(Y/N)" That Means What User Want? Want To Go Next Line Or End Input?
If "Y" Than Inputs Are Taken From Next Line Else Input Ends.
But The Problem Is When Program Encounters ch==10 It Shows "More?(Y/N)" And Takes Input In cmd variable.If cmd=='Y' I Mean More From Next Line Than It Should Execute Scanf Again To Take ch I Mean User Input.But Its Not!!! Its Always Showing "More?(Y/N)" Again And Again Like A Loop.
Code:
#include <stdio.h>
void writeFile(void);
void readFile(void);
int main(){
[Code].....