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

Threaded View

  1. #1
    Join Date
    Apr 2010
    Posts
    13

    Reading in only first couple lines of input file

    I'm trying to create a program that reads in data for 3 separate days and analyzes it. The data file has 72 lines (24 for each day). I only want to read in data for the first day (first 24 lines), but I cannot figure out how to.
    -------------------------------------------------------------------------------------------------------
    #include <iostream> // Needed for normal cin & cout statements
    #include <fstream> // Needed to read or write files on disk
    #include <cstdlib> // Needed for exit
    #include <cmath> // Needed for certain math functions
    #include <string> // Needed to read strings
    using namespace std;
    int main()
    {
    // Initializes variables
    double Data1, Data2, Data3, Total(0);
    int k;

    // Opens data file
    ifstream infile;
    infile.open("data.txt");

    if(!infile)
    {
    cout<<"Unable to open file: Data.txt\n"; // Displays error message if data file cannot be opened
    system("pause"); // Pauses command window
    exit (0); // Exits program
    }
    else
    {
    for(k=1; k<=24; k++) //Counter to read first 24 lines
    {
    while(infile>>Data1>>Data2>>Data3) // Reads in 3 data items
    {
    if(Data1 > 1) // Ignores Data1 if it is 1 or 0
    Total=Total+Data1; // Adds up Data1 for first 24 lines
    }
    }
    cout<<Total<<endl;
    }
    system("pause"); //Pauses console window
    return (0); // Ends program
    }
    Last edited by chara76; April 11th, 2010 at 12:17 PM.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured