The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com



High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
read a file wich fscanf() in a function cdbug High Level Programming 2 10-06-2008 08:46 AM
To read and separate number and words in file and store to two new file using shell kamakshi s Shell Programming and Scripting 2 06-26-2008 07:39 AM
script to Read Error,jbase,voilation,segmentation words from a file Mujtaba khan Shell Programming and Scripting 3 03-30-2008 01:19 PM
how to read all the unique words in a text file aditya.ece1985 Shell Programming and Scripting 5 11-30-2007 02:26 AM
Read words from file and create new file using K-shell. bsrajirs Shell Programming and Scripting 4 06-01-2007 12:15 PM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 06-24-2009
Dedalus Dedalus is offline
Registered User
  
 

Join Date: May 2009
Location: Italy/France
Posts: 60
fscanf: read words from file

Hi

I have a file like that:

1 2 3 4 5 6 7 8

and I want print on stdout:

1 3 8

in other words i want choose what print out.
I was thinking to use fscanf as:
Code:
fscanf(file_in,"%d %d %d",&a, &b,&c);
but in this way i get:
1 2 3
Is there a solution using fscanf to obtain my purpose?

Thanks
D

---------- Post updated at 11:08 AM ---------- Previous update was at 10:51 AM ----------

hi
I solved it. it enough to insert a format type indicator, something like:

fscanf(file_in,"%d %*1hd %d ",&a, &b,&c)

here the references fscanf - C++ Reference.


D.
  #2 (permalink)  
Old 06-25-2009
mgessner mgessner is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 50
scanf is generally considered unsafe, as is fscanf for similar reasons.

You're much better off learning how to use fgets and sscanf to do your work.

fscanf will continue to read %d from your stream, skipping whitespace and stopping when it can't read another one, so you could have read them in a loop, discarding the ones you didn't want.
  #3 (permalink)  
Old 06-29-2009
Dedalus Dedalus is offline
Registered User
  
 

Join Date: May 2009
Location: Italy/France
Posts: 60
thanks for your advice. I'll keep in mind and I'll try the solution you suggest

D.
Reply

Bookmarks

Tags
file, fscanf

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 05:22 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0