Counting Int's in a File

This is a discussion on Counting Int's in a File within the C++ Programming forums, part of the General Programming Boards category; Sorry if this is out there, I cannot find what I'm looking for. What I'm trying to do is read ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Location
    Freeport, IL
    Posts
    32

    Counting Int's in a File

    Sorry if this is out there, I cannot find what I'm looking for.

    What I'm trying to do is read a list of number from a txt file and then count how many total numbers there is in the txt file.

    Cannot seem to figure this out.

    Thanks

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    You can just read each number one at a time in a loop, then increment a counter each time.

    What have you tried that didn't work?

  3. #3
    Registered User
    Join Date
    Mar 2005
    Location
    Freeport, IL
    Posts
    32
    I guess I'm new to File I/O how does it read the numbers, does it differentiate between the spaces and the number like 123? And info on a good file I/O Tut.

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,412
    There's heaps of tutorials on this site
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    Simply using >> in a loop should be all you need to read in a file populated with distinct numeric values (ints) seperated by whitespace.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 08:35 AM
  2. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  3. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 01:58 AM

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