#include <iostream> #include <fstream> //needed for file functions #include <string> #include <vector> #include <sstream> //used for istringstream using namespace std; // this function takes a string pointer and pushes numbers into a // vector by tokenizing the string at spaces ' ' void parse(string *ext, vector<int>&types) { istringstream iss(*ext); string token; // you can replace ' ' with whatever you // would like to parse at while(getline(iss,token,' ')) types.push_back(atoi(token.c_str())); // push numbers into vector } int main() { ifstream in("CAR1837_Ch1_0.340_extract.txt"); //open file for reading if(!in.is_open()) //if the file is not open - exit exit(1); string line = ""; //the file will buffer every line into this string vector<int>numbers(0); // This vector will be used to store all of the data while(getline(in,line)) //loop through the file { //cout << line << endl; //output every line to the screen //numbers.push_back(atoi(line.c_str())); //atoi expects a const char so you must convert parse(&line,numbers); } // you now have all of the lines stored in the vector as doubles // lets output the vector for(int i=0; i<numbers.size(); i++) cout << numbers[i] << endl; //output to screen in.close(); //close file //pause window cin.ignore(); cin.get(); return 0; }
File IO with Numeric DataHow do I manipulate float data from a file
Page 1 of 1
1 Replies - 425 Views - Last Post: 22 December 2009 - 01:24 PM
Topic Sponsor:
#1
File IO with Numeric Data
Posted 22 December 2009 - 01:18 PM
Replies To: File IO with Numeric Data
#2
Re: File IO with Numeric Data
Posted 22 December 2009 - 01:24 PM
Thank you for using the code tags, but you might want to edit your post so that the code isn't all on one line, and all commented out.
Also, if you have a question or if your code isn't working properly, please explain what you need to know so we can help you.
Also, if you have a question or if your code isn't working properly, please explain what you need to know so we can help you.
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote




|