Thread: Opening file and loading data into Vector

  1. #1
    Registered User
    Join Date
    Dec 2003
    Location
    Fremont, CA
    Posts
    9

    Opening file and loading data into Vector

    I'm having trouble trying to load data from a file.
    I have an array that I'm saving to a file using this function:
    Code:
    void option_6() { //Save To File
    	cout << "\nEnter the name you'd like to save your file as: ";
    	cin >> FileName;
    	
    	outFile.open(FileName, ios::out);
    	outFile << station.size() << "\n";
    	for(int i = 0; i < station.size(); i++) {
    		outFile << station[i].StationDesignation << "\n" << station[i].TemperatureF << "\n" << station[i].TemperatureC << "\n" << station[i].DailyCount << "\n";
    	}
    	outFile << Average << "\n" << AverageC;
    	outFile.close();
    	
    	cout << "File saved as " << FileName << endl;
    }
    The outputted file is:
    Code:
    2
    Fremont
    55
    12.7778
    5.5
    Tustin
    55
    12.7778
    5.5
    55
    12.7778
    The first line is the size of the vector (stationSize)
    lines 2-5 and 6-9 are elements of the vector (station[i].StationDesignation, station[i].TemperatureF, station[i].TemperatureC, and station[i].DailyCount)
    and 10-11 are two more variables (Average and AverageC).
    *The data was generated from previous parts of the program.*

    I'm trying to read the data from the file back into those same variables using the following function, but I'm not sure exactly what I'm doing wrong.
    Code:
    void option_7() { //Read From File
    	int stationSize;
    	cout << "Enter the name of the file you want to open: ";
    	cin >> FileName;
    	
    	outFile.open(FileName, ios::in);
    	outFile >> stationSize;
    	station[stationSize];
    	for(int i = 0; i < station.size(); i++) {
    		outFile >> station[i].StationDesignation >> station[i].TemperatureF >> station[i].TemperatureC >> station[i].DailyCount;
    	}
    	outFile >> Average >> AverageC;
    	outFile.close();
    	
    	cout << "Opened " << FileName << "." << endl;
    }
    I've attached the code for the whole program, if it'll help. Although I must warn, it's pretty messy.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    What do you think "station[stationSize]" is supposed to do? Don't you want to create a vector of stations?

  3. #3
    Registered User
    Join Date
    Dec 2003
    Location
    Fremont, CA
    Posts
    9
    Yeah. the size of the vector changes dynamically as more stations are added on.
    But as I'm loading the file for the first time, the vector has nothing inside it, so I thought I needed to declare the size of it before loading data (like Temperature and DailyCount) back in.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by soopah256 View Post
    Yeah. the size of the vector changes dynamically as more stations are added on.
    But as I'm loading the file for the first time, the vector has nothing inside it, so I thought I needed to declare the size of it before loading data (like Temperature and DailyCount) back in.
    Then do so. Hint: you affect the size of the variable by using something like .resize().

  5. #5
    Registered User
    Join Date
    Dec 2003
    Location
    Fremont, CA
    Posts
    9
    Thanks for your help! I got it to work with the resize() function.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Error when loading 3ds file
    By The Wazaa in forum C++ Programming
    Replies: 6
    Last Post: 01-24-2006, 08:27 AM
  2. multiple file loading. so fruturated! help!
    By psychopath in forum Game Programming
    Replies: 5
    Last Post: 05-09-2005, 05:13 PM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. need help with .md3 file loading
    By Shadow12345 in forum Game Programming
    Replies: 2
    Last Post: 12-06-2002, 04:06 PM
  5. Loading a struct with data from a file
    By TankCDR in forum C Programming
    Replies: 1
    Last Post: 10-28-2001, 08:58 AM