dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: store new data in copy file

  1. #1
    Join Date
    Oct 2016
    Posts
    39

    store new data in copy file

    this is the code i have so far, and when i run it, it only stores one data item in the new file, I'm not quite sure what's wrong.


    Code:
    #include <iostream>
    #include <vector>
    #include <fstream>
    using namespace std;
    int main()
    {
    	//This file (data.txt) contains data from an instrument that measures sea surface temperature remotely.
    	ifstream fin;
    	ofstream sin;
    	int i = 0;
    	fin.open("C:/Temp/SSTdata.txt");
    	//Write a program that reads in the data from the file and stores it in a vector.
    	vector <double>temperatures;
    	double temp;
    	while (!fin.eof())
    	{
    		fin >> temp;
    		temperatures.push_back(temp);
    	}
    	sin.open("C:/Temp/copy_SSTdata.txt");
    	//test if open correctly
    	if (!sin)
    	{
    		cout << "unable to open output file." << endl;
    		system("pause");
    		return 1;
    	}
    	else
    	{
    		cout << "output file opened." << endl;
    	}
    	//Once all the data is read in find and replace all out of range values with the flag value -9999.
    	for (int i = 0; i < temperatures.size(); i++)
    		if (temperatures[i] < 0 || temperatures[i] > 40)
    			temperatures[i] = -9999;
    	for (int i = 0; i < temperatures.size(); i++)
    	cout << temperatures[i] << " ";
    	sin << temperatures[i] << " ";
    	cout << endl;
    
    	//put new data into file 
    
    	system("pause");
    		return 0;
    }

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,922

    Re: store new data in copy file

    Code:
    for (int i = 0; i < temperatures.size(); i++)
    	cout << temperatures[i] << " ";
    
    sin << temperatures[i] << " ";
    because look at how the code will be interpreted! There is only one statement (cout) controlled by the for loop.

    Note
    Code:
    while (!fin.eof())
    	{
    		fin >> temp;
    		temperatures.push_back(temp);
    	}
    can be improved to be
    Code:
    while (fin >> temp)
    	temperatures.push_back(temp);
    Also
    Code:
    for (int i = 0; i < temperatures.size(); i++)
    		if (temperatures[i] < 0 || temperatures[i] > 40)
    			temperatures[i] = -9999;
    Code:
    for (auto& t : temperatures)
    	if (t < 0 || t > 40)
    		t = -9999;
    and similarly for the other loop.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.4.0)

  3. #3
    Join Date
    Oct 2016
    Posts
    39

    Re: store new data in copy file

    thank you!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)