![]() |
|
#1
|
|||
|
|||
Using getline to read in dataHi, I've read these forums for help a lot of times, but this is my first time posting . Anyways, I'm looking for help on a problem I'm having reading in and storing data. The program is designed to read data in from a file, store the information for later calculations, and then save the data to an outfile. The data is information from the 2000 Presidential election, here is an example:
State Elec Vote Gore Bush Nader Buchanan Alabama 9 695602 944409 18349 6364 Alaska 3 79004 167398 28747 5192 Opening the file and saving the data to a file is simple enough for me, but I'm perplexed on how to read the data in, and what variables to store it under. I've tried using the getline function in a while loop, but I can never figure the syntax out, and I just end up getting frustrated. Also, I'm confused on how to avoid storing the first line into the variables (State Elec Vote Gore Bush Nader Buchanan), so they won't get full of a bunch of char value garbage. If anyone could show me an example of how this data could be read in a stored, I'd be most appreciative. Thanks again. |
|
#2
|
||||
|
||||
|
Hello krisbot. Long time listener, first time caller huh? Welcome to GIDForums.
I know that your original question was about getline. I don't know if you were intent on using getline, but I had to answer this anyway. Throughout this forum, scanf has a pretty bad reputation (with reason), but this program is perfect for scanf (IMHO). Anyway, I whipped this up real quick to show how this can be used: CPP / C++ / C Code:
It's not very fancy and it is entirely static in storage. Notice that I just read and throw away the first line since I know what the catagories are... My apologies if scanf is not what you want to use, but it is very good at reading structured data like this. Good luck! __________________
The best damn Sports Blog period. |
|
#3
|
||||
|
||||
|
Quote:
But to be completely safe, change all fscanf()'s to CPP / C++ / C Code:
__________________
Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough. -- Pearl Williams |
|
#4
|
|||
|
|||
|
Quote:
What compiler is used for that code? I use Microsoft Visual Studio C++ Pro (got it for free). Here is an example of what my code generally looks like: CPP / C++ / C Code:
Last edited by dsmith : 24-Oct-2004 at 16:48.
|
|
#5
|
||||
|
||||
|
Hi Krisbot. That is an ancient language known as "C"
![]() It should compile on any about any C compiler, there is nothing exotic there at all. As a personal preference, I like the stdio input/output much better than iostream & fstream. It is probably what I am used to, but to me, it is much easier to use. That is why I put the comment about your possible attachment to getline before I posted the code. Anyway, I think this is a great illustration of the ease of use of the fscanf function. Perhaps someone that is more fond of the fstream library will post an alternate solution using that. __________________
The best damn Sports Blog period. |
Recent GIDBlog
Stupid Management Policies by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| How to detect end of file with read() function call? | nkhambal | C Programming Language | 6 | 12-Oct-2004 02:08 |
| Transferring data between arrays | butters | C Programming Language | 3 | 07-Jul-2004 23:55 |
| [CONTEST?]Data Structure Test | dsmith | C Programming Language | 2 | 06-Jun-2004 16:13 |
| Automate a data change php form | mjfmn | MySQL / PHP Forum | 4 | 20-Oct-2003 10:37 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The