C++ :: How To Read Data From TXT File Into Array

Feb 25, 2013

I wanna read a txt file like this
1.00518 2.01903 3.01139 4.01343 5.02751 5.99913 7.00011 7.99851 .....
and fill the array P[i][j] with the data.

I try to use

if (myfile.is_open()) {
//for (int i = 0; i < 1; i++)
//{
getline (myfile,DMArray[0]);
cout << DMArray[i] << endl;
//}
myfile.close();
}

But this read the data into a string. How should i do with this string. Besides I know the dimension of the data I am reading.

View 1 Replies


ADVERTISEMENT

C++ :: Read A File Into 2D Array And Display Data

Nov 15, 2014

I am currently working on a problem where I read a file into a 2D array, and display data. If you saw my code earlier, this is the same problem, but I am now further along.

The file (90 characters) looks like this:
S
R
R
S
R
R
R
R
S
S
....

The "R" resembles rainy days, the "S" resembles sunny days, and the "C" resembles cloudy days. There are 90 characters in the file. The first 30 are for the month of June, the next 30 are for the month of July, and the last 30 are for the month of August. The 31st day of the months are NOT included.

I read the data into a 3 x 30 array, where the row indicates the month (0 = June, 1 = July, 2 = August) and the column indicates the number of days in each month.

Now I need to create a function that creates a report and displays, for each month AND for the whole 3 month period:
1) The number of rainy days
2) The number of cloudy days
3) The number of sunny days
4) Which of the 3 months had the largest number of rainy days

Here is my code so far:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
const int MONTHS = 3;//Number of months

[Code] .....

There are no compiling errors, but I am not getting the data I want. My rCount and cCount variables end up being 0, and my sCount variable is 90.

View 4 Replies View Related

C++ :: Read From A File And Place Data Into Array

May 3, 2014

Assume you want to read from a file and place the data from the file into an array. The array can hold 10 integers. The file may contain any number of integers with 1 integer per line. Fill in the missing loop test condition so that we will read from the file until either end of file occurs or the array is full. Note we are using cin to read from the file since input redirection is being used.

cin >> temp;
arrayIndex = 0;
while ( _____________________________________) {
array[arrayIndex] = temp;
arrayIndex++;
cin >> temp;
}

View 2 Replies View Related

C++ :: Unable To Read Data File From Two Dimensional Array

Apr 27, 2014

I am new to C++ and I am having an issue reading in a 2 Dimensional array from a data file. I am very close to reading it in perfectly except for one issue, the loop is ignoring the first value from the data file.

This is the code I have so far:

#include <iostream>
#include <string>
#include <cctype>
#include <algorithm>
#include <functional>
#include <conio.h>
#include <iomanip>
#include <string>
#include <fstream>

[Code]...

This is the values from the data file:

300 450 500 210
510 600 750 400
627 100 420 430
530 621 730 530
200 050 058 200
100 082 920 290

When the Program shows my array it comes up as:

450 500 210 510
600 750 400 627
100 420 430 530
621 730 530 200
050 058 200 100
082 920 290 0

Every array location is moved up one, like the program just completely ignored the 300 at the beginning.

View 1 Replies View Related

C++ :: Sort Data From Text File Into Array As Its Being Read?

Feb 12, 2013

I am suppose to make a program that reads in data from a text file (integers only) and sorts them as it inserts them into an array of size 10. I did this using an insertion sort, which worked great. But now I am being told that I need the function has to read ALL of the numbers in the text file, not just the first 10, and I am not allowed to store them THEN sort, it has to be sorted as being stored.

This is what I have for sorting first 10

void sortArray(int iArray[])
{
string fileName = "";
fstream inFile;
int tmp = 0;

[Code]....

View 3 Replies View Related

C++ :: Read Data From File Then Make It Into Array Of Structures And Print Afterwards

May 13, 2012

I am trying to read from a data file that has input as :

12.0, 11, 123
14.0, 12.1, 3

And I want the program to read the data from the file and then make it into an array of structures and then print that array afterwards.

Code:
#include <stdio.h>
#include <stdlib.h>
#define MAX_INPUT 1000
int n =0;
int i = 0;
typedef struct {
double x[MAX_INPUT];

[Code] .....

The program when run gives the following output:

Ishtiaque-Mohammed-Khans-MacBook-Pro:Comp20005 IshtiaqueMKhan$ gcc -Wall -ansi -o ProjectB ProjectB.c
ProjectB.c: In function "main":
ProjectB.c:59: error: incompatible type for argument 1 of "print_array"

View 1 Replies View Related

C :: How To Read Data In A File And Print It Then Copy Contents To A File

Mar 8, 2013

Code:

#include <stdio.h>
#include <stdlib.h>
int main (int argc, char *argv[])
{
char buffer[256];
FILE * myfile;
myfile = fopen("read.txt","r");

[Code]...

I also got an error in printing wherein the data in read.txt is "Hello" newline "World" and my program prints world twice.

View 3 Replies View Related

C++ :: Using Loop To Read In Data From A File?

Oct 30, 2013

My function is only reading the first line of numbers even though I have the loop to keep returning to it in main(). How do I have it read from the file until it runs out of data to retrieve?

File being read

90 83 94
72 65 81

File being sent the data(Only the second average is correct, working on the first)

Student Information
Student IDGradeAverageLetter Grade
1895612
90 83 94
289
90 83 94

#include <fstream>
#include <iostream>
using namespace std;
ifstream infile;
ofstream outfile;
void grades(int&,int&,int&); // Grades from the input file
int myavg(int,int,int); // Calculates the Average of the 3 grades

[Code] .....

View 8 Replies View Related

C/C++ :: Read Bytes From A Data File

Dec 5, 2014

I am a member of a group still developing the Microprose European Air War game. We have the source code and permission to do this. As the code dates back to 1998 I use Visual Studio 2006.

I have written many programs in Visual basic, and have the following one to read the airbase value in the "targets.dat" file. The file has a four byte header containing the number of tatgets, and a 32 byte data block containing the data for each target.

Private Sub Command1_Click()
List1.Clear
Dim nt As Long
Dim a4() As Long  
Open App.Path & " argets.dat" For Binary Access Read As 1
'read and display the number of targets  

[Code] ....

Despite many attempts I cannot write the C equivalent of this code into the eaw.exe source. All I really need to know is how to open the 'targets.dat' file and how to get the value of a byte (or integer, or long) at a given position as in the "Get #1, k, a4(n)" line.

View 1 Replies View Related

C :: Read Link List Data From File

May 7, 2013

i write this program and every things work correct except when i want to add data of file to the link list .when i add the data to the link list by add_to_linked_list() every things work correct but when i use read_linked_list_from_file() it doesn't work

Code:

#include <stdio.h>
#include <stdlib.h>
// double linked list structure
struct linked_list {
struct linked_list *next;
struct linked_list *prev;
char *value;
};

[code]....

View 2 Replies View Related

C++ :: Read Data From A File - Show Max And Min From Scores

Feb 26, 2013

My program reads data from a txt file and it displays the test scores and the sum of the scores, and it also shows the max and min from the scores , so far only one number shows up from the scores instead 10 and i think my sum is wrong too, but i cant get the max and min to work ...

#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;
void openfile(ifstream &infile);

[Code] .....

View 17 Replies View Related

C/C++ :: Read A Line With 8 Bytes Of Data From A File

Jul 23, 2012

How to read a line with maximum of 8 bytes from a line in a file.

Let say line has 10248 bytes in one single line and same repeats for remaining lines in the file.

I need to read the first 8 bytes of a line and go on read the last 2 bytes of the line and repeat the till end of file.

View 3 Replies View Related

C/C++ :: Read BMP Image Data Into Multidimensional Array Of Structures

Apr 8, 2014

I am trying to read the information from a .bmp file into my program while dynamically allocating memory for all of it. So I defined the structures for the headers and for a pixel, and then I have to read each pixel into a multi-dimensional array of pixels. I am completely new to structures, and definitely new to pointers combined with structures. I will have to use a filter with the information later, but right now I just want to read everything in correctly. The array of pixels will be two-dimensional -- the width by the height of the image, I guess.

Here's my code:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
FILE *inFile, *outFile;
typedef struct

[Code] ....

View 4 Replies View Related

C++ :: Not Able To Read Data From Text File And Convert It To Integer

Mar 25, 2013

I am trying to read an array values from Tmin.txt file. I take array size from user viz. number of raw & column and then, read the following data from Tmin.txt file which look like this:

20 40 20 25 30

20 30 40 20 25

20 40 20 25 30

20 30 40 20 25

30 40 40 30 40

I am using getline, and then converting it to int by using atoi. But my code is not working.

Code :
// reading a text file
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

[Code] ....

View 10 Replies View Related

C :: Read Data File And Emit Various Bits Of Information

Mar 31, 2014

For my intro to c programming class, our homework requires us to read a data file and emit various bits of information. the data file looks like this

#Domain: the URL
#Alexa: some kind ranking, small numbers are better
#Rank: another sort of ranking, big numbers are better

#Domain Alexa Rank Site Name
#------------ ----- ------- ---------
amazon.com 13 1177136 Amazon.com
google.com 1 4533883 Google
youtube.com 3 3637788 YouTube

[Code] ....

I've started and am able to get the correct number of lines, and the alexa number, but I keep getting some errors and am just unsure where to correct my code. This is what it looks like;

Code:
// Lexi Anderson
//CS 156
#include <stdio.h>
int main() {

[Code] .....

and once compiled, I get errors.

View 7 Replies View Related

C :: Unable To Read Data From Text File And Print It

May 12, 2014

I am having trouble in reading data from my text file and print it out exactly like how it looks like in the text file. The problem im having is it will not read the last y Coordinates of the point. it keep reading the second last point for y coordinates which is wrong.

my text file is
0.0 0.0
0.0 1.0
1.0 0.0(but it read until here)
0.0 0.0(suppose to read the last point which is here)

For your information, this is my 1st year degree assignment in C programming. It is to create a program which can read text file (manually create) and print it out in a program and calculate the area for the polygon using ADT function ( .c and .h files)

*This is the code for my read file function*

Basically this accepts a Polygon and a file pointer as parameters. It reads the polygon point data from the file, pass the read data to plg_new() to create a new Polygon and returns the new Polygon created.

Code:

polygon *plg_read(polygon *New_polygon, FILE *Coord) {
int i;
int numberofvertices=0;
int count=0;
char filename[50];
double xCoor[50], yCoor[50];

[Code]....

This is the second function my polygon new code. This ADT function basically creates a new Polygon with malloc(), initialize all ADT data members with its parameter values and returns the Polygon.

Code:

polygon *plg_new(double *xCoordinates, double *yCoordinates, int numberOfVertices) {
int x;
polygon *New_poly = (polygon *)malloc(sizeof (polygon));
if(New_poly->xCoordinates == NULL || New_poly->yCoordinates == NULL) {
free(New_poly);

[Code]....

This is the rest of the code if you need to refer to other codings.

Code:
/**
*@file polygon.c
*@brief Functions for polygon / Struct has polygon numberOfVertices, polygon *xCoordinates and polygon *yCoordinates
*@author: Tan Xian Yao
*@id: 4323440
*@date: 22/04/2014
*/

[Code]...

View 6 Replies View Related

C++ :: Read Input File Of Data And Store Them In Arrays

Nov 5, 2013

Read the input file of data ( employees.txt) and store them in arrays. This company can have up to 55 employees [b]i need to do these following in these program:

Write a function to read the input file and store the data in arrays.
Write a function to calculate regular pay.
Write a function to calculate overtime pay
Write a function to calculate gross pay.
Write a function to bubble sort the employees into order by last name, first name.
Write any swap functions that are needed.
Write a function to write output to a file called payroll.txt

Format of file is EMPLOYEES.TXT[/b]

Hours Pay Rate Employee Number First Name Last name
40.0 10.00 A1234 Jane Adams
50.0 10.00 L8765 Mary Lincoln
25.5 10.85 W7654 Martha Washington
52.0 15.75 A9876 John Adams
45.0 25.00 W1235 George Washington
40.25 55.00 L9087 Abraham Lincoln
30.0 9.75 T9876 William Tell
42.5 12.50 M7654 Missy Muffett
30.0 10.00 P8765 Peter Piper

HERE IS MY CODE SO FAR:

#include <iostream>
#include <cstdio>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
void tellUser();

[code]....

View 9 Replies View Related

C/C++ :: How To Reuse Existing Code To Read Data File In Qt

Aug 13, 2014

I've got some code in C++ that does some basic analysis on price data and I run it from the cmd prompt. One of the functions I had built to read in the data is as follows:

void ReadPricesFromFile(constchar*filename,std::vector<PriceInfo>&prices)
{std::ifstreaminput(filename);
input>>prices;}

How would one merge this type of code into a Qt GUI? Ideally, I'd like to use something like QFileDialog to open up a folder with my data files, select a file, and then read the file that the user selects. Would the main.cpp look something like this?

int main (int argc, char *argv []) {
QApplication prog (argc, argv);
QPushButton *load_button = new QPushbutton ("Load File");
QObject:: connect (load_button, SIGNAL (clicked()), &prog, SLOT ( (ReadPricesfromFile function?);
load_button-> show ();
return prog.exec ();}

View 3 Replies View Related

C# :: Read Data From More Than One File At Once - Not All Code Paths Return A Value

Apr 11, 2015

I am trying to read data from more than one file at once. The files are different types e.g. one is a text file one is an xml file like so, StudentInformation.txt, CollegeInformation.xml. The files are all stored in one place, in this case on the D drive of a local computer. I am trying to locate any files in the D drive with a file extension of .txt or of .xml (there may be more than two of these files in the future, so I'm trying to allow for that). Then I want to open all of these files, extract the information and output all the information in one display window. I want all the information from these two or more files to be displayed together in the display window.

Here is the code so far. It is throwing up errors.

//Load from txt files
private void btnLoad_Click(object sender, RoutedEventArgs e)
{
IEnumerable<string> fileContents = Directory.EnumerateFiles("D:\", "*.*", SearchOption.TopDirectoryOnly)
.Select(x => new FileInfo(x))
.Where(x => x.Extension == ".xml" || x.Extension == ".txt")
.Select(file => ParseFile(file));}

[Code] ....

The error it throws up is:
Error 1 'BookList.Mainwindow.ParseFile(System.IO.FileInfo)': not all code paths return a value

View 2 Replies View Related

C++ :: Read Phone Number Into Array And Can't Use String Data Type

Jan 13, 2013

I have an assignment where I need to read in a phone number into any array and I can't use the string data type. Assuming I need to use the getline function, how do I do this? I'm guessing it's with the cstring or .c_str() functions? I don't know.

View 4 Replies View Related

C :: Program To Read Binary Data And Print It - File I/O Error

Mar 14, 2013

Objective of this program is to read binary data from a file and print it.

Code:
#include <stdio.h>
void readFile(void);
int main(){
readFile();
return 0;

[Code] .....

But When I Run This Code First It Print "Error." Then Rest Of The File.Say In My File I Have "I AM HUMAN", It Prints "Error. HUMAN"!!

I Cant Figure Out Whats Wrong In The readFile() Function.It seems right to me.

View 4 Replies View Related

C :: Read Specific Amount Of Data From File Until Terminating Set Is Reached

Jul 23, 2014

I wrote a code to read a specific amount of data from file until terminating set is reached. however the code does what its supposed to correctly until it reaches the 5th loop it justs stops responding. I've checked the reading file and all elements are correct in the file.

Code:
int main(void){
FILE *file1;
FILE *file2;
FILE *file3;

struct t test1;
struct t test2;

[Code] ....

Screenshot of program crashing:

Screenshot of reading file:

View 11 Replies View Related

C++ :: Open A Text File Then Read Data And Print It Back?

Nov 5, 2013

I am in an "intro" C++ course and am having trouble with a section of my current project.

We are required to open a text file, read the data from that file, and print it back.

However, we are supposed to let the user input the name of the file that is to be opened. This is the code I have so far. It is not opening anything.

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
const int NUM_QUESTIONS = 30;
typedef char CharArray[NUM_QUESTIONS];
string fileName;

[Code]...

View 2 Replies View Related

C++ :: Writing Functor - Read Data From A File And Store In Multimap

Mar 29, 2014

How would i write a functor for this code. The code is written to read data from a file and store in a multimap.

The data has numbered lines. E.g.:
1 This is a string
2 This is a string too

So the aim is to store each word in the line with the number and then to enter a word to search for the line numbers it appears on. I do not know how to go about and write a functor

#include<iostream>
#include<sstream>
#include<fstream>
#include<cstdlib>
#include<map>
using namespace std;
int main() {
multimap<int, string>myMap;

[Code] ....

View 4 Replies View Related

Visual C++ :: Use For Loop To Read Certain Data From Txt File And Print To Console

Sep 28, 2014

I'm new to programming and i'm trying to do a certain task. I want to use a for loop to read certain data from a txt file and print them to a console. I'm trying to read student names and their grades.

Something like
3 // 3 represents the number of students.
George 97
Sarah 70
Maya 88

The data may vary but it's always in this format.

View 14 Replies View Related

C :: Creating Linked List Of Students With Individual Data Read From A File

Aug 27, 2014

In the program I'm writing, I'm creating a linked list of students with individual data read from a file. At the moment, I have written two functions to accomplish this; one that creates a student node and fills it from a line file, and a second that calls on the first to create a linked list. It seems to work fine except for one thing; It seems that EOF is being reached first, but the function continues on anyways? Here is my code so far...

Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct student
// Declaring student structure globally.

[Code] .....

And here is what the input file would look like...

Thui Bhu, 100, 90, 80, 100, 89, 99, 88
Ariana B. Smith, 90, 90, 100, 100, 99, 100, 95
Emily Gonzales, 100, 90, 100, 70, 78, 78, 80
Jennifer L, 80, 90, 90, 100, 89, 99, 85
Maria Jones, 65, 72, 77, 68, 62, 70, 65
Bill Gates, 60, 54, 38, 62, 65, 60, 50
Escobar Morris, 83, 77, 88, 76, 79, 72, 76
Anne Latner, 80, 80, 85, 95, 90, 95, 98
John Doe, 45, 87, 88, 89, 67, 96, 79

And here is what the output is...

EOF

EOF in create_record

Thui Bhu: 100 90 80 100 89 99 88
Ariana B. Smith: 90 90 100 100 99 100 95
Emily Gonzales: 100 90 100 70 78 78 80
Jennifer L: 80 90 90 100 89 99 85
Maria Jones: 65 72 77 68 62 70 65
Bill Gates: 60 54 38 62 65 60 50
Escobar Morris: 83 77 88 76 79 72 76
Anne Latner: 80 80 85 95 90 95 98
John Doe: 45 87 88 89 67 96 79
Press any key to continue . . .

View 2 Replies View Related







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