7 Replies - 1399 Views - Last Post: 29 October 2011 - 12:58 PM Rate Topic: -----

#1 codingindream  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 11
  • Joined: 29-October 11

How to put strings from file in an Array [help]

Posted 29 October 2011 - 01:35 AM

HI DIC
My Question is simple i have words i need to put words in txt file then i need to put these strings in an array and the sort them using bubble sort etc but my problem is iam unable to put the words in a string array!! how i can put these words in array ?
here is the code i have done so far
Help :helpsmilie:

 
#include<iostream>
#include<string>
#include<string.h>
#include<fstream>

using namespace std;

int main()

{
    string output;

{
    ofstream myfile;

    myfile.open("words.txt");

    myfile<<"bark ,pull ,joke ,boast ,hike ,stamp ,chap ,stuff ,blend ,cry ,dream ,faint ,"
        "smash ,fly ,chase ,groan ,hide ,pretend ,hop ,"
        "float ,crawl ,melt ,paddle ,blast , ,push ,race ,jump ,"
        "roll ,row ,rub ,sail ,search ,frown ,shout cheep ,spoil ,spray ,stalk ,climb ,step ,beep,"
        "swim ,tag ,tickle ,travel,trip ,vote ,toss ,wish ,bake ,juggle ,boil ,mix ,buzz ,cackle ,chatter ,"
        "chime ,wave ,clang ,slide ,click ,broil ,crush ,dash ,frighten ,shake ,screech ,growl ,speed ,hiss ,"
        "weep ,swing ,hum ,fry ,bang ,leap ,caw ,pass ,poach ,sink ,rattle ,"
        "stumble ,roar ,roast ,rush ,scramble ,scream ,clash ,shiver ,blink ,laugh ,snap ,wrestle ,"
        "sob ,giggle ,hoot ,heat ,clap ,toast ,ring ,wail ,thump ,purr ,whisper";

myfile.close();

}

{

    ifstream myfile;

    myfile.open("words.txt");
    a
    if(myfile.is_open())

        while(!myfile.eof())

            //how to put these strings in an array??
}
system("Pause");
return 0;

}




Is This A Good Question/Topic? 0
  • +

Replies To: How to put strings from file in an Array [help]

#2 Anarion  Icon User is offline

  • The Persian Coder
  • member icon

Reputation: 282
  • View blog
  • Posts: 1,456
  • Joined: 16-May 09

Re: How to put strings from file in an Array [help]

Posted 29 October 2011 - 02:00 AM

I highly encourage you to read This.

When you want to input formatted data from a file, you should use a loop like this one:
std::ifstream ifile("somefile.format"); //the input file stream
std::string s; //the string object to hold the input data in each go of the loop
if(ifile.is_open()) {
    while(ifile>>s) { //while there is data to be read and the extraction is successful...
        //here you can operate on s, which holds the data which has just been extracted from the stream
        //for example, print it on screen:
        std::cout<<s<<std::endl;
    }
}


Hope that could help you with your problem ;)

This post has been edited by Anarion: 29 October 2011 - 02:04 AM

Was This Post Helpful? 0
  • +
  • -

#3 codingindream  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 11
  • Joined: 29-October 11

Re: How to put strings from file in an Array [help]

Posted 29 October 2011 - 04:08 AM

Well Thanks , but the thing is that the code you wrote is for input of data from file into a string right ? then we can operate on that string as output etc but i want the data into an array of strings because i want to do bubble sort on it. :dozingoff:
i want like this


// creating file 
//Entering words
//closing file
// opening again 
// then 


string str[116];

 ifstream myfile;

    myfile.open("words.txt");
    a
    if(myfile.is_open())

        while(!myfile.eof())

         // here i want the words to be in string array something like this

getline(cin,str[116]) //input of words in string array(i know code is wrong) just a concept



   //how to put these strings in an array??  :dontgetit:/>

[quote name='codingindream' date='29 October 2011 - 04:04 AM' timestamp='1319886296' post='1472878']
Well Thanks , but the thing is that the code you wrote is for input of data from file into a string right ? then we can operate on that string as output etc but i want the data into an array of strings because i want to do bubble sort on it.  :dozingoff:/> 
i want like this 

[code]

// creating file 
//Entering words
//closing file
// opening again 
// then 


string str[116];

 ifstream myfile;

    myfile.open("words.txt");
    a
    if(myfile.is_open())

        while(!myfile.eof())

         // here i want the words to be in string array something like this

getline(cin,str[116]) //input of words in string array(i know code is wrong) just a concept



   //how to put these strings in an array??  :dontgetit:/>


Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is offline

  • member icon


Reputation: 3993
  • View blog
  • Posts: 12,321
  • Joined: 25-December 09

Re: How to put strings from file in an Array [help]

Posted 29 October 2011 - 06:50 AM

It looks like you need to study up on arrays.

Jim
Was This Post Helpful? 0
  • +
  • -

#5 deefat  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 5
  • Joined: 24-October 11

Re: How to put strings from file in an Array [help]

Posted 29 October 2011 - 10:29 AM

I don't get what u want, if u need to put single string into an array, that's very easy. Define your array's size first! If u dont know how many line u will take, just read till end of file, count "\n" in ur file. Or u can do this: declare max size (ex: string arrSTR[100]) xD
int i = 0; // first of array = 0;
while (!ur_file.eof()){
getline(cin, line);
array[i] = line;
i++;
}



But if you want every single word in the line become items in an array...
I think, first, you should read line by line, and then, split the line into tokens THIS IS EXAMPLE. Your array will become a 2D array...
+ Count how many word in that line then define size of array
+ Instead of write token in new line, you put it into slots of array

p/s: sorry for my terrible English. I am vietnamese.
Err.. 1 question, what is "string output" for?
Was This Post Helpful? 0
  • +
  • -

#6 bloodzdevil  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 56
  • Joined: 12-June 10

Re: How to put strings from file in an Array [help]

Posted 29 October 2011 - 10:58 AM

I might be wrong, but you could use the read function to read the bytes directly into the array.
A better approach would be if you create a nested loop and just read the file character-wise/ using the read function/using getline until a space or other terminating character is obtained. Then you could switch the index of the matrix to start obtaining the next string...
Was This Post Helpful? 0
  • +
  • -

#7 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 651
  • View blog
  • Posts: 2,230
  • Joined: 31-December 10

Re: How to put strings from file in an Array [help]

Posted 29 October 2011 - 11:09 AM

Does it have to be an array or could you use a vector? If you can use a vector, then should use that over an array, because with vectors you don't need to know the size of the vector, it will take care of the size on its own and you just have to insert(push_back) each string into the vector. The vector also has a sort function which would make your job easier unless you have to write the sort out yourself.
Was This Post Helpful? 0
  • +
  • -

#8 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 277
  • View blog
  • Posts: 1,783
  • Joined: 20-September 08

Re: How to put strings from file in an Array [help]

Posted 29 October 2011 - 12:58 PM

View Postvividexstance, on 29 October 2011 - 02:09 PM, said:

Does it have to be an array or could you use a vector? If you can use a vector, then should use that over an array, because with vectors you don't need to know the size of the vector, it will take care of the size on its own and you just have to insert(push_back) each string into the vector. The vector also has a sort function which would make your job easier unless you have to write the sort out yourself.

There is a sort for arrays also ... but it seems, from the top post, that a bubble sort is to be coded.

The problem reduces to reading back from file a long string of words that have a space then a comma after each word ... (except the last word has no comma or space after it).

One simple way to handle this would be to read the file back into a single long string.

Then using an istringstream object iss formed from that long string, get each word into a growing dynamic array of strings ( or better, a growing STL vector of strings) ...

Note: below 'word' is a type string object ...

You could use the comma as the delimiter, if you use
while(getline(iss, word, ',' )) ...
and then trim the trailing space(s) from each word before adding that word to your array (or vector) ...

Or ... you could just use
while(iss >> word) ...
and erase the first char of the word if it is a comma char, before adding it to your growing container of strings ...

Or ... without using stringstream, you could just read the file word by word something like this:
while( getline(fin, word, ',') )
{
   unsigned pos = word.find_first_of(' ');
   if( pos != string::npos ) word.erase(pos); // trim any end spaces
   // add word to container ...
}



You also might like to see the examples here ...
http://developers-he...index.php/topic,2019.msg2921.html#msg2921

This post has been edited by David W: 30 October 2011 - 08:53 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1