Google Answers Logo
View Question
 
Q: (C++help) how to store the "file.txt" into arrays 2D ? ( No Answer,   4 Comments )
Question  
Subject: (C++help) how to store the "file.txt" into arrays 2D ?
Category: Computers > Programming
Asked by: ozcheaper-ga
List Price: $2.00
Posted: 17 Jan 2006 20:40 PST
Expires: 16 Feb 2006 20:40 PST
Question ID: 434844
Im kind of new comer for C++ and I do need a little help from you guys.

I have a 2d array with name :
array[100][4]

also I have file.txt :
Bertie Bee,Honey Suckle,123 Beezwaz St,190
Computer Cat,N/A,45 Hard Dr,170
Slippery Snake,N/A,Grassy Lane,240
Slow Snail,N/A,65 Slimey St,120
Black Crow,N/A,Airways Rd,280
Jacky Lizzard,Frilly Lizzard,1 Fence Post Dr,140


My question is : how do I store each piece information into an array,
check below ...
Bertie Bee ==> should be in array[0][0]
Honey Suckle ==> should be in array[0][1] 
123 Beezwaz St ==> should be in array[0][2] 
190 ==> should be in array[0][3]

Computer Cat ==> should be in array[1][0]
N/A ==> should be in array[1][1]
45 Hard Dr ==> should be in array[1][2]
170 ==> should be in array[1][3]

and so on.

also if you guys can give me some idea how to do the other way (from
array into file.txt).

appreciated for your help.
Answer  
There is no answer at this time.

Comments  
Subject: Re: (C++help) how to store the "file.txt" into arrays 2D ?
From: nickericson-ga on 18 Jan 2006 23:00 PST
 
Depends on which libraries you are using with C++ (managed, unmanaged,
etc) which way is preferred.

Check out the strtok method (http://www.cplusplus.com/ref/cstring/strtok.html).

Your code would do something like:
while ( not at end of file )
  Read a line of text into a string buffer
  Place the string up to the 1st , into array location [ lines_read ][ 0 ]
  ... Repeat until done with line.


Because this looks like homework I would also say to be sure to
deallocate any strings that you allocate, close the file you open,
deallocate the array, etc.

If you don't want to use strtok you can 
1) determine where the "," is in the string.
2) allocate a string of that length & copy over from the buffer to this new string
3) repeat at 1 while you still have buffer left.
Subject: Re: (C++help) how to store the "file.txt" into arrays 2D ?
From: gahlawat-ga on 19 Jan 2006 22:56 PST
 
question id 434844

--> i think that using array is not a good approach ,
   one solution to this problem is by using a user defined index , index
may be static or dynamic depending on your requirment.

--> as far as the storage from "file.txt" is considered , it is simple by
using looping statement or by defining a tempary class &
  the data is first stored in this temp class for manipulation of data &
final data is transfered to the index.
Subject: Re: (C++help) how to store the "file.txt" into arrays 2D ?
From: daniel_dearlove-ga on 21 Jan 2006 10:29 PST
 
You could try something like :

void read_func( std::string* arr )
{
    for( int x = 0; x < 3; ++x )
        std::getline( std::cin, (*arr)++, ',' );

    std::getline( std::cin, arr );
}

Your question appears to assume the 2D array is an array of std::string

A better way to structure the code would be :

struct Person_details
{
    std::string name;
    std::string house_name;
    std::string street;
    std::string house_number;
}

then overload operator >>() function so you can use the STL

std::vector<Person_details> arr;

std::copy( std::istream_iterator<Person_details>( std::cin ),
    std::istream_iterator<Person_details>(),
    std::back_inserter( arr ) );

To do the reverse :

void print_func( std::string *arr )
{
    for( int x = 0; x < 3; ++x )
        std::cout << (*arr)++ << ',';

    std::cout << *arr << std::endl;
}

Using the STL and the structure, above:

copy( arr.begin(), arr.end(),
    std::ostream_iterator<Person_details>( std::cout, "\n" ) );

assuming the operator <<() function is overloaded

It is trivial to input or output from a std::fstream instead of the
standard istream or ostream

DISCLAIMER: not tried any of the code out, just brain-farted
Subject: I think you cannot do that with a 2D array
From: sasidhar_nitw-ga on 24 Feb 2006 06:54 PST
 
1)array[0][0] can store only a character but not a string like "Bertie Bee".


Create a 3D array and u can do the thing  u needed more easily like

Bertie Bee ==> will be in array[0][0]
Honey Suckle ==> will be in array[0][1] 
123 Beezwaz St ==> will be in array[0][2] 
190 ==> will be in array[0][3]

so on

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy