Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 20th, 2006, 4:16 PM   #1
can342man
Newbie
 
can342man's Avatar
 
Join Date: Jan 2006
Location: London, Ontario, Canada
Posts: 16
Rep Power: 0 can342man is on a distinguished road
Data input

I just want to get some ideas on how to import numerical data from a file into a C++ program.

Is it easier to use cin >> into an array or the read/write I/O methods.

Also when importing the data should it be in binary format?

Really appreciate it if anyone has ideas.

Thanks

:banana:
__________________
Greatness courts failure and solitude.
--- Anonymous
can342man is offline   Reply With Quote
Old Mar 20th, 2006, 4:31 PM   #2
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Use the std::ifstream class. It works similarly to std::cin:
std::ifstream myFile ("myFile.txt");
std::vector<int> data;

int temp;
while (myFile >> temp)
{
    data.push_back(temp);
}
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Mar 20th, 2006, 4:37 PM   #3
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
In *nix, all files are binary mode. In Windows, the default is text mode, and it can cause problems under certain conditions (these have been discussed in several threads on the forum). Binary data may be encoded in different ways, which may be the source of your 'binary' question, but the data in the file IS binary.

Cin is a stream. It has a number of methods other than the extraction operator (>>). These methods perform in differing ways in order that you may make the most efficient or productive operations with your file. You should read up a bit on iostreams and basic_iostreams. You should also understand that all input operations can fail. ALWAYS test your operations for success. This, too, has been covered several times on the forum. I would suggest that you browse or search the forum, read the forum's rules/FAQ post, and a "How to Post a Question" thread (to be found at the top of the C forum). There are a number of other recent threads explaining some of the attitude behind these recommendations.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:27 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC