Staff: Mentor

Your program probably won't know how many lines are in the data file, so you can't declare a number of arrays at compile time, and fill each of them with one line of input.

To get around this problem you can allocate storage from the heap. You can write a loop that
1. reads a line of input.
2. Allocates a block of memory using a memory allocation function such as malloc().
3. Stores the address of the block of memory somewhere for later use.

To use this technique you should be very familiar with pointer usage, since that's how you will need to access each of your blocks of memory.

Does this make the pointer start reading from the next line? (after the end of the first line). This is the critical problem I'm having basically. I read the first complete line but after that I'm not able to read the next line because the pointer stars reading the whitespaces even after the line had ended.

Staff: Mentor

Alright, how should I modify the following line to get only till the end of line and not the entire file?:

while (fgets (s,1000,f)!=NULL)

That's what fgets does - read a line of characters. It copies characters from the input stream to the array, and stops copying when it reaches a newline character, or the end-of-file marker, or when it has copied one less character than the middle parameter specifies. It stores a null character after the last character in the string.

In your line of code above, you probably aren't going to encounter any lines that are 1000 characters long, so you can reduce that number to something more appropriate.

Staff: Mentor

Does this make the pointer start reading from the next line? (after the end of the first line). This is the critical problem I'm having basically. I read the first complete line but after that I'm not able to read the next line because the pointer stars reading the whitespaces even after the line had ended.

I have no idea what this does, but I'm pretty sure nothing good. Since you are using standard I/O functions such as scanf and fgets, you should read the documentation for these functions so that you can use them correctly.

Now, through the above code, I'm able to read P1 and it's time, i.e 0. But after that, I think it should run for all Ps and there corresponding time given in front of them. But after running for the first process, the loop does not go any further.

Now, through the above code, I'm able to read P1 and it's time, i.e 0. But after that, I think it should run for all Ps and there corresponding time given in front of them. But after running for the first process, the loop does not go any further.

What's the problem in the above code?

Comments:

In the line with fgets, you are attempting to read in up to 1000 characters. Your input file is unlikely to have that many characters in one line.

What happens if f == 0? The code will print "Could not open the file.\n", and this is good, but it will continue executing the next line after that, and this is not good.

The i variable seems to be both undeclared and undefined. If i is undeclared, the compiler will issue an error. If i is declared by undefined, the values in i, i + 1, and i + 2 will be garbage, so there's no telling what will happen when you try to read s, s[i + 1], and s[i + 2].
[*]The first time through your code, you are apparently looking for P1. You should probably be looking for P0 the first time through, based on your description of the data file.
[*]The line i = i + 4; replaces the garbage value in i by garbage + 4, which is still garbage.
[*]The fscanf line inputs a single character into the variable a. I don't understand the purpose of fscanf here.

I think your best course of action is to start fresh with a new algorithm. Since your data is formatted in a certain way, and consists of alpha characters (the Ps) and numbers, it's might be better to use fscanf for lines the odd lines (lines 1, 3, 5, and so on), and something else for the even lines.

You haven't said what your program will do with the data in the even-numbered lines. What information do you have about the even-numbered lines. In your example, the 2nd and 4th lines both have 7 numbers in them. Is that always the case? If not, do these lines have a minimum number of numbers and a maximum number of numbers? The code you write has to take this into consideration.

To formulate your algorithm, it would be helpful to write down, in words, what your program needs to do with the data. From that description it will be much easier to write the code that does that. Trying to write code without a clear understanding of what the code needs to do is a complete waste of time.

You should think of the computer as a very stupid, but very fast machine. It will do exactly what you tell it to do, but if you don't understand what it needs to do, you will not be able to give it the instructions to do what you want it to do.