How to Keep <fscanf()> to not Stop when it Comes to a Blank

K

kvnsmnsn

Over the course of my career I've transitioned from an Ada programmer
(am I dating myself?) to a C programmer to a Java programmer and now
back to a C programmer with the job I've currently started.

What I'd like to do is write a piece of C code that inputs to the pro-
gram a line written to a file. The Java code written below does
exactly what I want; it writes the <String> "ab cd" to file "Java.Txt"
and then reads it back in to variable <line>, so that when I print va-
riable <line> I get "ab cd".

I wrote a C program, also below, that I _thought_ would do the same
thing, but when I print variable <line> I get only "ab". Does anybody
know what I would have to do in C to get the program to read the en-
tire line into <line>, not just the first space-delimited portion of
the line like <fscanf()> apparently does? Any information on this
would be greatly appreciated.

---Kevin Simonson

"You'll never get to heaven, or even to LA,
if you don't believe there's a way."
from _Why Not_

####################################################################

import java.io.FileReader;
import java.io.FileWriter;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.PrintWriter;
import java.io.IOException;
import java.io.FileNotFoundException;

public class Bug
{
public static void main ( String[] arguments)
{
try
{ PrintWriter toDisk
= new PrintWriter( new BufferedWriter( new
FileWriter( "Java.Txt")));
toDisk.println( "ab cd");
toDisk.close();
BufferedReader fromDisk
= new BufferedReader( new FileReader( "Java.Txt"));
String line = fromDisk.readLine();
System.out.println( "Read \"" + line + "\" from file \"Java.Txt
\".");
}
catch (FileNotFoundException excptn)
{ System.err.println( "Exception <FileNotFoundException>
thrown.");
}
catch (IOException excptn)
{ System.err.println( "Exception <IOException> thrown.");
}
}
}

####################################################################

#include <stdio.h>

int main ( int argCount
, char** arguments)
{
FILE* toDisk = fopen( "C.Txt", "w");
fprintf( toDisk, "ab cd\n");
fclose( toDisk);
char line[ 1024];
FILE* fromDisk = fopen( "C.Txt", "r");
fscanf( fromDisk, "%s\n", line);
printf( "Read \"%s\" from file \"C.Txt\".\n", line);
}
 
B

Ben Pfaff

I wrote a C program, also below, that I _thought_ would do the same
thing, but when I print variable <line> I get only "ab". Does anybody
know what I would have to do in C to get the program to read the en-
tire line into <line>, not just the first space-delimited portion of
the line like <fscanf()> apparently does? Any information on this
would be greatly appreciated.

Use fgets instead of fscanf.
 
G

Guest

Over the course of my career I've transitioned from an Ada programmer
(am I dating myself?) to a C programmer to a Java programmer and now
back to a C programmer with the job I've currently started.

What I'd like to do is write a piece of C code that inputs to the pro-
gram a line written to a file. The Java code written below does
exactly what I want; it writes the <String> "ab cd" to file "Java.Txt"
and then reads it back in to variable <line>, so that when I print va-
riable <line> I get "ab cd".

I wrote a C program, also below, that I _thought_ would do the same
thing, but when I print variable <line> I get only "ab". Does anybody
know what I would have to do in C to get the program to read the en-
tire line into <line>, not just the first space-delimited portion of
the line like <fscanf()> apparently does? Any information on this
would be greatly appreciated. [...]
char line[ 1024];
FILE* fromDisk = fopen( "C.Txt", "r");
fscanf( fromDisk, "%s\n", line);

If you have to use fscanf for whatever reason, you can use %[^\n] to
read until the end of the line. However, keep in mind that it will /
not/ read the newline, so the next time you call fscanf with the same
format, no extra characters will be read. You'd need to explicitly
read the newline first, and then call fscanf again. Also keep in mind
that it provides no protection whatsoever against buffer overflows: if
your line can exceed 1024 characters, you're in trouble.

If you don't need to use fscanf, consider use fgets, or user functions
such as CBFalconer's ggets <http://cbfalconer.home.att.net/download/>
designed specifically to read a single line.
 
N

Nelu

On Mar 28, 2:43 pm, (e-mail address removed) wrote:
What I'd like to do is write a piece of C code that inputs to the pro-
gram a line written to a file. The Java code written below does
exactly what I want; it writes the <String> "ab cd" to file "Java.Txt"
and then reads it back in to variable <line>, so that when I print va-
riable <line> I get "ab cd".

I wrote a C program, also below, that I _thought_ would do the same
thing, but when I print variable <line> I get only "ab". Does anybody
know what I would have to do in C to get the program to read the en-
tire line into <line>, not just the first space-delimited portion of
the line like <fscanf()> apparently does? Any information on this
would be greatly appreciated.
<snip java code>

#include said:
int main ( int argCount
, char** arguments)
{
FILE* toDisk = fopen( "C.Txt", "w");
fprintf( toDisk, "ab cd\n");
fclose( toDisk);
char line[ 1024];
FILE* fromDisk = fopen( "C.Txt", "r");
fscanf( fromDisk, "%s\n", line);

Use fgets.
printf( "Read \"%s\" from file \"C.Txt\".\n", line);

You should get into the habit of checking the return values of the
functions you use. They may fail.
 
R

Robert Gamble

Over the course of my career I've transitioned from an Ada programmer
(am I dating myself?) to a C programmer to a Java programmer and now
back to a C programmer with the job I've currently started.

What I'd like to do is write a piece of C code that inputs to the pro-
gram a line written to a file. The Java code written below does
exactly what I want; it writes the <String> "ab cd" to file "Java.Txt"
and then reads it back in to variable <line>, so that when I print va-
riable <line> I get "ab cd".

I wrote a C program, also below, that I _thought_ would do the same
thing, but when I print variable <line> I get only "ab". Does anybody
know what I would have to do in C to get the program to read the en-
tire line into <line>, not just the first space-delimited portion of
the line like <fscanf()> apparently does? Any information on this
would be greatly appreciated.

Why not just use fgets()?

Robert Gamble
 
K

kvnsmnsn

Quite a number of you suggested that I use <fgets()> instead of
<fscanf()>; I tried that out and am having great results. Thanks! My
next question is, how do you tell when you've reached the end of file
using <fgets()>?

---Kevin Simonson

"You'll never get to heaven, or even to LA,
if you don't believe there's a way."
from _Why Not_
 
N

Nelu

On Mar 28, 3:44 pm, (e-mail address removed) wrote:
My
next question is, how do you tell when you've reached the end of file
using <fgets()>?

fgets returns NULL if it fails. If it fails you can use feof to check
whether the failure is a result of EOF being reached or not.
 
B

Ben Pfaff

My next question is, how do you tell when you've reached the
end of file using <fgets()>?

Did you read the description of fgets in your C reference manual?
comp.lang.c is not best used as a substitute for documentation.
 
C

CBFalconer

Quite a number of you suggested that I use <fgets()> instead of
<fscanf()>; I tried that out and am having great results. Thanks!
My next question is, how do you tell when you've reached the end
of file using <fgets()>?

Make sure you quote enough of the preceding article so that your
message stands by itself. There is no guarantee in Usenet than any
other messages have been, or ever will be, received.

fgets is fine as long as you know the maximum length of the input
line, otherwise you may have to go to lengths to detect and handle
partial lines. From the standard:

[#3] The fgets function returns s if successful. If end-of-
file is encountered and no characters have been read into
the array, the contents of the array remain unchanged and a
null pointer is returned. If a read error occurs during the
operation, the array contents are indeterminate and a null
pointer is returned.

You can also use ggets (non-standard, but written in standard C)
which avoids this problem. See:

<http://cbfalconer.home.att.net/download/>

The other file copying routine you can use is:

int ch;
while (EOF != (ch = getchar())) putchar(ch);

which uses the preassigned stdin and stdout files, which are
automatically opened and closed for you. They usually default to
the console, but most systems allow you to redirect them to other
devices or disk files. putchar and getchar are putc and getc to
predefined files.

Just never use gets. Under any circumstances. Not even then.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,836
Latest member
login dogas

Latest Threads

Top