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);
}
(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);
}