jeudi 26 mars 2015

Need help printing out number of items per txt file


I'm new to C++ and just started working with some hw problems from my class. I'm having problems with this last bit where I need to print out the number of items that each text file has.


This is the problem: You have been asked to gather some statistics on a dataset. This dataset consists of some number of files, each with some number of floating point values. You are given a file named "file_names.txt" that has the names of all the data files listed inside. You do not know how many files are listed in "file_names.txt" but you know that it is at most 50. Write a program that lists the name of each data file and the number of items in that data file.


Here is my work so far



#include "stdafx.h"
//Windows Visual studio seems to want me to add this in order for it to work
#include <iostream>
#include <fstream>
#include <string>
using namespace std;


int main()
{
string fileNames;
ifstream infile ("file_names.txt");

while (infile >> fileNames)

{

cout << fileNames << endl; // Prints out string



ifstream file_names(fileNames.c_str());

const int MAX_SIZE = 100;
float array[MAX_SIZE];
int array_size = 0;

while (file_names >> array [array_size++])
{
cout << array_size << endl;
}


}


return 0;


}


The problem I'm having is that it's printing every time the counter goes up but I only need the last number (60). It's a total of 3 txt files with each 60 items btw. Thanks in advance for the help.




Aucun commentaire:

Enregistrer un commentaire