C/C++ :: Reading Numbers From Txt File?

Sep 19, 2014

My code is working all except one part I'm just completely stuck on and it's probably something simple. There are 14 numbers in the file. The way I have the code now it will tell me there are 14 which is correct but it displays my low number as 0 and it should be 6. If I change if(infile) to if(infile >> num) it will read the correct low number as 6 except now it says there's only 13 numbers and it's not reading the first number in the file which is 23 so it skews the whole program.

#include <iostream>
#include <fstream>
/*This program pulls numbers from a file and displays various information such as the sum of the numbers, average, highest and lowest. */
using namespace std;
int main() {
ifstream inFile;//Declare the variables including the input file stream.

[code]....

View 2 Replies


ADVERTISEMENT

C :: Reading 16 Bit Binary Numbers From File

Mar 6, 2015

This is supposed to read a series of 16 bit binary numbers as short ints in the array memory. However, it only appears to be reading the first number.

Code:
short int memory[256];
void readFile(){
//read input file into int memory[]
FILE *ifp;
ifp = fopen("proj1a.dat", "r"); //input file

[Code] ....

View 9 Replies View Related

C :: Reading Numbers From A Text File

Feb 23, 2013

The program runs fine but i just want it to read the inputs from a text file rather than user manually entering it! what changes should i make in the codes..

Code:
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<math.h>
float friction,flow;

[Code] ....

View 4 Replies View Related

C :: File I/O Reading In Characters And Numbers

Dec 1, 2013

I think it comes from having to align the data in the output file. How to start this is to use a function to read in all the characters using a while loop. Then read in all the digits in a separate function. When it comes to outputting the data in the correct format I'm lost, so for now if I could figure out the functions that would be awesome. A final note we have not gotten to using strings so the I'm trying to use getc, fgetc, and ungetc.

Write a program to compute average grades for a course. The course records are in a single file and are organized according to the following format: Each line contains a students first name, then one space, then the students last name, then one space, then some number of quiz scores that, if they exist, are separated by one space. Each student will have zero to ten scores, and each score is an integer not greater than 100. Your program will read data from this file and write its output to a second file. The data in the output file will be the same as the data in the input file except that there will be one additional number at the end of each line: the average of the students ten quiz scores.

The output file must be formatted such that first and last names appear together in a left justified column that is 20 characters wide. Each quiz score should be listed in a right justified column that is 4 characters wide, and the average should appear in its own right justified column that is 10 characters wide.

Note that if a student has fewer than 10 scores, the average is still the sum of the quiz scores divided by 10; these students are assumed to have missed one or more of the quizzes. The output file should contain a line (or lines) at the beginning of the file providing appropriate column headings. Use formatting statements to make the layout clean and easy to read.

View 3 Replies View Related

C++ :: Reading From A File With Characters And Numbers

Apr 17, 2014

I need to open a file that has multiple lines that look something like this, " black box 100.01 33.5 78.93". What this shows is the coordinates of the black box. Lines will have different objects with different coordinates. I need to create a loop that will read this file and tell me when a black box is found by displaying a message. I don't need to know how to create this file. I don't need to display the entire file but rather have it search for black boxes.

View 3 Replies View Related

C/C++ :: Reading Binary Numbers From File?

Feb 9, 2014

I wanted to know if it is possible to read binary numbers(0's and 1's) from a file(text,picture, anything), and see them in the compiler.

View 14 Replies View Related

C++ :: Reading Certain Numbers At A Time From Int File

Sep 15, 2013

I am reading certain int's at a time from one int number stored in a file. I'll explain. I am working on an ezpass project and basically I have to store an int in a file, and from that file, write a program that separates numbers into information.

For example:

the number 204601324 is stored in a file. I know how to open a file from terminal, but the issue is reading the individual numbers. The first number is weight class (2). My program has to display that number as the Variable "weightClass." how do I get it to do that? In addition, miles allowed is the number "0460." How do I get that number to display as the variable "oMiles?"

View 1 Replies View Related

C++ :: Reading Numbers From A Text File

Sep 25, 2014

I am trying to read numbers stored in a txt file to populate an integer array. The problem is that the numbers read from a file have to be treated as strings and then I am not able to put them in an integer array.

I tried the stoi function to convert the string in 'box' into an integer but I am getting an error.

Code:
#include <iostream>
#include <stdlib.h>
#include <fstream>
#include <string>
using namespace std;
int main() {
int amount=10;
int k[amount];

[Code] ....

View 2 Replies View Related

C++ :: Reading Data (Constant Numbers) From A File

Feb 24, 2015

How do I get c++ to read a file containing six numbers - a1, b1, a2, b2, a3, and b3 - that are constants for the following equations:

clubangle(degrees) = a1 + b1*0.85*clubnumber

clublength(inches) = a2 + b2*1.05*clubnumber

clubspeed(yards/s) = 1.1 * (a3 + b3 * swingnumber) * (clublength(inches)/40)^2;

View 5 Replies View Related

C :: Reading In Comma Separated Numbers From A Data File

Apr 18, 2013

I am attempting to read in a file that has 4128 sets of 21 numbers separated by commas and write it into an array. I now know that in order to use fseek, I have to make my array a character array, but I need my function to read in decimals (ex: 0.172635). I'm reading in

0.102598,0.000000,0.000000,0.000000,0.000000,0.307 793,0.000000,0.410391,0.102598,0.000000,0.102598,0 .102598,0.000000,0.000000,0.102598,0.102598,0.8207 83,0.000000,0.000000,0.000000,0.000000

and keep getting numbers like 48 49 50.

Code:

void CSread(char filename[100], char array[22], char array2[22], unsigned int arraysize)
{
char genename[32];
double temp = 0;

FILE *CSfile;
CSfile = fopen(filename, "r");
");

[code].....

View 8 Replies View Related

C++ :: Reading Group Of Numbers Per Line In Text File

Mar 27, 2013

I am working on a project for school that has us read in a text file that contains 8 lines of SSN and grades. I am not sure how to go about reading in the lines and differentiate between the SSN and grades in the calculations.

View 1 Replies View Related

C++ :: Read Stream Of Numbers From A File And Writes Only Positive Numbers To Second File

Mar 27, 2013

Write 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] ....

View 1 Replies View Related

C/C++ :: Reading Numbers From Two Files

Jun 11, 2014

My assignment was to take numbers from two different files, and merge them into a third file in numerical order. I have everything working well, but the output shows the lowest number everytime and i'm not sure why. logical error ....

Sample:
File1 File2 File3
10 12 10
20 25 12
30 37 10
40 49 25
10
37
10
49

Here is my code as well

#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
int main() {
ifstream File1, File2;
ofstream Output;

[Code] ....

View 14 Replies View Related

C++ :: Reading Numbers From A Text Document?

Oct 11, 2014

so the data file is a .txt that is going to be pre-written like this:

numbers:

562
444223.123321 312233.31222 3232323.31122
332231.323223 333232.32323 3232322.32233

am i reading the txt correctly? why are the numbers so weird when i "cout" to check?

#include <iostream>
#include <cmath>
#include <cstdlib>
#include <fstream>
#include <sstream>
#include <ctime>
#include <stdlib.h>
using namespace std;
int main() {
double mb1; //mass body 1

[code]....

View 4 Replies View Related

C/C++ :: Reading Lines Of Numbers / Characters While Checking For EOF?

Aug 30, 2014

Simple program to convert files to XML files. However I am trying to narrow down why my code is not working. I am reading a line that looks like "2001 Joe Dirt Los Angeles", then the next few lines are followed by text and the the fifth line looks like "---End of Description---".

So here is the file

2001 Joe Dirt Los Angeles
Home is where you make it
Best movie ever(but not really)
But seriously
---End of Description---

Here is example code to test the process

string line;
while(dataFile >> line) {
getline(dataFile, line);
cout << line << endl;
}

My returning output is

[space]Joe Dirt Los Angeles
[space]Home is where you make it
[space]Best movie ever(but not really)
[space]But seriously

My test code is skipping the date on the first line but replacing it with a space. Then it is skipping my fifth line which reads "---End of Description---". Now if I take the code out of the while loop and hard code in five lines of output (x5 getline(dataFile,line); cout << line << endl;) then my code works as expected. I get all of the information. Date and the fifth line.

It looks like when I am testing for the EOF it is also taking in the numeric value at the beginning of the first line. However, that does not explain why it is dropping that fifth line.

View 3 Replies View Related

C :: Reading Three Digits At A Time As One Number In Long String Of Numbers

Nov 29, 2013

How can I read a file that contains numbers only, but read it by three digits at a time? I have a long string of numbers and every three digits corresponds to a particular number in itself. i.e. a string of 064045154 would need to be read as '064' '045' and '154'. I need to then subtract one from each of these numbers and the new values I need to convert into their ASCII characters and place these in a new file. This is what I have (focusing on the 'Decrypt' function) but all it does is in the new file place a string of the same character repeated over and over a total number of times equal to the number of integers in the numbers file.

Code:

#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "limits.h"
int Encrypt(char * FILENAME)

[Code]..

View 1 Replies View Related

C :: Reading From A File And Skipping Lines To Find String In File

Nov 26, 2013

Program background: Ticket Sales Details You will sell tickets in advance and at the door. Prices for buying in advance and at the door will be given. Also, the total number of tickets sold in advance will be given. Each guest will have a unique number. If there are n tickets sold in advance, then these guests will be numbered 0 through n-1. As the event starts, requests to buy tickets at the door may be made and these guests will be numbered sequentially, starting at the lowest unassigned number. The maximum number of guests will be 1000. The first line of the file contains the following three values, separated by spaces: Cost of the presales tickets (in dollars), Cost of the tickets at the door (in dollars), and the number of presale tickets. The first two values will be positive real numbers to two decimal places and the last will be a positive integer.

The second line of the file will contain one positive integer representing the number of auction items followed by a positive real value to two decimal places (at most) representing the minimum bid increment, in dollars. The first value is guaranteed to be 1000 or less and the second will be in between 1 and 50, inclusive.

The third line of the file will contain all the prices of the auction items, in dollars, separated by spaces, in order. Thus, the first price is the price of item 0, the next price is the price of item 1, and so on. These values will be real numbers represented to up to 2 decimal places.

The fourth line of the file will contain the three following positive integers pertaining to the raffle: the number of raffle tickets available, the cost of a raffle ticket in dollars, and the number of raffle prizes. (It's strange to have raffle tickets that don't cost a whole number of dollars.)

The fifth line of the file will contain each of the values of the raffle items, in dollars, separated by spaces, in order. Thus, the first price is the price if item 0, the next price is the price of item 1, and so on. These values will be real numbers with upto 2 decimal places.

The sixth line of the file will contain ten positive integers representing the number of each of the drinks 0 through 9, in order, that are in stock.

The seventh line of the file will contain ten positive real numbers with upto 2 decimal places representing the price of each of the drinks 0 through 9, in order.

The eighth line of the file will contain a single positive integer, numEvents, representing the number of events that occur at the charity ball. These events are split into two groups: actions by guests at the ball and awards given (raffle, auction, person, totalrevenue). All of the actions precede all of the awards. You will produce exactly one line of output for each event described. Here are the formats of each event that could occur:

If a patron buys a ticket at the door, a command will be on a line by itself:

BUY TICKET k

where k is a positive integer indicating the number of tickets bought at the door. These guests will be numbered as previously mentioned. You are guaranteed that the total number of tickets bought, including presales, will not exceed 1000. This is what I have so far and I cannot figure out why it wont calculate the total revenue. I am not completely sure if it is even accessing the if statement in main.

Code:

#include <stdio.h> #include <stdlib.h>
#include <string.h>
#define N 1000

[Code].....

View 4 Replies View Related

C++ :: Reading File Then Printing Data Onto Other File

Nov 7, 2014

I am reading a file then printing the data onto the other file. It is working, however when I check to see if each variable is being properly set after reading the file a issue arises.

Example of the file being read

Code:
Vehicle PV50CAN passed camera 1 at 05:33:26.
Vehicle W867BRO passed camera 1 at 05:33:29.
Vehicle KQ63ARU passed camera 1 at 05:33:38.
Vehicle K954ITQ passed camera 1 at 05:33:40.
Vehicle V220MXB passed camera 1 at 05:33:42.

[Code] .....

View 14 Replies View Related

C/C++ :: File I/O - Reading Values From File

Jan 28, 2014

I am trying to write a program that will open a file with 2 points, take those points, determine what quadrant those points are in, and save the result back into the same file without truncating the data.

I'm having some trouble with my file I/O. I can't get my program to read the two values for X and Y that I put in the points.txt file. It simply uses whatever random number was used last time in that block of memory. In my points.txt file I put something like "4 -5" and it wont assign those numbers to my X and Y variables. I believe the issue is on line 25

#include <iostream>
#include <fstream>
using namespace std ;
int main() {
//defining X and Y
double X, Y ;

[Code] ....

View 14 Replies View Related

C++ :: Reading From A TXT File?

Jan 5, 2014

If I am reading data from a .txt file with a person's last name and first name (e.g. Miller Andrew), How do I output the same data with the first name followed by the last ?(e.g. Andrew Miller) Do I use the ignore function?

View 2 Replies View Related

C++ :: Reading PGM File From HDD

Aug 30, 2014

I want to know how to read a .pgm file from my hdd and store it in a 2d array so i can perform some action on the array later on. This is ny first time working with an imqge in c so i know nothing about it. .

View 3 Replies View Related

C/C++ :: Reading ID3 Tag From MP3 File

May 25, 2014

I have been given a task to make a c++ program that promptsd the user to enter a mp3 file name, read the ID3v1.1 tag and displays it. It then should prompt the user to to decide if they want to edit the comments section of it, then if they edit the section, it should re-print the updated ID3 tag. The list of genres are in a cstring that is as follows.

char genre[126][128] =
{
"Blues" "Classic Rock", "Country", "Dance", "Disco", "Funk",
"Grunge", "Hip-Hop", "Jazz", "Metal", "New Age", "Oldies", "Other",
"Pop", "R&B", "Rap", "Raggae", "Rock", "Techno", "Industrial",
"Alternative", "Ska", "Death Metal", "Pranks", "Soundtrack",

[Code] .....

View 1 Replies View Related

C/C++ :: Reading From TXT File - Last Value Appear Twice

Aug 22, 2012

Using the feof() function in the output file i have the last value twice.

This is the input:
1 3 6
1 1 4
0 3 1
1 1 6
0 6 2
1 1 6

And the output should be:
12
6
15
13

But using the feof() the last value appears twice:
12
6
15
13
13

How can i change that?

while(!feof(pfile1))  {  
        sum=0;
        fscanf(pfile1,"%d%d%d",&choice,&day,&val);
        if(choice==0)

[Code] ....

View 1 Replies View Related

C++ :: Write Negative To One File And Positive Numbers To Another File

Dec 11, 2013

im supposed to create a program that reads in a list of integers from the terminal and writes the negative numbers to one file and the positive numbers to another file.

i got most of it doen but for some reason its not writting the negative numbers. on what im doing wrong?

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(){
int pos_num = 0;
int neg_num = 0;
int positive_numbers = pos_num % 5;

[Code]...

View 2 Replies View Related

C++ :: Reading A JPEG File?

Dec 9, 2014

How to read a grayscale jpeg image in C++ ?

How do I write the code to read the jpeg file and store its content in a 2D array

View 1 Replies View Related

C++ :: Reading Spaces From TXT File

Mar 29, 2014

I am practicing with C++ since I done most of C already and I have a problem with reading spaces from a .txt file.

I created a little program that creates a wannabe Cube in wannabe 3D xd, well to appear as 3D like:

Code:
cout << " O------O" << endl;
cout << " / /|" << endl;
cout << " / / |" << endl;
cout << " / / |" << endl;

[Code] .....

The actual code is a bit longer since it offers you to input the size and then it draws the pic. Now that wasn't so hard and I've done that but now I wanted to implement the "MessageBox" func for output.

I managed to write the cube in file cube.txt but when I'm reading from it 1 char at a time since I need to output as char array it avoids all spaces and new lines and just puts all symbols in the same row.

I didn't have that issue with C and I've found on stackoverlow a solution using strings & getline but I need it to be in "char" form.

How to actually read spaces and newlines? This is my current code for reading from file:

Code:
ifstream di("kocka.txt", ios_base::in);
char c[5000];
int br=0;
while( di >> c[br])
{
br++;
}
MessageBox(NULL, c, "Kocka", MB_ICONHAND);
di.close(); P.S kocka = cube (in croatian )

View 1 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved