File IO with Numeric Data

How do I manipulate float data from a file

Page 1 of 1

1 Replies - 425 Views - Last Post: 22 December 2009 - 01:24 PM Rate Topic: -----

Topic Sponsor:

#1 Freddoopie850  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 15-October 09

File IO with Numeric Data

Posted 22 December 2009 - 01:18 PM

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


Is This A Good Question/Topic? 0
  • +

Replies To: File IO with Numeric Data

#2 OliveOyl3471  Icon User is offline

  • Everybody's crazy but me!
  • member icon

Reputation: 133
  • View blog
  • Posts: 6,581
  • Joined: 11-July 07

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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1