0

I have been a longtime viewer of Stack Overflow, but this is my first question. As such, please forgive and correct me if I am missing any important details or information!

I am just beginning to program in C. I'm trying to create a program that will take in a text file I specify via my console on run time and using stdin. This text will will be formatted in the following 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 are the month, day, and year. After that the following lines contain data. The first number on each line represents the amount of data readings following it. I need to perform calculations based on the numbers following the first number after each line. So far I have been using the scanf function to obtain the data for the date and print it out as a heading.

#include <stdio.h>

int main()      
{     

 // Define Variables

 int month;
 int day;
 int year;


 // Scan text file for month, day and year

 scanf("%d", &month);
 scanf("%d", &day);    
 scanf("%d", &year);

 // Print out date and headers

 printf("\nTest date: %d/%d/%d", month, day, year);
 printf("\nLED Lumens\n");
 return 0;
 }

I need to be able to take the numbers following the first number in each line. I will then use these numbers to calculate various things (which I don't need help with). My question is what's the best way to do this? My idea would be to use scanf to get the first number in the row, then loop until the number of values in the line is reached, scanning the number each time.

So, if I scan the first number and it's a 3, I will loop and scan the next 3 numbers. I will then perform the calculations for those numbers, then repeat for each line.

Is this the right way to go about this, and will this work? Mainly I'm wondering how scanning the data from the text works. Every time I use the scanf function does it just obtain the next number in my text file. Does it work regardless that my data is on the following line?

Also note that I have not learned any of the advanced functions for dealing with text files. Hence, I am sticking to scanf to obtain the data.

Thanks! Let me know if I need any more details or clarification.

Update

Thanks to everyone who responded. I appreciate explaining how the scanf worked in the text file. I was able to get my program working. As for everyone who also responded with different functions and methods I could use, thanks! I will be sure to try those out at some point. I realize that there are probably easier and better ways to go about my task than the scanf function.

2
  • 2
    Use fgets() to read each line, then sscanf() to parse the buffer. Check results of both. Commented Oct 8, 2014 at 2:13
  • 1
    With scanf(), input fields are separated by whitespace (space, newline, and tab)... Additionally, the format string may contain multiple conversion specifications [e.g. scanf( "%d%d%d", &month, &day, &year );]... I know this isn't an 'answer', per se, but I wanted to note that the newlines shouldn't be trouble. Commented Oct 8, 2014 at 3:39

3 Answers 3

1

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

To have something more robust, you should follow chux's advice and : - first read full lines with fgets - then use sscanf the way you describe, but at least you are sure to be correctly line synchronized and able to detect a missing number in input file.

Sign up to request clarification or add additional context in comments.

3 Comments

Note that scanf() won't care about where the line breaks appear. That's why it is almost certainly better to read a line at a time and then parse each line in turn.
@JonathanLeffler: Fully agree with you. But as OP stated Hence, I am sticking to scanf to obtain the data, if proposed first solution with only scanf ...
At one level, it's fine to stick with scanf() because the OP says he wants to, but doing so means that the code cannot validate the data layout. It cannot tell whether everything is on one line, or each number is on its own line, perhaps with blank lines in between, or any hybrid layout, because scanf() doesn't care about newlines except as white space separating numbers it tries to read. Generally, I'll try to help with the direct question (as you did), but where the approach has problems, I will suggest a better alternative.
1

scanf and friends are too complicated for your simple task.

  • I'd use fgets to read the lines of the file into a buffer.
  • Then use strtoul to obtain the numbers from within. Its second parameter can be used to find the end position of each number.
  • The blanks between the numbers are skipped by strtoul without problems.

Comments

0

Here, you will need a file pointer to work with files.

FILE* fp;
int temp;

//then open the file you want to edit
//fopen(fp,"location of file with file name","mode of opening")
fp = fopen("Home/info.txt","a");

//now read through the file 
fscanf(fp,"%i",&temp);

To have a better understanding on file handling you can have look at on this tutorials:

2 Comments

I'd be happier if you showed checking that fopen() succeeded, and that fscanf() succeeded.
yea that would have been better. Thanks for the suggestion.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.