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 <lineI get "ab cd".
I wrote a C program, also below, that I _thought_ would do the same
thing, but when I print variable <lineI 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()appare ntly 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.FileRea der;
import java.io.FileWri ter;
import java.io.Buffere dReader;
import java.io.Buffere dWriter;
import java.io.PrintWr iter;
import java.io.IOExcep tion;
import java.io.FileNot FoundException;
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.readLi ne();
System.out.prin tln( "Read \"" + line + "\" from file \"Java.Txt
\".");
}
catch (FileNotFoundEx ception excptn)
{ System.err.prin tln( "Exception <FileNotFoundEx ception>
thrown.");
}
catch (IOException excptn)
{ System.err.prin tln( "Exception <IOExceptionthr own.");
}
}
}
############### ############### ############### ############### ########
#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);
} 9 2364
kvnsm...@hotmai l.com wrote:
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 <lineI get "ab cd".
I wrote a C program, also below, that I _thought_ would do the same
thing, but when I print variable <lineI 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()appare ntly 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.
On Mar 28, 2:43 pm, kvnsm...@hotmai l.com wrote:
<snip>
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 <lineI get "ab cd".
I wrote a C program, also below, that I _thought_ would do the same
thing, but when I print variable <lineI 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()appare ntly does? Any information on this
would be greatly appreciated.
<snip java code>
#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);
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.
--
Ioan - Ciprian Tandau
tandau _at_ freeshell _dot_ org (hope it's not too late)
(... and that it still works...)
On Mar 28, 2:43 pm, kvnsm...@hotmai l.com wrote:
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 <lineI get "ab cd".
I wrote a C program, also below, that I _thought_ would do the same
thing, but when I print variable <lineI 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()appare ntly does? Any information on this
would be greatly appreciated.
Why not just use fgets()?
Robert Gamble kv******@hotmai l.com writes:
I wrote a C program, also below, that I _thought_ would do the same
thing, but when I print variable <lineI 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()appare ntly does? Any information on this
would be greatly appreciated.
Use fgets instead of fscanf.
--
"Welcome to the wonderful world of undefined behavior, where the demons
are nasal and the DeathStation users are nervous." --Daniel Fox
Quite a number of you suggested that I use <fgets()inste ad 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_
On Mar 28, 3:44 pm, kvnsm...@hotmai l.com wrote:
<snip>
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.
--
Ioan - Ciprian Tandau
tandau _at_ freeshell _dot_ org (hope it's not too late)
(... and that it still works...) kv******@hotmai l.com writes:
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.
--
"What is appropriate for the master is not appropriate for the novice.
You must understand the Tao before transcending structure."
--The Tao of Programming kv******@hotmai l.com wrote:
>
Quite a number of you suggested that I use <fgets()inste ad 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.
--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net>
--
Posted via a free Usenet account from http://www.teranews.com This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: ai |
last post by:
Hi ,
I have an application which has to read files of size few mba nd read
them into array . Since i am using STL and std namespace to avoid the
error
error C2872: 'ifstream' : ambiguous symbol
when i include <iostream.h> with std namespace and <List>
|
by: Kay |
last post by:
1) If i want to read data from a txt file,
eg John; 23; a
Mary; 16; i
How can I read the above data stopping reading b4 each semi-colon and
save it in three different variables ?
2) If I enter a number, can I use to call a particular node ?
eg enter a number: 3
calling node of number 3
is it possible ?
|
by: Cal Lidderdale |
last post by:
My input line is i1,i2,i3,i4,i5,i6,i7,i8^,...i596,597, ...
14101,14102...NL/CR
very long line of data - I only want the first 8 items and
the delimiter between 8 & 9 is a carrot "^".
The line can end at the 100th item or the 40,000th item.
My code is:
char data, mynull;
|
by: nt91rx78 |
last post by:
Our college changes 18 weeks semester to 16 semester, so our CS professor cannot finish teaching the last important chapter which is related with my problw\em.
This is program C problem
Anyone can help me with this problem, please!!!!!!!!!
This is the problem:
3. Several input text files have been provided as input to your program.
a) Write a function to combine these files into a single file.
b) Write a function to take care of...
|
by: liujiaping |
last post by:
Hi, all.
I have a dictionary-like file which has the following format:
first 4
column 7
is 9
a 23
word 134
....
Every line has two columns. The first column is always an English
| |
by: David Mathog |
last post by:
Apologies if this is in the FAQ. I looked, but didn't find it.
In a particular program the input read from a file is supposed to be:
+ 100 200 name1
- 101 201 name2
It is parsed by reading the + character, and then sending the
remainder into fscanf() like
|
by: odin607 |
last post by:
I'm making a little program to track what items I sell/buy how much of them how much it was to buy it and when I bought it. This program is only C (or trying to keep it that way)
Theres a few files that im writing to to display all that information, but that's for another time.
This post is to ask how to read from one of the files (it brings in "All Sessions" data)
the file looks like this
Last Session
|
by: wilco |
last post by:
Hi, I want to copy a csv file into an array using fscanf and keep the blank spaces in the csv file rather than just skip over them to the next number.
there are too many spaces in the csv file to fill them all in manually
I'm using C
any clues how i could do this??
thanks, heres a sample of my code
|
by: George |
last post by:
1 0001000000000000001
2 0001000000000000001
3 10000011001000000000000001
4 10000011001000000000000001
5 10000011001000000000000001
6 10000011001000000000000001
7 10000011001000000000000001
8 10000011001000000000000001
9 10000011001000000000000001
10 10000011001000000000000001
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |