Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,179 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,373 people online right now. Registration is fast and FREE... Join Now!




How to read from a file until a space is found?

 
Reply to this topicStart new topic

How to read from a file until a space is found?

ana hina
7 Jul, 2008 - 05:46 AM
Post #1

New D.I.C Head
*

Joined: 7 Jul, 2008
Posts: 1

hey everyone, its a simple question, im a beginner in C, i have an input file with: 12 44 23 22 as the first line,

i want to read this line as it is and place each number in an array of integers, how could i do that? the fscanf method

reads the 12 only ( until the space )... thnx for the help...

** Edit ** Made text easier to read
User is offlineProfile CardPM
+Quote Post

lordms12
RE: How To Read From A File Until A Space Is Found?
7 Jul, 2008 - 08:53 AM
Post #2

D.I.C Regular
Group Icon

Joined: 16 Feb, 2008
Posts: 314



Thanked: 15 times
Dream Kudos: 225
My Contributions
* Read it as string %s

* Read it as integer %d
User is offlineProfile CardPM
+Quote Post

gabehabe
RE: How To Read From A File Until A Space Is Found?
7 Jul, 2008 - 10:52 AM
Post #3

Donkey DIC
Group Icon

Joined: 6 Feb, 2008
Posts: 5,556



Thanked: 99 times
Dream Kudos: 2650
Expert In: ruling the world.

My Contributions
It will take the space as a tokeniser, so it's nothing to worry about at this point. Just use the >> operator, like so:

cpp
for (int i = 0; i < 3; i++)
myFile >> array[i];

Hope this helps smile.gif
User is online!Profile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/4/08 11:06AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month