![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Jan 2006
Location: London, Ontario, Canada
Posts: 16
Rep Power: 0
![]() |
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 |
|
|
|
|
|
#2 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
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);
} |
|
|
|
|
|
#3 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|