semi-noob to c++ question

This is a discussion on semi-noob to c++ question within the C++ Programming forums, part of the General Programming Boards category; Ok I want to read 30 numbers from a text file "temperature.txt" into an array capable of holding 30 values ...

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    5

    semi-noob to c++ question

    Ok I want to read 30 numbers from a text file "temperature.txt" into an array capable of holding 30 values [0-29].
    Each number is on a seperate line
    34
    67
    63
    etc...
    so array[0] = 34
    array[1] = 67
    array[2] = 63
    etc...
    Problem is i have no idea on how to read from a file, read each line one after the other while saving each number into the array.
    Please help.

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    You create an input stream with your file, and then you read from that stream just like you would when using cin>>:

    Code:
    #include<iostream>
    #include <fstream>
    using namespace std;
    
    int main()
    { 
    	ifstream inFile("C:\\TestData\\input.txt");
    	const int size = 3;
    	int numbers[size];
    
    	for(int i=0; i<size; i++)
    	{
    		inFile>>numbers[i];
    	}
    
    	for(i=0; i<size; i++)
    		cout<<numbers[i]<<endl;
    
    	return 0;
    }

  3. #3
    Registered User
    Join Date
    Jun 2003
    Posts
    5
    Thank you. Works perfectly.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. quick noob question
    By thanatos1 in forum C# Programming
    Replies: 2
    Last Post: 06-17-2009, 09:28 PM
  2. another noob question
    By clb2003 in forum C Programming
    Replies: 4
    Last Post: 02-12-2009, 01:28 PM
  3. Noob printf question
    By lolguy in forum C Programming
    Replies: 3
    Last Post: 12-14-2008, 08:08 PM
  4. Very noob question :(.
    By GamerProduction in forum Tech Board
    Replies: 4
    Last Post: 04-14-2007, 06:40 AM
  5. Noob question ( little dos program )
    By Demon1s in forum C++ Programming
    Replies: 13
    Last Post: 04-04-2003, 09:28 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21