Friday 15 June 2012

C Programming - Scanning information from text file for use in Program -



C Programming - Scanning information from text file for use in Program -

i have been longtime viewer of stack overflow, first question. such, please forgive , right me if missing of import details or information!

i origin programme in c. i'm trying create programme take in text file specify via console on run time , using stdin. text will formatted in next way:

10 2 2012 3 765 821 764 5 856 976 847 757 789 4 838 842 832 815 4 809 815 789 765

the first 3 numbers month, day, , year. after next lines contain data. first number on each line represents amount of info readings next it. need perform calculations based on numbers next first number after each line. far have been using scanf function obtain info date , print out heading.

#include <stdio.h> int main() { // define variables int month; int day; int year; // scan text file month, day , year scanf("%d", &month); scanf("%d", &day); scanf("%d", &year); // print out date , headers printf("\ntest date: %d/%d/%d", month, day, year); printf("\nled lumens\n"); homecoming 0; }

i need able take numbers next first number in each line. utilize these numbers calculate various things (which don't need help with). question what's best way this? thought utilize scanf first number in row, loop until number of values in line reached, scanning number each time.

so, if scan first number , it's 3, loop , scan next 3 numbers. perform calculations numbers, repeat each line.

is right way go this, , work? i'm wondering how scanning info text works. every time utilize scanf function obtain next number in text file. work regardless info on next line?

also note have not learned of advanced functions dealing text files. hence, sticking scanf obtain data.

thanks! allow me know if need more details or clarification.

update

thanks responded. appreciate explaining how scanf worked in text file. able programme working. responded different functions , methods use, thanks! sure seek out @ point. realize there easier , improve ways go task scanf function.

the algo describe should work ... provided input file correctly formatted and test result of different scanf stop on end of file (return value <= 0).

to have more robust, should follow chux's advice , : - first read total lines fgets - utilize sscanf way describe, @ to the lowest degree sure correctly line synchronized , able observe missing number in input file.

c text scanf

No comments:

Post a Comment