C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 22nd, 2003, 11:52 AM
Jak_Jan Jak_Jan is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Posts: 2 Jak_Jan User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
read from file one line at a time using fscanf

Hi!

I would like to know how I can read from a file, one line at a time using the function fscanf().

Actually, ideally, character by character as I need to store the characters in an array.

This should be done in C

Thanks

Reply With Quote
  #2  
Old November 22nd, 2003, 12:07 PM
pianomahnn pianomahnn is offline
Can this change?
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Posts: 266 pianomahnn User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 m 52 sec
Reputation Power: 10
Send a message via AIM to pianomahnn Send a message via Yahoo to pianomahnn
Ah, the always pleasant homework problem.

Welcome newbie. We don't do your homework, but lets take a simple look at that fscanf() function.

As I whip out my old C book, I see the format of the fscanf() function:
Quote:
fscanf(input stream, control string, . . . );


That is quite nice.

Seeing as strings are stored as character arrays in C, there is no other way to store them.

There are a few ways you could go about reading a line from the file.

1) Assuming each line is a set number of bytes, simply read that entire line into a char[].

2) Using a looping structure to (a) read until a newline character is encountered, or (b) until EOF.

I'm going to assume each line length is not know, so number 2 would work best in this situation.

In this situation, the code to read the character would be:
Code:
fscanf(infile, "%c", line);


So, you have fscanf reading a character from infile to the line array.

If you want to try method (1):
Code:
fscanf(infile, "%24s", line);


This will reads in a line of 24 characters to the variable line array.

So, you've got that going on for yah. Now it's up to you to figure out the rest.
__________________
Quote:
Originally posted by assemblysucks
Pretty much nobody even uses assembly anymore so its pretty pointless to be asked to do this crap.

Reply With Quote
  #3  
Old November 22nd, 2003, 12:21 PM
Jak_Jan Jak_Jan is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Posts: 2 Jak_Jan User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
As you might have gathered I am not too good at this.

How would you write the conditional for the newline character?

Reply With Quote
  #4  
Old November 22nd, 2003, 01:56 PM
pianomahnn pianomahnn is offline
Can this change?
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Posts: 266 pianomahnn User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 m 52 sec
Reputation Power: 10
Send a message via AIM to pianomahnn Send a message via Yahoo to pianomahnn
Then practice, practice, practice.

The newline character is '\n'.

Reply With Quote
  #5  
Old November 22nd, 2003, 07:17 PM
ClayDowling ClayDowling is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2002
Location: Flint, MI
Posts: 328 ClayDowling User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 19 m 25 sec
Reputation Power: 11
Jak_Jan,

Since you're so lost, it would be a really good idea to get together with your professor. Professors have office hours, and that's what you need. He will help you with your homework.
__________________
Clay Dowling
Lazarus Notes
Articles and commentary on web development
http://www.lazarusid.com/notes/

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > read from file one line at a time using fscanf

Developer Shed Advertisers and Affiliates



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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap