C++ :: Reading And Storing CSV File Data
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
ADVERTISEMENT
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
Dec 5, 2014
I am trying to read a file use the data line by line to create into an object. The current file I have is like this and the code reading the file will be found below.
1223 Fake1 Name1 60 70 80 24 89 add1 Male
1224 Fake2 Name2 61 70 81 80 24 add2 Male
1225 Fake3 Name3 63 70 82 80 89 add3 Male
1226 Fake4 Name4 63 70 83 80 88 add4 Male
The problem I am having is that I need to put delimiters in the file so that a person can have more than one name and also the address can now hold multiple strings until the delimiter.
I would like to change my file to this;
1223 : Fake1 Name1 : 60 : 70 : 80 : 24 :89 : This will be address1 : Male
1224 : Fake2 Name2 : 61 : 70 : 81 : 80 :24 : This will be address2 : Male
1225 : Fake3 Name3 : 63 : 70 : 82 : 80 :89 : This will be address3 : Male
1226 : Fake4 Name4 : 63 : 70 : 83 : 80 :88 : This will be address4 : Male
How can I update the code below so that it can use the delimiters to create an object?
void loadFile(Person people[], int* i) {
ifstream infile("people2.txt");
if ( !infile.is_open()) {
// The file could not be opened
cout << "Error";
[Code] .....
View 5 Replies
View Related
Jan 6, 2014
I am reading data from a text file into a program. I am well aware of the subtle distinctions in the mode of data input/entry when using the stream extraction operator, the get() function, and the getline() function.
My problem is that all of them do not read and/or store the newline character alongside the data read!
Any function that reads and stores data and the terminating newline character together??
View 1 Replies
View Related
Jun 27, 2014
I am doing SerialPort communication program where i am opening the port and reading data from port and storing in one document.
I debug my program and check that my read function is reading the data properly but when checking the document i find only unicode.
Suppose I am sending 1,2,3,4 to port i able to read same but my document showes þÿÿ þÿÿ þÿÿ þÿÿ
//My Read Function which read data from serial port
tInt serialport::Read(tVoid* pvBuffer, tInt nBufferSize) {
tInt nBytesRead = 0;
[Code] ....
Why I am getting unicode instead of 1,2,3,4 in my document ....
View 3 Replies
View Related
Sep 12, 2013
Is something like this possible and if not how can I make this work?
fscanf(in, "%d %d %d", &i, &x, &arr[i+x]);
View 4 Replies
View Related
Feb 7, 2013
I know how to do this in c++ with fstream and std::string and getline and so on and so forth. Im writing my code solely in c however. I can't get g++ installed so figured it was a good excuse to learn c instead of using the equivalent c++ abstracts.
My problem is, I'm making a game in c that I have made in c++ but have ran into an issue with my map. I want to read in my map from a file which just looks like this:
Name of Town
* * * * * * *
* * * * * * *
* * * * * * *
etc...
so i tried using fscanf to first read in the name of the town (stored in a char*) then read in the characters (in this case '*')(not including white spaces becuase i can just print those) into another char*. what is the better way to do this?
View 16 Replies
View Related
Apr 12, 2013
I would like to store the titles of a CD and then read them. I have started a program but not sure how to display or make sure it is storing it in the .txt file.
#include <iostream>
#include <fstream>
#include <cstdlib>
[Code]....
View 3 Replies
View Related
Mar 16, 2015
how to read a csv file where the data is seperated with commas so the csv file looks like:
programming,03,60,141,01,W,2015
programming,03,60,141,30,W,2015
Algebra,03,62,102,02,S,2013
Religion,08,98,938,20,F,2014
So i need the name of the course in one variable, the course code (ex 0360-141-01 for line 1) in another variable and the term (ie W2015 for line1). So i got the name done but cant figure out the course code since i need more than one value that is seperated by a comma. my code is:
#include<stdio.h>
#include<string.h>
typedef struct CourseInfo {
int courseID;
[Code].....
View 2 Replies
View Related
Dec 11, 2012
I've created a class that is supposed to store first name, last name, date of birth, date of death, and a fact about a person (all variables within the class). Im trying to fill these variables with a read function. it reads a .txt file like this
Firstname Lastname 1987 1988 this guy did this
The problem is, I don't know how to handle the last variable. the variable needs to hold the entire "this guy did this" sentence. i made it a string, just because i was clueless, and as expected, it only holds "this"
this is my .h:
#include <string>
#include <fstream>
#include <iostream>
using namespace std;
class Person {
public:
Person();
Person(const Person & person);
[Code] ....
Here is the read function in the .cpp:
bool Person::Read(ifstream & input) {
return (input >> fname >> lname >> dob >> dod >> fact);
}
View 3 Replies
View Related
Feb 12, 2014
I am trying to read and store a txt file with format below:
2.5;abc;2,4000
2.5;bef;3,2000
2.5;ref;3,1000
I try the fscanf( pFile, "%d;%s;%d,%d", buffer,buffer2,buffer3,buffer4 ); to store 2.5 to buffer, abc to buffer2, 2 to buffer 3 and 4000 to buffer 4 on first line.
However, it doesn't work. How can I achieve this? I want to read the 2nd line and so on.
Code:
char buffer[100]="";
char buffer2[100]="";
char buffer3[100]="";
char buffer4[100]="";
FILE * pFile=NULL;
pFile = fopen("C: est est.txt", "r");
[Code] ....
View 11 Replies
View Related
Aug 18, 2013
I've a text file : Random.txt which comprises of
Jade
12MS234
Male
18
Rocky
12MS324
Male
18
Marx
12MS632
Male
18
Now in my program i've a class
class stud
{
char name[10];
char reg[10];
char gender[10];
int age;
};
Now I've to write a code in c++, where i've to read the given data and store this into 3 objects of stud class(array of objects) ...then further i've to manipulate the above stored data. I think i'm getting error while storing...variables are showing random characters... give me the code.for this program..in C++
View 2 Replies
View Related
Oct 28, 2014
My main issue is that when I store just one value into a simple integer variable, it gives me a big negative number.
My goal is to store a file that reads like this into arrays:
2014 1 23
2012 2 15
2013 1 25
etc
Where I would have the first column in an array, the second in another and the third in third array since I am not allowed to use multidimensional.
View 5 Replies
View Related
Feb 16, 2015
I need to read input from a file , which contains multiple sentences of varying lengths. After each new line char, i need to store that sentence into an array.
#include <iostream>
#include <cstring>
#include <fstream>
#include <cstdlib>
[Code].....
View 2 Replies
View Related
May 4, 2013
I'm trying to read in lines for a file and storing it in an array. This is what I have so far. Am I allocating the memory correctly?
Code:
typedef struct {
/* declartion of variables */
} App
typedef struct {
[Code].....
How do I access the array content? I'm suppose to strtok the contents from the lines stored in the array and use it again.
View 2 Replies
View Related
Apr 12, 2013
I am trying to store the Title, Artist, and date published of a list of CD's. I can't seem to be able to get it to print the list or not sure if it is actually storing it. This is what i have so far.
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;
int main() {
char names[5][100];
[code]....
View 2 Replies
View Related
Aug 5, 2014
I'm having trouble reading my data from a .txt file into a structure of the format shown in my code. I've made my student database in the program below based on user input and I didn't have a problem with that, but now it's come to input from a file it's making it difficult.
My three tasks are:
(1) A table containing 1 row per student, containing the student ID number and all of the student's marks.
(2) Another table, containing 1 row per student, containing the student ID number and the average mark obtained by that student.
(3) The average mark for each subject.
The assumptions to be made are: The student ID can begin with a zero, and should therefore be read in as a string.The test data will contain 20 students but the program should be able to deal with up to 100 students.Assume there are no more than 4 different subjects.
So based on the first assumption I've arranged the data in the file in an order in which the student ID begins with a zero:
003953 Computing 38
003953 English 88
003953 Mathematics 29
003953 Physics 83
073241 Computing 63
073241 English 99
073241 Mathematics 32
073241 Physics 73
...for twenty students, 80 lines of data. Now, on the assumption they must be read in as strings, this is what's making it tricky to store in the structure because, I've got 80 ID numbers but 20 repeat themselves 4 times. Once I've got this data in the structure below the tasks I won't have a problem with because I can just base it on a user input program but the data's already stored instead.
Below is my code for user input associated with task (1). In this example the IDs are stored as ints but for the file they will be strings. It compiles fine, displays the data as shown in the assignment sheet, but I don't know how to get the data into my structure. I can store the data in a structure of three arrays using fscanf() no problem, but it's not very "workable" for what I need to do with it.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
[Code].....
View 11 Replies
View Related
Feb 20, 2015
I have a structure
struct Wine
{
string wineName;
int vintage;
int rating;
double price;
};
how can i store the file data below in an array with the structure type???
Dow Vintage Port ;2011;99;82
Mollydooker Shiraz Carnival of Love ;2012;95;75
Prats & Symington Douro Chryseia ;2011;97;55
Quinta do Vale Meão Douro ;2011;97;76
Leeuwin Chardonnay River Art Series ;2011;96;89
View 1 Replies
View Related
Sep 25, 2013
So I'm reading a file line by line and storing it backwards into a list. So if the file has has this format...
1
2
3
4
The code should store each line in a list as such...
4, 3, 2 ,1
Instead the code will store the last variable in all nodes. So the final list will look like this...
4, 4, 4, 4
Here is my code...
struct node *head = NULL;
int i;
while(read(in, &i, sizeof(int)) != 0) {
struct node *temp = malloc(sizeof(*temp));
temp->line = &i;
temp->next = head;
head = temp;
}
View 4 Replies
View Related
Nov 22, 2014
If i have a data,how to write the code for read this data?
View 1 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
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
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