473,396 Members | 1,725 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,396 software developers and data experts.

Read data from text file into vector

8
Hello,

i have a problem where i need read dataset from text file and keep them into a vector.

e.g. dataset:

"0012 0033 00045 0044 0047"

Based on this dataset, i need to keep every 4 numbers separately into a vector.

: P
Feb 14 '07 #1
6 14529
sicarie
4,677 Expert Mod 4TB
Hello,

i have a problem where i need read dataset from text file and keep them into a vector.

e.g. dataset:

"0012 0033 00045 0044 0047"

Based on this dataset, i need to keep every 4 numbers separately into a vector.

: P
Did you have a question about that? In declaring/manipulating a vector or reading input in from a text file?
Feb 14 '07 #2
arf
8
Did you have a question about that? In declaring/manipulating a vector or reading input in from a text file?
The basic idea is to transfer the dataset from a text file into a vector. From this vector, the manipulation process will be conducted. This dataset as been inserted as "string" in the vector.e.g. "0012 0034 0045 0023 3343 0001". I do not know how to convert/divide this string into a group of characters .."0012" , " 0034" and etc so that i could keep them as a separated data in the vector. Based on my code, each vector will keep the data for the whole string. What is wrong with my code?

This is my code for your reference

Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.         SDTVECTOR Std;
  4.         SDT TpStd;                  //declared TpStd -- vector<string>
  5.         string x;
  6.  
  7.         ifstream infile;
  8.         infile.clear();
  9.         infile.open("staf.txt");
  10.  
  11.        if (!infile) 
  12.       {
  13.           cerr << "File could not be opened" << endl;
  14.           exit(1);
  15.       }   
  16.  
  17.       while (getline(infile, x, '\n'))
  18.  
  19.      {
  20.     TpStd.Em.push_back(x);
  21.     {
  22.         Std.push_back(TpStd);
  23.         TpStd.Em.erase(TpStd.Em.begin(), TpStd.Em.end());
  24.     }
  25.      }    
  26. .
  27.  
  28. .
  29. .
  30. }return;
Feb 14 '07 #3
horace1
1,510 Expert 1GB
the statement
Expand|Select|Wrap|Line Numbers
  1. while (getline(infile, x, '\n'))
  2.  
read whole lines at a time, replace it with
Expand|Select|Wrap|Line Numbers
  1. while (infile >> x)
which will read words delimited by whitespace
Feb 14 '07 #4
arf
8
Thanks..It's work. But how about, if there is a several strings and each string is assumed as a single respondent..e.g.

"0111 0233 9449 0022"
"4453 8899 3343 0003"
"4456 4453 2223 1223"

* this case, there are 3 respondents

The question is how the program will know once it reaches at the end of string.
Feb 14 '07 #5
horace1
1,510 Expert 1GB
Thanks..It's work. But how about, if there is a several strings and each string is assumed as a single respondent..e.g.

"0111 0233 9449 0022"
"4453 8899 3343 0003"
"4456 4453 2223 1223"

* this case, there are 3 respondents

The question is how the program will know once it reaches at the end of string.
Expand|Select|Wrap|Line Numbers
  1. while (infile >> x)
will read whitespace delimeted strings until end of file or error
Feb 14 '07 #6
arf
8
Expand|Select|Wrap|Line Numbers
  1. while (infile >> x)
will read whitespace delimeted strings until end of file or error

Ok.

If i would like to maintain these line of strings (because each string represent for each respondent) put all of data into the vector, where from there i could count or compare them based on these respondents. Should i need to use '\n' in order to separate these strings?

*I need to use the vector because the data is fluctuate from one string to another
Feb 14 '07 #7

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

Similar topics

3
by: John Flynn | last post by:
hi, having problems reading from and writing back to the same file. basically, i want to read lines of text from a file and reverse them and write them back to the same file.. it has to...
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...
22
by: Jason Heyes | last post by:
Does this function need to call eof after the while-loop to be correct? bool read_file(std::string name, std::string &s) { std::ifstream in(name.c_str()); if (!in.is_open()) return false; ...
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>...
24
by: Ground21 | last post by:
Hello. How could I read the whole text file line after line from the end of file? (I want to ~copy~ file) I want to copy file in this way: file 1: a b
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 *...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.