473,698 Members | 2,737 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Read data from text file into vector

8 New Member
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 14547
sicarie
4,677 Recognized Expert Moderator Specialist
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 New Member
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 Recognized Expert Top Contributor
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 New Member
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 Recognized Expert Top Contributor
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 New Member
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
2662
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 replace the text its reversing eg.
4
1858
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 Subject: how to read it out using c++ Reply | Reply to Author | Forward | Print | Individual Message | Show original | Remove | Report Abuse
22
13297
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; char c; std::string str;
9
6815
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> buf(SIZE); fread(&buf, 1, SIZE, f);
24
3208
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
5513
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 filled with a tab delimited list of 10 columns (floats) and read it into a 2D array. The length of the columns are all the same, however this will be variable from text file to text file. Any help (starter code or where to read) would be much...
6
5710
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 * vector<string>. Now, use istringstream to read read each line * from the vector a word at a time.
9
3457
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
4962
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 np # for array/matrix processing import matplotlib.pyplot as plt # for general plotting from mpl_toolkits.mplot3d import axes3d # for 3d plotting wordList= numFiles = 0 numDataSets = numVectors = sizeVector = 0
0
8683
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8610
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9170
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8873
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5862
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4372
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4623
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3052
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2339
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.