C++ :: Reading In And Initializing Data From File
Oct 30, 2014
Im familiar with fstream and know how to read in data, given that there is one type per line.
But I have a file that has multiple lines each containing a string (an address) and two double values (latitude and longitude).
Looking for support with the logic part of reading in data and initializing them to member variable.
View 1 Replies
ADVERTISEMENT
Nov 22, 2014
If i have a data,how to write the code for read this data?
View 1 Replies
View Related
Apr 11, 2014
What I need to know is how I can pass an argument to the Book constructor so I can change the const data member Category (with cascading capacity if possible. I also posted some of my set functions for further comprehension.
class Book
{
friend void CompPrice(Book &,Book&); //friend function that has access to the member functions of this class
//The arguments sent to it are by address, and of type the class Book, so that it can have access to its member functions
private: //private data members
[Code].....
View 2 Replies
View Related
Apr 29, 2013
I'm having issues reading to a 2d array from a file. When I try to read the data from my file into my matrix variable it simply doesn't read anything and leaves the variable unmodified. I've tried just reading the first piece of data in the main function and it doesn't work there either. I'm really perplexed at this point since I've never had an issue reading from a file before. Here's my relevant code:
Code:
int main(int argc, char** argv) {
double matrix[MINSIZE][MINSIZE]={0}, vectors[MAXSIZE][MINSIZE], ans[MAXSIZE][MINSIZE];
FILE * inFile;
inFile = fopen(FILENAME,"r");
if(inFile==NULL){
printf("File does not exist.
[Code] .....
View 4 Replies
View Related
Feb 21, 2013
I am working on a small c++ project where i read from a csv file with information in this format:
string,string,int
string,string,int
string,string,int
And all of the above information is one person. As is "John,Peter,23" is first name, last name, age. When there are multiple people in a csv file how can i parse through and separate the information in a string and int for later use?
View 4 Replies
View Related
Feb 18, 2014
I can't get my program to read the rest of my variables from my data file it will only read the first two and my end of file won't work it keeps continuing on.
879.46
C 400.00
D 100.0
F 525.00
C 450.00
D 500.00
D 1000.00
C 2000.00
D 3000.00
D 3500.00
C 5500.00
C 500.00
B 200.00
C -235.00
D 250.00
H -500.00
D 500.00
E
That's my data it will only read the inital number and C and D but nothing else.
// develop algorithm to balance checking account make transactions
#include<iostream> // required for keyboard and screen I/O
#include<fstream> // required for external file stream
#include<iomanip>
#include<conio.h>
#include<string>
[Code] ......
View 5 Replies
View Related
Jan 19, 2015
I have been experimenting with a program that loads WAV file and tries to read 1s and 0s from audio file (binary coded information). So far I got working code, but the problem is that I'm not getting correct data. I do get somewhat similar data. The problem is that I get data that amplifies low amplitudes, so instead some noise and binary data I get lots of noise and hard to recognize binary data. My code is this
#include <stdio.h>
#include <iostream>
#include <math.h>
using namespace std;
// An unsigned char can store 1 Bytes (8bits) of data (0-255)
[Code] .....
This is wav file in binary viewer (after header)
0030 0032
00300035003200320035003A0048004A
004C0049004A00510049004300360038
and translated to decimal
48 50
4853505053587274
7673748173675456
and this is output from "wavosaur" program (and this one is correct)
0.001465
0.001526
0.001465
0.001617
0.001526
0.001526
0.001617
0.001770
0.002197
0.002258
0.002319
0.002228
0.002258
0.002472
0.002228
0.002045
0.001648
0.001709
You can see it's not the same. I do get the same data like I do from binary viewer, but I don't understand how they are translated to amplitude value. ? Where is the catch?
So this is not really question about c++, but about wav file structure and reading data.
View 2 Replies
View Related
Feb 25, 2013
I wanna read data from txt file with space divided each value like
1.00518 2.01903 3.01139 4.01343 5.02751 5.99913 7.00011 7.99851 8.99506 9.98015 10.9901 11.992 12.9923 13.9932 14.996 16.0034 17.012 18.0255 19.0366 20.0485 21.0505 22.0664 23.0455 24.0383 25.0374 26.0439 27.0378 28.0376 29.0576 30.066
I know the size of the data is 2*500.
In matlab I can use like
fid = fopen('example.txt');
data = fscanf(fid,'%f,',1499500);
how should I write in C++?
View 3 Replies
View Related
Jan 16, 2013
I've got a problem with reading my file data but before I explain it I'll give some background. I'm programming a 2D Platformer using the SDL library for rendering.
My problem is that when reading in the data from the file it ignores all the data and just skips to the end of the function, I've attempted to debug it and the load function is getting called, the file is in the right directory and is being opened.
Here is my code:
Level.h
#include <vector>
#include <map>
#include <string>
#include <iostream>
#include <fstream>
#include "gameobject.h"
#include "miscobject.h"
struct LevelData {
// Multimap Prototype
[Code] ....
I'm not sure if it's the way I'm using multimaps. I should have mentioned that I put a break point in the multimap loops where it writes and inserts the objects and it doesn't seem to be hitting the functions, this is why I think there's something wrong with the multimaps.
View 3 Replies
View Related
May 5, 2014
read some information from a text file. The program I'm working on is like a simple betting program.
What I need to read are:
match_code - team1 - team2 - odd1 - odd0 - odd2
139 Atletico Madrid - Real Madrid 2.853.40 2.35
But the spaces between datas are not known. We only know that both team names may contain more than one word and there is one space, exactly one dash and one more space (" – ") between team names.
Also match_code is an int and odds are double values.
while(getline(input, line)) {
istringstream ss(line);
ss >> match_code >> team1;
string temp;
while(ss >> temp) {
[Code] .....
In that missing part, I need to get the second word of team name if there is one; and the three odds that are odd1 odd0 and odd2.
View 2 Replies
View Related
Feb 10, 2015
Each line of the text file has first name, last name, and a salary. Something along the lines of this:
john doe 4000
bob miller 9000
I want my program to take the first name, last name, and salary of each line and assign them to strings. I have tried this so far:
while (inFile){
inFile >> firstName;
inFile >> lastName;
inFile >> grossPay;
cout << firstName << " " << lastName << " " << grossPay << endl;
}
When it outputs the names and the salary, the last line of the text file gets output twice by the program.
View 1 Replies
View Related
Sep 26, 2013
I need debugging the code provided below, which reads data from a file and puts it into several vectors.
Something very strange happens: if I try to read the file up to the last line, I get an runtime error, while if I try to read the file up to the second last line, I do not get the error.
However, if I try to read the file up to the last line, it READS IT CORRECTLY, as you can see that it prints out the right values.
Following there is the code and the file that I am trying to read.
Code:
#include<stdio.h>
#include<iostream>
#include<fstream>
#include<vector>
#include<set>
#include<queue>
#include<cmath>
int main() {
std::ifstream fp;
[Code] ....
Following, there is the file "link1.dat" that I am trying to open.
10
1 1 8 2.171563e-06 1.837725e-02 3.939158e-05
2 3 17 1.067390e-05 3.612572e-02 1.047469e-04
3 2 9 1.166078e-05 3.280536e-02 3.207107e-05
4 4 26 1.236551e-05 3.143551e-02 2.054519e-04
5 7 18 3.430566e-05 2.821557e-02 1.109601e-04
6 12 18 9.582478e-06 2.847688e-02 4.161835e-04
7 12 43 5.816782e-06 4.352799e-02 4.141623e-04
8 12 16 2.036719e-05 4.194844e-02 2.629832e-04
9 12 11 1.829523e-05 3.096140e-02 4.543953e-04
10 11 47 2.563568e-05 4.001301e-02 3.140850e-04
View 5 Replies
View Related
Feb 15, 2014
I'm not sure why my program isn't reading data from the dat file. I'm not sure if it's because I messed up my pointers, or I'm missing something. I know we usually use Infile >> blah >> blah >> blah; to get the data from the file, but my instructor showed an example that had the struct read it for you in this format and I could just call it with pointers.
#include <iostream>
#include <iomanip>
#include <fstream>
#include <assert.h>
using namespace std;
using namespace System;
typedef int Integer;
[Code] ....
View 12 Replies
View Related
May 28, 2013
I am getting junk value in file,if i write a integers to file.can body tell me what's the prob.
int main () {
FILE* fp = NULL;
UINT32 data[4];
UINT32 i = 0;
UINT32 res[MAX_SIZE];
UZN_STATUS status = UZN_SUCCESS;
UINT32 count = 0;
/*Creating file called x*/
[Code] .....
View 2 Replies
View Related
Nov 22, 2012
I have a multivalued function (created in some other software Mathematica) that looks like f[8.7,5.4]=4.8+8.7*I. In other words it's a complex valued function of two variables. I store the output of this function over a range of parameters in a text file that looks like:
Code:
f[1,2]=9.8+8.7I;
f[4,5]=5.6-5.21I;
.
.
I could store it differently if that is easier, but essentially I just want some c++ code to read this in and store it, either in a vector type structure or Dictionary? (I don't think a double array will work as I don't know how long list of data is in advance).
View 2 Replies
View Related
Jul 7, 2012
File Data format
A,A
A,B
C,E
C,F
And so on ... it could be up to any alphabet. The number of As and Bs and Cs and Ds in first column depends on the number of unique alphabets used in the first column. For example the unique alphabets used in this case are 2 (A,C) hence each alphabet appears in the first column of consecutive 2 rows.
Which means
Code:
A:A,B,C,D
B:B,C,D,E
C:C,D,E,F
D:D, ,F,G (E is missing)
Actual Data
A,1
A,2
A,3
A,4
C,18
C,33
Final Solution: Add each integer value to its previous value and store the resulting value.
View 8 Replies
View Related
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
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
Jul 26, 2014
I have a csv file (comma delimited text file really) and I have data ,about 6400 lines, and I need to read and display data from 1 line. At the bottom is sample data from my csv file.
I want to read onto the screen the 3rd item .. ie Test User 1 and the date and time at the end (the last 2 items).
I will enter in a number .. from which I will want to retrieve the appropriate record .. in this case I would enter in 514, 563 or 874 and get back the info for that record.
This is a DOS 16 bit application .. and I know that some of the data is in memory already but I am not sure how to tell .. or how to retrieve it.
I do have the ability to save the data to the file at any time .. so I can use that to ensure that the data is current in the text file before I retrieve the data from this 1 row.
I think that it may just be easier to get from the text file ? but I am not sure.
0000,7,0,"Test User 1",53c145cb,db,5,514,514,2014/07/12,10:27:23
0001,7,0,"Test User 2",53d7f,1903,10,563,563,2014/07/24,11:04:31
0002,7,0,"Test User 3",529c,5ffb03,0,874,874,2013/12/02,09:07:58
I am a database guy .. and I am just a bit lost when it comes to C programming as such.
View 9 Replies
View Related
Nov 2, 2014
I'm reading the following file:
Osgood,Marcus 298542123 CHM FR mosgood@whatever.edu
Cronk,Melissa 873489021 BIO SR mcronk@whatever.edu
Pry,Seth 349908431 MTH SO spry@whatever.edu
Langlais,Susan 783323545 ME SR slanglais@whatever.edu
Davis,Nicole 987543345 PHY FR ndavis@whatever.edu
It's supposed to split it up into name, ID number, major, year, and email. The file reads it without any errors, and assigns name to the first part of the structure. However, ID gets assigned the ID, major, year, and email. Then Major gets assigned major, year, and email. Year gets assigned year and email, while email just gets assigned email. I don't know if it has something to do with the loop. For example, this is what I get what I print just the name and the ID.
Cronk,Melissa 873489021BIOSRmcronk@whatever.edu
Pry,Seth 349908431MTHSOspry@whatever.edu
Langlais,Susan 783323545ME
Davis,Nicole 987543345 PHYFRndavis@whatever.edu
Anyway. This is my function code for reading the array. I have it printing the ID number just to see if I can catch the errors earlier:
Code:
#include <stdio.h>
#include <string.h>
#include "functions.h"
void read_data(char filename[], studentinfo_t s[], int *size) {
FILE *infilep;
int countstudents = 0;
infilep = fopen("student_data", "r");
[Code].....
the name is reading correctly, but not anything else!
View 1 Replies
View Related
Jun 1, 2014
I've been trying to write my homework assignment (a list of countries, their codes and their capitals) and I've done most of it but I'm stuck at this: I have to open a file, read it and if there are data, add them to the list. So far I've created an element of the structure, queue list, printed the list on the screen and freed the memory. I thought that for reading the file and adding the data I could first open the file (of course) with fopen and after that use a for loop (i=0;i=!EOF;i++) to scan the whole file and fscanf(fp,"%s",result->country),etc in the loop to add the data to the structure of the element and finally insert that element to the queue list. However, when I try to do these operations, I only get to writing the name of the file and the program crashes.
Code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct List {
char country[20];
char code[5];
char capital[20];
struct List*next;
[Code] .....
View 1 Replies
View Related
Mar 6, 2013
I am having problems either writing data to a binary file or reading from the file. Through the process of elimination I am posting the code where the data is written to file to see if I can eliminate that as an option. I know the data is being processed correctly because, through the use of another function, I can view the data.
I also know that fwrite must be including some padding because the file size ends up being 576 bytes after it is written instead of 540 bytes (the size it would be if no padding is used). Here is my struct:
Code:
typedef struct {
char teams[25];
float wins;
float losses;
float pct;
int runsScored;
int runsAgainst;
} STATISTICS;
Here is where I initialize it:
Code:
STATISTICS stats[12] = {
{"Anaheim Arrays", 0.0, 0.0, .000, 0, 0},
{"Pittsburg Pointers", 0.0, 0.0, .000, 0, 0},
{"Indianapolis Integers", 0.0, 0.0, .000, 0, 0},
[Code] ....
And here is the function that writes my data. The sA array is only used to change the scheduled games based on the variable week.
Code:
void schedule(STATISTICS stats[]) {
FILE *f;
int sA[12], week = 0, runsPerGameA = 0, runsPerGameB = 0, runsAgainstA = 0, runsAgainstB = 0;
int index, a = 0, b = 1, i = 0;
[Code] .....
View 13 Replies
View Related
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 5 Replies
View Related
Mar 27, 2013
Im tasked with reading a data file, this is an example snippet
list of trophy winners
year facup leaguecup 1stdiv 2ndiv
1960/61 Tottenham Hotspur Aston Villa Tottenham Hotspur Ipswich Town
1961/62 Tottenham Hotspur Norwich City Ipswich Town Liverpool
1962/63 Manchester Utd Birmingham City Everton Stoke City
The file starts in 1892 and is up to 2011/12, there is data missing for some years due to the wars etc,
once ive read the file, i need to store the data, for me to re-use.
There are a lot of useful link regarding reading data in, but they tend to be with very small files with say 10 lines of numbers.
View 1 Replies
View Related
Jan 5, 2015
Code to write data(Double type e.g 12345.67891) in text file like pattern given below. Remember to put tab between each column.
-----------------------------------------------------
Column1 Column2 Column3
Value 1 Value 2 Value 3
Value 4 Value 5 Value 6
Value 7 Value 8 Value 9
----------------------------------------------------
& Also how to read data from this file.
View 3 Replies
View Related
Mar 26, 2015
I have a data file that looks like:
111111111
Lisa
Porter
3
ENEE 114
CMSC 412
ENME 515
333333333
Alex
Simpson
1
CMSC 412
***
In the form student ID and then first name last name number of courses and finally the course code(s). Im currently attempting to read these into linked lists with the following code:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct CourseInfo {
int courseID;
char courseName[30];
[Code] ......
I get no errors or warnings however when i print this out i get:
111111111
Lisa
Lisa
Porter
3
ENEE 114
CMSC 412
ENME 515
And then the program crashes. So i know that for some reason the problem is the
fgets(rootPtr->FirstName, 22, p);
But I do not know why because it seems to work for the other fgets. I also want to know why the while loop never runs and it crashes instead.
View 14 Replies
View Related