Jump to content

fscanf for a certain amount of lines

- - - - -

  • Please log in to reply
5 replies to this topic

#1
th72468

th72468

    Newbie

  • Members
  • PipPip
  • 17 posts
I'm working with lightning data of the form:

2010/08/01,00:00:00.196558, 19.3757,-100.9560, 16.4, 9
2010/08/01,00:00:00.057130, 19.3016,-101.0137, 17.3, 9
2010/08/01,00:00:02.169931, 13.3377, -87.6697, 26.0, 5
2010/08/01,00:00:02.979120, 6.9769, 107.8329, 6.2, 5
2010/08/01,00:00:04.244443, 15.7306, 104.0247, 16.3, 8
2010/08/01,00:00:05.089689, 48.9474,-103.4495, 25.6, 8

and would like for my fscanf function:

  k=0;
  while(fscanf(in,"%d/%d/%d,%d:%d:%f, %lf, %lf, %lf, %d", &YYYY[k], &DD[k], &MM[k], &s1[k], &s2[k], &s3[k], &lat[k], &lon[k], &DE[k], &sense[k]) != EOF) k++;
  fclose(in);

to only scan for the first say 50k lines. I'm quite naive when it comes to knowing the specifics of code, I just kind of replicate what I did in undergrad without really paying attention to why. It would be nice to learn one of these days. Anyways, it's the EOF portion that's messing with me. My input file is 313k + lines. I just want the first 50k lines fscanf-ed searched for criteria (lat/lon domain (4th and 3rd from right columns)), and then printed to an output file. Once I did the first 50k lines, I want to move to the next 50k lines (50,001 - 100,000), append to the output file, and repeat until EOF.

#2
DarkLordofthePenguins

DarkLordofthePenguins

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 409 posts
Any reason you can't just use a for loop that goes from 0 to 50000?
Programming is a journey, not a destination.

#3
th72468

th72468

    Newbie

  • Members
  • PipPip
  • 17 posts
It just gets stuck.

  for(k=0;k=N;k++) {
    fscanf(in,"%d/%d/%d,%d:%d:%f, %lf, %lf, %lf, %d", &YYYY[k], &DD[k], &MM[k], &s1[k], &s2[k], &s3[k], &lat[k], &lon[k], &DE[k], &sense[k]);
  }

N would be number of lines wanted to scan.

#4
th72468

th72468

    Newbie

  • Members
  • PipPip
  • 17 posts
It prints nothing off like it's stuff in a loop while within the fscanf.

#5
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
  for(k=0;k=N;k++) {
This will not work. Regardless of what you put in the for loop body, this will always result in an infinite loop unless you change the value of N or N is equal to 0. k=N does not check to see if k is equal to N, instead it assigns the value of N to k, and returning that value! You instead want to do this:
  for(k=0;k<N;k++) {
Since the for loop will continue so long as the result of k<N is true.
Wow I changed my sig!

#6
th72468

th72468

    Newbie

  • Members
  • PipPip
  • 17 posts
yea. that works. duh. I feel stupid. Thanks a lot!




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users