I have a question:
I have a file named "data.inp". This file have numbers:
6 3 5 4 5
2 5 3 1
5 8 7 9
2 5 6 3 5 8 4 6
If I want more line of numbers, I will add numbers.
But I don't know how to read file "data.inp" line by line. I think I should use fgets, but seem that fgets is used for char. Or I used fgetc to read all numbers, then I count '\n' (note: "enter"), add 1, I have number of line. Then maybe I read file by that way?
thanks in advance!;)
donaldunca 0 Light Poster
Recommended Answers
Jump to Postfgets() doesn't know the difference between characters, such as 'a', 'b', 'c' ... and numbers such as '1', '2', '3' ... '9'. In the file they are all just text. If you want to treat all those numbers as text, read the file line by line, then just use fgets() …
Jump to PostBut I don't know how to read file "data.inp" line by line. I think I should use fgets, but seem that fgets is used for char.
Yes, use fgets() to read the line, then you can use sscanf() to convert the line into numbers.
Jump to PostI don't know whether or not this may help:
http://www.daniweb.com/code/printsnippet444.html
Jump to PostOK. So use the information from my post and think about how you'd have to do it.
Or read the link from Dave. Two possible ways to handle this.
Jump to PostPost your code so that we can atleast point out the mistakes you are making...
All 17 Replies
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague
Dave Sinkula 2,398 long time no c Team Colleague
donaldunca 0 Light Poster
WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague
donaldunca 0 Light Poster
~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster
iamthwee
donaldunca 0 Light Poster
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague
donaldunca 0 Light Poster
WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague
~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster
John A 1,896 Vampirical Lurker Team Colleague
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
donaldunca 0 Light Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.