Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 03-10-2005, 10:59 PM   #1
Registered: Aug 2003
Distribution: Slackware
Posts: 69

Rep: Reputation: 15
File Input, how to get number of items?

I have a file to input into one of my C++ programs. I can get input and all that, but I was wondering if it is possible to take the file and get the number of items (each item is seperated by whitespace) and put that into a variable. The variable and the data in the array is all of type int.

THank you.
Old 03-11-2005, 02:31 AM   #2
LQ Guru
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 658Reputation: 658Reputation: 658Reputation: 658Reputation: 658Reputation: 658
Do you mean without reading the data? If the data is of fixed size, you could calculate the number of items from the size. If the data ends with a newline, then divide the size by the item size +1.
Or you could precede the file with metadata containing the number of items.
Another option is to read up to a sentinel and have the count after the sentinel. Then you could position the file pointer at an offset from the end of the file and read just the count.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Creating Linked Lists from items in a file SeniorSE Programming 5 09-14-2005 11:53 PM
How to limit number of items in .recently-used neo_in_matrix Linux - Newbie 2 06-09-2005 12:04 PM
Having rm or mv take file input BlueOrb Linux - Newbie 4 06-29-2004 10:42 PM
terminal items to file g00ral Linux - General 2 05-09-2004 06:39 AM
input from a file ?? FredrikN Programming 5 03-30-2002 01:53 AM

All times are GMT -5. The time now is 04:00 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration