Win a copy of Terraform in Action this week in the Cloud forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

fgets and fscanf

 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Is this use of fgets and fscanf correct ?
It seems to skip lines ...
I am working on this legacy C code, and very little experience in C
 
Marshal
Posts: 74393
335
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How large is LINESZ? How large is the buffer and how long is the String you are trying to read? Remember a 20-letter String occupies 21 spaces in a char*, so you need LINESZ to be 1 more than you think.
I would suggest you find somebody round there who knows lots of C. I can make a few hints, but I don't think my C is up to telling you anything definite. Sorry.
I would also suggest you check that regular expression; it appears to be something which isn't a \n followed by a \n. I didn't know you could use a regex after the %. Check how many characters that will match, and also whether the last line writes 3 characters at a time.

Anybody else able to help?
 
author and iconoclast
Posts: 24203
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Although that looks like a regular expression, it's not; it's just a character class (a negated one, because of the "^"). That's a legal call to fscanf; it will read everything up to and including a newline, and put everything but the newline into "linein" (which better be a pointer to a large-enough char array).

But if this is real code, I'm not quite sure what it's trying to do. The fgets() reads up to LINESZ characters, but stops at a newline -- generally it's going to read one line of text. If it successfully reads a line, then the fscanf call immediately reads another line into the same buffer, and so the line read by fgets() is ignored. That does sound like the "skipping lines" problem that the poster mentions.

Unless there are lines that are deliberately supposed to be skipped, my suggestion is to comment that fscanf() call out, recompile, and run the program again -- that may fix all your problems right there.
 
Campbell Ritchie
Marshal
Posts: 74393
335
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Fred. I am not too hot on regexes.
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24203
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Thank you Fred. I am not too hot on regexes.



Fred?
 
Grace Green
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, that is what I did (commenting out fscanf) and it worked.

There were several other little issues with processing the input ... I fixed those too.
I am going to replace the legacy C code as soon as I can, unfortunately I could not do it at this release ...

 
Campbell Ritchie
Marshal
Posts: 74393
335
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ernest Friedman-Hill wrote:Fred?

Sorry
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic