Jump to content

How to know the total number of items from external file by using array and function

- - - - -

  • Please log in to reply
5 replies to this topic

#1
kuang

kuang

    Newbie

  • Members
  • Pip
  • 4 posts
C++.......as stated above...thanks for any answer

#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;

int main()
{
   ifstream file( "items.txt" ) ;
   string line ;
   vector<string> lines ;
   while( getline( file, line ) ) lines.push_back( line ) ;
   cout << "#items: " << lines.size() << '\n' ;
}

Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#3
kuang

kuang

    Newbie

  • Members
  • Pip
  • 4 posts
if i don want to use string and vector?becauce we not yet learn vector

#4
mebob

mebob

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 490 posts
Just input each "item" into a variable, increment a counter, and loop through that until EOF is found.
Latinamne loqueris?

#5
kuang

kuang

    Newbie

  • Members
  • Pip
  • 4 posts
if we do not know what is the item in the external file,what should we do to calculate the total number of items?

#6
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
Read it as binary data and count new lines? Does this work?
A conclusion is where you got tired of thinking.
#define class struct    // All is public.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users