0

I am trying to take a text file and take the integers inside the file and transfer them into a vector in which can be read into different functions.

This is what i have so far:

int main(int argc, char *argv[] )
{
vector<int> buff;
argv[1] = "input_24_0.txt";

if (argc < 2)
{
    std::cout << "usage: " << argv[0] << " <filename>\n";
    return 2;
}
std::ifstream fin(argv[1]);
if (fin)
{
    std::stringstream ss;
    // this copies the entire contents of the file into the string stream
    ss << fin.rdbuf();
    // get the string out of the string stream
    std::string contents = ss.str();
    std::cout << contents;
    // construct the vector from the string.
    std::vector<int> buff(contents.begin(), contents.end());
}
else 
{
    std::cout << "Couldn't open " << argv[1] << "\n";
    return 1;
}

clock_t t1, t2, t3, t4;

int maxSum;

t1 = clock();
maxSum = maxSubSum1(buff);
cout << "MaxSubSum1 is " <<  maxSum << endl;
cout << double( clock() - t1 )
/ (double)CLOCKS_PER_SEC<< " seconds." << endl;
t1 = clock() - t1;

t2 = clock();
maxSum = maxSubSum2( buff );
cout << "MaxSubSum2 is " <<  maxSum << endl;
cout << double( clock() - t2)
/ (double)CLOCKS_PER_SEC<< " seconds." << endl;
t2 = clock() - t2;

t3 = clock();
maxSum = maxSubSum3(buff );
cout << "MaxSubSum3 is " <<  maxSum << endl;
cout << double( clock() - t3 )
/ (double)CLOCKS_PER_SEC<< " seconds." << endl;
t3 = clock() - t3;

t4 = clock();
maxSum = maxSubSum4( buff );
cout << "MaxSubSum4 is " <<  maxSum << endl;
cout << double( clock() - t4 )
/ (double)CLOCKS_PER_SEC<< " seconds." << endl;
t4 = clock() - t4;

system("pause");

return 0;
 }
user977154
  • 1,045
  • 4
  • 19
  • 39
  • NG:**argv[1] = "input_24_0.txt";**,Local redifine?:std::vector buff(contents.begin(), contents.end()); – BLUEPIXY Oct 05 '11 at 11:49

1 Answers1

0

If the numbers in the file are separated by whitespace, you could do the following:

 std::ifstream iStream;
 iStream.open("filename.ext");
 int tempVariable;

 if (iStream) {
   while (iStream >> tempVariable) {
      // Any routines to check the value of the number if necessary
      vector.push_back(tempVariable);
   }
   iStream.close();
 }
 else
   // Error routine

If the numbers are not separated by whitespace, you can use your string stream approach, and loop through the stream converting each char into it's decimal value.

  • Sorry.. I was trying to provide a snippet of how the code might look. Obviously you would declare an int variable that you would put values in one at a time, do any necessary checking of the value read from the file, then store it in the vector. –  Oct 05 '11 at 02:55
  • ya i declared the tempVariable to an int. i assumed. And it doesnt take any routines all it does it put all the ints into a vector so i can use the vector in the arguments in the other functions for my program. – user977154 Oct 05 '11 at 03:06
  • Then the code sample above placed in your main() function will work assuming the values are separated by whitespace (space, tab, line break, etc). –  Oct 05 '11 at 03:11
  • My functions take in these parameters:int maxSubSum4(const vector & a); – user977154 Oct 05 '11 at 03:42
  • Change the parameter to maxSubSum4(vector& a); Remove the 'const' keyword. Const makes it read-only. –  Oct 05 '11 at 04:02