473,398 Members | 2,113 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,398 software developers and data experts.

reading from a textfile into an array

i need to write a program in c++ that is going to read English sentences from a textfile, a line at a time and store them in an array. the file has 100 sentences each occupying a single line.
Oct 23 '06 #1
5 2691
arne
315 Expert 100+
i need to write a program in c++ that is going to read English sentences from a textfile, a line at a time and store them in an array. the file has 100 sentences each occupying a single line.
And what is it you have difficulties with?
Oct 23 '06 #2
And what is it you have difficulties with?
having problems with array declaration. is it suppossed to be a one-dim array or a two-dim array?
i have managed to have the program read from a textfile but it is not storing into an array. displaying the senteces has to be from an array not from the textfile as it is doing.
Oct 24 '06 #3
arne
315 Expert 100+
having problems with array declaration. is it suppossed to be a one-dim array or a two-dim array?
i have managed to have the program read from a textfile but it is not storing into an array. displaying the senteces has to be from an array not from the textfile as it is doing.
One dimensional: it could be an array of strings like this
Expand|Select|Wrap|Line Numbers
  1. vector<strings> sentences; 
  2.  
Oct 24 '06 #4
One dimensional: it could be an array of strings like this
Expand|Select|Wrap|Line Numbers
  1. vector<strings> sentences; 
  2.  

i don't even understand this line of code that u've sent. can u elaborate. what is a vector anyway?
Oct 27 '06 #5
arne
315 Expert 100+
i don't even understand this line of code that u've sent. can u elaborate. what is a vector anyway?
A vector is a container class from the C++ Standard Template library (STL). All you need to know for practical use, however, is that a vector can be used to store a sequence of values of a certain type, that it grows automatically if you need more space and that it grants efficient access. It's similar to a simple array, but somewhat more comfortable.

For example,
Expand|Select|Wrap|Line Numbers
  1. vector<string> vec_of_str;
  2. vector<int> vec_of_int;
  3. vector<double> vec_of_dbl;
  4.  
define vectors that hold elements of type string, int and double, respectively.
BTW, string is also a type from the STL and no built-in type like int or float.
Please refer also to http://www.cppreference.com to get an idea what things you can do with vectors and strings.

Here comes the code that may do what you need. Have a look and try to understand it (I added some comments :))

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <vector>
  5.  
  6. using namespace std;
  7.  
  8. int main( void ) 
  9. {
  10.     // define an infile stream
  11.     ifstream fin( "textfile.txt" );
  12.  
  13.     // define a vector of strings
  14.     vector<string> sentences;
  15.  
  16.     //define a temporary string
  17.     string tmp_str;
  18.  
  19.     // read the file line by line into tmp_str
  20.     while( getline( fin, tmp_str ) ) {
  21.  
  22.         // append the tmp_str as a new last 
  23.                 // element to sentences 
  24.         sentences.push_back( tmp_str );
  25.     }
  26.  
  27.     // print out the 3rd element of our vector
  28.     cout << sentences[2];
  29.  
  30.     return 0;
  31. }
  32.  
Oct 27 '06 #6

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

Similar topics

2
by: Anders Eriksson | last post by:
Hello! I'm a beginner at PHP and I wonder how I would go about to read a textfile into an array(hash). The textfile looks like this: A/S=age/sex? A/S/L=age/sex/location? AA=alcoholics...
7
by: Hans A | last post by:
I have a textfile "textfile.txt" containing a list of words. There is one word on each line. I want to pick two random lines from this textfile, and I have tried to do something like: //Loading...
2
by: Suchi | last post by:
Hi all: I want to read a textfile from an ASP program. My program is like this: Set fso = CreateObject("Scripting.FileSystemObject") workFile=http://localhost/Readme.txt) Set textFile =...
3
by: Markus Hofmann | last post by:
Hi @ all hope someone can help me as my PC is ready to go through the window...here it is: I have a text file with approximately 150,000 lines of the following format: ...
2
by: chris | last post by:
Hi there, I am reading in a textfile which looks like this (there is no new line after the last number) 03 98661881 0407 566453 The code to load the textfile looks like this:
2
by: novacreatura | last post by:
Hi, I have a project that's supposed to create a program for a "Dating Service". The first part of the program is to read a textfile of profiles which include names, age, etc...into a string...
2
by: Rain | last post by:
I have a textfile with multiple line. How do i get (for example) the 1st line or the 3rd line then change the line to something i want in C#? How do i delete a line in C#... or backspace in a...
1
by: Justin Fancy | last post by:
Hi everyone, I have a textfile which I need to read and compare dates. The text file summarizes every time I do an update to an internet site. Sample output is as follows: Copying...
16
by: Ron | last post by:
Hello everyone, I've created a functioning ATM program, a bank machine. Now I want to implement usernames and Pins into it. So I have a text file with this info. BILL, 1111 TOM, 2222...
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
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
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...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.