wood burning stoves*
The moose likes C / C++ and the fly likes fgets and fscanf Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Languages » C / C++
Bookmark "fgets and fscanf" Watch "fgets and fscanf" New topic
Author

fgets and fscanf

Grace Russo
Ranch Hand

Joined: Nov 08, 2010
Posts: 64

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

Joined: Oct 13, 2005
Posts: 38075
    
  22
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

Joined: Jul 08, 2003
Posts: 24183
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38075
    
  22
Thank you Fred. I am not too hot on regexes.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

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


Fred?
Grace Russo
Ranch Hand

Joined: Nov 08, 2010
Posts: 64
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

Joined: Oct 13, 2005
Posts: 38075
    
  22
Ernest Friedman-Hill wrote:Fred?
Sorry
 
jQuery in Action, 2nd edition
 
subject: fgets and fscanf
 
Similar Threads
Not able to send a mail using JAVA code
Problem reading a file through BufferedReader
Why is my loop not working properly?
dependency vs association again???
C to Java Translation, HELP!