C++ :: Reading File With Delimiter - Storing Data To Object

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


ADVERTISEMENT

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 View Related

C++ :: Reading Data From File Then Storing It To Reuse?

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

C/C++ :: Reading In A Text File With New Line Delimiter?

Feb 23, 2014

I have attached the file that I need to read into a data structure. In the example I am just printing it to the screen. This following code has worked for me before, but now isn't. I have tried in visual studios, and on unix, and neither are running it. I ultimately need it to run on unix.

Example file:

word
book
programming

Here is my function to read in the file.

ifstream file;
string line;
file.open("words.txt");
if(file.is_open()){
while(!file.eof()){
getline(file, line);
cout << line <<endl;
}
}
file.close();

View 7 Replies View Related

C++ :: Reading And Storing Newline Character Alongside Data

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

C++ :: SerialPort Communication - Reading Data From Port And Storing In One Document

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

C++ :: Reading All Data For Object

Apr 15, 2014

All i need is to create a "read" function to read all the data for a Player Object.

So Far i made a display function to display all the data for the "Player" object, but i don't know how to make a read function.

Here is my program:

#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include "player.h"

[Code] .....

View 3 Replies View Related

C :: Storing A Value In Array From Reading In From A File

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

C++ :: Reading A File And Storing It In A String

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

C++ :: Storing Words In TXT File And Reading Them?

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

C/C++ :: Reading CSV File And Storing In Variables

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

C++ :: Reading And Storing A Sentence From TXT File

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

Visual C++ :: Reading And Storing TXT File

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

C++ :: Reading File And Storing In Class Objects?

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

C++ :: Reading Integers From File And Storing Into Array?

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

C/C++ :: Reading Input From A File And Then Storing It Into Array?

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

C :: Reading Lines From A File And Storing It In Array From Typdef Struct

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

C++ :: Storing Data In A File?

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

C :: Using Fgets() To Scan A Data Before A Delimiter

Dec 21, 2014

Is it possible to fgets() the string (or word) only after a delimiter? I yes then how?

Example: Code: printer, scanner, machine

Also, how can I sscanf() a string with an indefinite number of sizes and assign it to only one variable?

Example:

Code:
str = "I Love C programming 9000";
sscanf(str, "%s %d", strvar, intvar);

View 13 Replies View Related

C :: Storing File Data In A Structure Of Format Shown

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

C++ :: Storing File Data In Array With Structure Type?

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

C++ :: Reading From A File To Object?

Oct 8, 2013

I have an input file with 6 lines of data, each formatted in the following way: X,Y,Z.

My goal is to get the file name from the user, the store each line from the input file into an object of "class sample". I need to separate each variable from the file into 3 categories for further manipulation.

My idea is to get the file name in the main program, open the file, use an object to read each line and store each encountered character (non-whitespace or symbol) into it's respective variable. Then, I can use the distance equation to determine the distance between points and keep track of what class the data is in.Am I missing a step?

I am trying to write a program for the feature reduction using the RELIEF algorithm. I want it to be as simplistic as possible as I will be utilizing it often.

View 1 Replies View Related

C :: Reading A File Line By Line And Storing It Backwards Into A List

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

C++ :: Read Binary File With Line Delimiter?

Oct 3, 2013

I want to read a binary file using as line separator "ff77" in order to parse further each line one by one with some regex since the file is big. I have a small ruby code shown below, but I'm new in C++, and I don't know how to replicate in C++ what this ruby code does.

Code:
#!/usr/bin/env ruby
BEGIN{ $/="xffx77" } # Line separator = FF77
File.open(ARGV[0],"rb") # Open in binary mode

[Code].....

View 14 Replies View Related

C++ :: Read Binary File With Line Delimiter

Nov 2, 2013

I run with debugger and appear this file referring the error to line 142 (in red):

Code:
/***
*xtoa.c - convert integers/longs to ASCII string
*
* The module has code to convert integers/longs to ASCII strings. See
*
*******************************************************************************/

#include <cruntime.h>
#include <stdlib.h>
#include <limits.h>
#include <tchar.h>
#include <internal.h>
#include <internal_securecrt.h>
#ifdef _UNICODE
#define xtox_s xtow_s

[Code] ...
In the calls stack window appears this

Code:
=>msvcr110d.dll!xtoa_s(unsigned long val, char * buf, unsigned int sizeInTChars, unsigned int radix, int is_neg) Line 142C
msvcr110d.dll!_itoa_s(int val, char * buf, unsigned int sizeInTChars, int radix) Line 176C
Get_Blocks.exe!main(int argc, char * * argv) Line 224C++
Get_Blocks.exe!__tmainCRTStartup() Line 536C
Get_Blocks.exe!mainCRTStartup() Line 377C
kernel32.dll!7695336a()Unknown

[Frames below may be incorrect and/or missing, no symbols loaded for kernel32.dll]
ntdll.dll!76f19f72()Unknown
ntdll.dll!76f19f45()Unknown

It seems could be becuase to _itoa_s(), I'm using like this:

Code:
_itoa_s(CONVDEC(i), num, 10, 10);
sub += num;

View 5 Replies View Related

C++ :: Reading Data File?

Nov 22, 2014

If i have a data,how to write the code for read this data?

View 1 Replies View Related







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