473,379 Members | 1,253 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

how to read a file and store it in a vector?

hey i have a data.csv file which has something like this...

Label,Count,Type,Time



the values for each count and type and time is some numbers rite and is separated by commas in the text file as seen.
wat i want to do is the like say for example......
i want to take all the values of say "type" and store it maybe in a vector so that i can use it for use. I was looking at some split function as well as struct or sumtin but i not sure how exactly to do it.Also it have more than one line in the text file so i have to store all the lines of say ''count''

can anyone help mee
Oct 12 '06 #1
3 3456
Banfa
9,065 Expert Mod 8TB
I would say that you want a structure that represents the data on a single line and then a vector of those structures to store the data for the file.

To read the file you will need to look up the following functions

fopen - opens the file
fclose - closes the file

fgets - reads a line from the file
feof - tests for end of file

To split a line from the file into it's constituent parts to store in the structure you can either use pointers and do it manually or use strtok or alternitively load it into a string (C++ class) and use the functions of the class to work out where the constituent parts start and end.
Oct 12 '06 #2
hey thannks well i know how to open the file and read from the file.

example of wat the file.csv contains is this:

GA,0,2226444650,5,16/9/6,22:00:40
GA,1,76804458,5,16/9/6,22:00:44
GA,2,345317295,5,16/9/6,22:00:47


which represents
Label,Count,ID,Type,Date,Time
separated by commas

now i need to store the count in a vector(that is the 1 2 and 3) for future use that is between the 1st and 2nd comma. It must run thru each line(here we see we have 3 lines).

this is wat i really want to do.
Oct 13 '06 #3
Banfa
9,065 Expert Mod 8TB
having read the line into a variable use

char * strchr ( const char * string, int c );

to find the first comma

move the returned pointer on 1 (to skip the comma) and use either

int atoi ( const char * string );

or

long strtol ( const char * string, char** endptr, int radix );

to convert the following data to an integer

store that integer in a vector using member function vector::push_back(...)

vector<int> intvec;
int value = 5;

intvec.push_back(value);
Oct 13 '06 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: forums_mp | last post by:
I've got an STL class (see below) with two functions to store and retrieve data - msg structs. The "Store" function when called will copy the received message (depending on which message) into...
4
by: pisscot | last post by:
Lind Apr 13, 6:32 am show options Newsgroups: comp.lang.c From: piss...@gmail.com (Lind) - Find messages by this author Date: 13 Apr 2005 06:32:15 -0700 Local: Wed,Apr 13 2005 6:32 am...
9
by: sshock | last post by:
Hi all, I want to read from a file into a vector<unsigned char>. Right now my code looks like this: FILE* f = fopen( "datafile", "rb" ); enum { SIZE = 100 }; vector<unsigned char>...
4
by: aarthi28 | last post by:
Hi, I am trying to read every other string from a text file and store it in a vector. I can use istream_iterator to read all strings and store it in a vector. However, I only need the first,...
2
by: RyanS09 | last post by:
Hi- I have read many posts with specific applications of reading in text files into arrays, however I have not been able to successfully modify any for my application. I want to take a text file...
6
by: arnuld | last post by:
This works fine, I welcome any views/advices/coding-practices :) /* C++ Primer - 4/e * * Exercise 8.9 * STATEMENT: * write a program to store each line from a file into a *...
1
by: gdarian216 | last post by:
Okay I am writting a program that will take input from a file and store it in a string. Then the string is separated at the whitespaces and these separate strings are loaded in a vector. My problem...
9
by: Matrixinline | last post by:
Hi All, Here is the problem char* memblock; std::ifstream file(sFileName, ios::in|ios::binary); if (file.is_open()) { size = file.tellg();
1
by: csgirlie | last post by:
I'm trying to store or arrange three sets of two-dimensional data into three 2xN matrices that are stored as NumPy arrays. import os # for file handling functions import numpy as...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.