954,480 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

File I/O Help

I need help with a program. I need to know how to make it so the lines with a -1 will not show up. The code is linked to a file. The file looks like this:
4 112.7
154 115.5
4 116.4
321 -1
72 129.8
321 -1
154 219.0
72 128.8
555 110.8
555 137.6
432 86.7
432 -1

If the score was equal to -1 then it would be null. What would I put in here to make it so this happens.

#include <stdio.h>
#include <math.h>
#include <stdlib.h>

int main()
{

FILE *olympicfile;
float x;
int i;
int j;
int s;
float Scores[12][2];

olympicfile = fopen( "speed", "r" );
if (olympicfile == NULL)
{
 printf("olympicfile not available\n");
 exit(1);
}
for(i = 0; i < 12; i++)
 {
  for (j = 0; j < 2; j++)
  {
   fscanf(olympicfile, "%f", &x);
   Scores[i][j] = x;
  }
 }
 for(i = 0; i < 12; ++i)
  {
   for(j = 0; j < 2; ++j)
    {
     printf("%5.1f ", Scores[i][j], Scores[i][j]);
    }
   printf("\n");
  }
fclose( olympicfile );
}
FreakNFlow
Newbie Poster
4 posts since Feb 2010
Reputation Points: 10
Solved Threads: 0
 
if ( something != -1 ) {
  // do something
}

No?

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

You have to deal with the array index and the loop index i separately.

Only increment the array index if -1 is not read.
If you see the -1, do not increment the array index.

WaltP
Posting Sage w/ dash of thyme
Moderator
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

where would i put that bit of code at. every time i try something like that, nothing happens or it just gets rid of the -1 and leaves the runners number there with a blank space by it.

FreakNFlow
Newbie Poster
4 posts since Feb 2010
Reputation Points: 10
Solved Threads: 0
 

> printf("%5.1f ", Scores[i][j], Scores[i][j]);
What's wrong with this line?

Why not
printf("%5.1f %5.1f\n", Scores[i][0], Scores[i][1]);

and forego the inner loop.


Can you add a test now to stop it printing?

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: