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

array question

I have an input file like test.txt which has 15 sentences in it on
different lines. Can I get 1 full line into 1 element of an array? Is
that possible? I try to do it and it will cut out at the first trailing
whitespace.

Apr 28 '06 #1
14 1733
foker wrote:
I have an input file like test.txt which has 15 sentences in it on
different lines. Can I get 1 full line into 1 element of an array? Is
that possible? I try to do it and it will cut out at the first trailing
whitespace.


Don't use arrays. They're evil
(http://www.parashift.com/c++-faq-lit...html#faq-34.1). Use a
std::vector of std::strings instead, and use std::getline to read. It
won't strip whitespace. See this post for an example:

http://groups.google.com/group/comp....9afd6f122340c8

Cheers! --M

Apr 28 '06 #2
foker wrote:
I have an input file like test.txt which has 15 sentences in it on
different lines. Can I get 1 full line into 1 element of an array? Is
that possible? I try to do it and it will cut out at the first
trailing whitespace.


How can we diagnose code that you keep hidden from us?

Brian
--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.
Apr 28 '06 #3
> How can we diagnose code that you keep hidden from us?

I have this
string get_sentence;
for(int i = 0; i < 15; i++)
{
fin >> get_sentence[i];
cout << get_sentence[i];
}

The problem with this is like I said above it only takes in the first
word into the first element of the array. I want to be able to put the
whole sentence into one element in the array. Not put 1 string in one
element or 1 char in one element.

Apr 28 '06 #4
u will do better with a vector
This is a string dunno wht happens when u index into a string.
with C -Style strings u get chars

-SIGTERM
amit

Apr 28 '06 #5
foker wrote:
How can we diagnose code that you keep hidden from us?
I have this
string get_sentence;
for(int i = 0; i < 15; i++)
{
fin >> get_sentence[i];
cout << get_sentence[i];
}

The problem with this is like I said above it only takes in the first
word into the first element of the array.


That's what << is designed to do. Your text should explain that.
I want to be able to put the
whole sentence into one element in the array.


What you need is getline().

As said elsewhere, you'd be better off with a std::vector of
std::string. Use the stand-alone version of getline():

std::string s;
std::vector v;

getline(fin, s); // fin is your file stream

v.push_back(s); // inserts a copy of the string
If you feel that you MUST use arrays, then the member version of
getline will work:

char s[256];

fin.getline(s,256);

Brian
Apr 28 '06 #6
Amit Limaye wrote:
u will do better with a vector
You will do better without shorthand like "u" and by quoting the
previous message. See the information in my .sig.
This is a string dunno wht happens when u index into a string.
with C -Style strings u get chars


I suggest a decent book on C++. This is pretty fundamental.
Brian
--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.
Apr 28 '06 #7
I V
On Fri, 28 Apr 2006 13:21:32 -0700, foker wrote:
How can we diagnose code that you keep hidden from us?
I have this
string get_sentence;


This is a single string, not an array. Do you mean:

string get_sentence[15]

It's best to copy-and-paste (a short section of) your code into posts, so
that people can identify exactly what the problem is.
for(int i = 0; i < 15; i++)
Are you sure you'll always have exactly 15 sentences? It's better not to
hardcode this in - instead, use a vector which can resize to fit as many
sentences as you need.
{
fin >> get_sentence[i];
With the code you have here, that will input single character (and
produce undefined behavior, because you are trying to write to a
zero-length string). If you have an array of strings, it will (as you
report) input a string up to the first whitespace character. To read a
whole line into a string, use the getline function.
cout << get_sentence[i];
}


Try:

std::vector<std::string> sentences;
std::string one_sentence;

while( std::getline(fin, one_sentence) )
{
sentences.push_back(one_sentence);
std::cout << sentences.back() << std::endl;
}

Apr 28 '06 #8
Default User wrote:
foker wrote:
I want to be able to put the
whole sentence into one element in the array.
What you need is getline().

As said elsewhere, you'd be better off with a std::vector of
std::string. Use the stand-alone version of getline():

std::string s;
std::vector v;


This should be

std::vector<std::string> v;

I personally prefer to typedef container types since I often work with
iterators:

typedef std::vector<std::string> string_list;
string_list v;
getline(fin, s); // fin is your file stream
v.push_back(s); // inserts a copy of the string

Jonathan

Apr 28 '06 #9
Jonathan Mcdougall wrote:
Default User wrote:
foker wrote:
I want to be able to put the
whole sentence into one element in the array.


What you need is getline().

As said elsewhere, you'd be better off with a std::vector of
std::string. Use the stand-alone version of getline():

std::string s;
std::vector v;


This should be

std::vector<std::string> v;


Good pick, I was in a hurry.


Brian
Apr 28 '06 #10
< C++ Primer 3rd Edition> Chapter 6, Section 7
Lippman has told you how to

Apr 29 '06 #11
Jonathan Mcdougall wrote:
I personally prefer to typedef container types since I often work with
iterators:

typedef std::vector<std::string> string_list;


However, I wouldn't use the name 'string_list' for a vector of strings.
That's kind of misleading. When I read the name, my first expectation would
be that it's an std::list<std::string>.

Apr 29 '06 #12
zw********@163.com wrote:
< C++ Primer 3rd Edition> Chapter 6, Section 7
Lippman has told you how to


This sentence makes no sense. Context would probably help. See below.

Brian

--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.
Apr 29 '06 #13
Rolf Magnus wrote:
Jonathan Mcdougall wrote:
I personally prefer to typedef container types since I often work with
iterators:

typedef std::vector<std::string> string_list;


However, I wouldn't use the name 'string_list' for a vector of strings.
That's kind of misleading. When I read the name, my first expectation would
be that it's an std::list<std::string>.


I use "list" in a generic way, as a "serie" of things. This allows me
to change the underlying representation of the "list" transparently (as
long as the operations are still valid, of course). Often the
implementation doesn't matter to the user (sometimes me), it is only
what I call an optimization. Matter of taste, perhaps.
Jonathan

Apr 29 '06 #14
In message <11*********************@e56g2000cwe.googlegroups. com>,
Jonathan Mcdougall <jo***************@gmail.com> writes
Rolf Magnus wrote:
Jonathan Mcdougall wrote:
> I personally prefer to typedef container types since I often work with
> iterators:
>
> typedef std::vector<std::string> string_list;
However, I wouldn't use the name 'string_list' for a vector of strings.
That's kind of misleading. When I read the name, my first expectation would
be that it's an std::list<std::string>.


I use "list" in a generic way, as a "serie" of things.


So call it "series", or better "sequence", which is the real underlying
Concept.
This allows me
to change the underlying representation of the "list" transparently (as
long as the operations are still valid, of course). Often the
implementation doesn't matter to the user (sometimes me), it is only
what I call an optimization. Matter of taste, perhaps.


--
Richard Herring
May 8 '06 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: SilverWolf | last post by:
I need some help with sorting and shuffling array of strings. I can't seem to get qsort working, and I don't even know how to start to shuffle the array. Here is what I have for now: #include...
9
by: buda | last post by:
Hi, I've been wondering for a while now (and always forgot to ask :) what is the exact quote from the Standard that forbids the use of (&array) (when x >= number_of_columns) as stated in the FAQ...
3
by: Pol Bawin | last post by:
Hi All, One : I have a property that get/set a array of an abstract class A By default my array is null In the propertygrid, It is not works correctly when my array is null. (when my array...
11
by: Geoff Cox | last post by:
Hello, I am trying to get a grip on where to place the initialization of two arrays in the code below which was created using Visual C++ 2005 Express Beta 2... private: static array<String^>^...
28
by: anonymous | last post by:
I have couple of questions related to array addresses. As they belong to the same block, I am putting them here in one single post. I hope nobody minds: char array; int address; Questions...
104
by: Leszek | last post by:
Hi. Is it possible in javascript to operate on an array without knowing how mamy elements it has? What i want to do is sending an array to a script, and this script should add all values from...
51
by: Pedro Graca | last post by:
I run into a strange warning (for me) today (I was trying to improve the score of the UVA #10018 Programming Challenge). $ gcc -W -Wall -std=c89 -pedantic -O2 10018-clc.c -o 10018-clc...
7
by: heddy | last post by:
I have an array of objects. When I use Array.Resize<T>(ref Object,int Newsize); and the newsize is smaller then what the array was previously, are the resources allocated to the objects that are...
8
by: T. Wintershoven | last post by:
Hello all, I have a form with some checkboxes. The names of these checkboxes come from an array. When i click the submit button the resultcode doesn't recognize the names when i want to check...
4
by: mab464 | last post by:
I have this code on my WAMP server running on my XP machine if ( isset( $_POST ) ) { for($i=0; $i<count($_POST);$i++) { if ($ans != NULL ) $ans .= ", " . $_POST ; // Not the first...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
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: 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...

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.