-2

I'm a beginner at C programming and I would appreciate some help in order to understand the problem.

Alright so, I have a data file (input.dat) with data like this: (first line) 0 2 3 4 5; (second line) 1 2 3 5 4, (third line and so on...). I'm required to read the data one line at a time until the end of file and print it. This is what I have done so far:

int main(void)
{
float coeffs[5];
FILE *input;  /* File pointer to the input file */
fopen_s(&input, "input.dat", "r"); /* Location of the input file */
int count = 0;
/* Loops to read data set*/
while (fscanf_s(input, "%f %f %f %f %f ", &coeffs[0], &coeffs[1], &coeffs[2], &coeffs[3], &coeffs[4]) != EOF)
{
    printf("a=%.4f; b=%.4f; c=%.4f; d=%.4f; e=%.4f\n", coeffs[0], coeffs[1], coeffs[2], coeffs[3], coeffs[4]);
    count++;
}
return 0;
}

This is showing all of the lines in the data file at once. But this is not what I want. I need to read one line at a time and perform some calculations and conditions for that one line first before I move to the next line. So how can I do that?

Next problem is, for the first line, I need to implement a loop from -10 to +10 with increment of 2 (to get 11 results in total). For example the program will read the first line, display it on the screen, then for the first value -10, the program will calculate and again display something . Then it will do the same for -8, then for -6 and so on until +10. After the 11 results are displayed, the program will then and ONLY then, move to the second line and so on. Hence for each line in the data file, the program will have 11 results. How can I use the loop function with increment of 2 to achieve these 11 results?

I would appreciate if anyone can provide me a simple layout of the structure of the codes which I've to write. NOTE: The formats are a bit different than other compilers as I must use Microsoft Visual Studio to do it.

James
  • 11
  • What are the down-votes for? I'm just trying to understand the base of the problem. :/ – James Dec 07 '17 at 13:57
  • If your input is formatted with exactly five whitespace-separated numbers per line, then your code will indeed read one line at a time. It indeed does perform some calculations (`printf()` and `count++`) for each such line. What's not working about that? – John Bollinger Dec 07 '17 at 13:59
  • 1
    I imagine the downvotes are related in part to that, and in part to the fact that you show no effort to solve the second part of your question. – John Bollinger Dec 07 '17 at 14:01
  • The output of my code displays all the lines of the data file at ONCE when I run. Is it possible that it only displays the first line, then if I press ENTER, it displays the second line and so on? – James Dec 07 '17 at 14:04
  • I actually have no idea how to solve the second part of the question being a beginner. That's why I'm here. – James Dec 07 '17 at 14:05
  • The program's output for each line immediately follows that for the previous line because you don't output anything else in between. You don't notice a delay because computers can do this sort of thing very swiftly. If you want to do additional work per input line (prompt, read a response from the user, ...) then code for that would go into the loop. – John Bollinger Dec 07 '17 at 14:09
  • 1
    As for you being a beginner, this is not a tutorial site, nor really even a help site. That we provide help is a happy side effect of our mission to create a database of on-topic questions and answers that will be useful to others. The downvoters do not think your question meets our criteria, which you can and should read about here: https://stackoverflow.com/help/asking. – John Bollinger Dec 07 '17 at 14:13
  • Ahhh I get it now. So it indeed does read one line at a time. – James Dec 07 '17 at 14:13
  • @John...Oh I see. I will try to keep that in mind next time I'm asking a question. Thanks for your time to comment. Really appreciated! – James Dec 07 '17 at 14:20
  • It seems you really are missing some fundamentals of programming - repeating a numeric pattern and running a code block for each number in there is day 1 or day 2 of learning how to code. That is likely the reason for the downvotes - we can't write a coding basic tutorial on every question here. Try reading this to start fiiling up your knowledge gap: https://www.tutorialspoint.com/cprogramming/c_loops.htm – jsbueno Dec 07 '17 at 14:24
  • I will take a look at that. Thank you very much! @jsbueno – James Dec 07 '17 at 14:29

1 Answers1

1

Add your calculations to your while loop. You are reading one line at a time anyway.

If you want to loop from -10 to 10 with increments of 2, use a for loop.

for(count = -10; count <= 10; count = count + 2)
    {
        // Calculations
    }
Julius Hörger
  • 383
  • 3
  • 11