This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

fgets and fscanf

 
Grace Green
Ranch Hand
Posts: 79
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Campbell Ritchie
Sheriff
Pie
Posts: 47270
52
  • 0
  • Mark post as helpful
  • send pies
  • 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?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Pie
Posts: 47270
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Fred. I am not too hot on regexes.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Pie
Posts: 47270
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ernest Friedman-Hill wrote:Fred?
Sorry
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic