Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   Data input (http://www.programmingforums.org/showthread.php?t=8958)

can342man Mar 20th, 2006 5:16 PM

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:

Ooble Mar 20th, 2006 5:31 PM

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);
}


DaWei Mar 20th, 2006 5:37 PM

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.


All times are GMT -5. The time now is 9:30 AM.

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