Hi,
I am reading strings from a text file using fscanf("%s", currToken);
This returns strings delimited by whitespace. How can I tell if the
string was followed by a carriage return in the file?
Michael 11 2947
In article <11**********************@g47g2000cwa.googlegroups .com>,
Michael McGarry <mi*************@gmail.com> wrote: I am reading strings from a text file using fscanf("%s", currToken);
This returns strings delimited by whitespace. How can I tell if the string was followed by a carriage return in the file?
The whitespace character (including newline) that terminates the
string will be "put back" into the input stream. Therefor to
determine whether currToken was followed by a carriage return,
read the next character, test to see if it was carriage return,
and if not then ungetc() the character to put it back in the
input stream.
By the way, do you really mean "carriage return", ascii character 13?
Or do you mean "the operating system's end of line sequence, whether that
be carriage return or linefeed or some combination of the two or
something else completely" -- which C compactly encodes as \n ?
--
Programming is what happens while you're busy making other plans.
Michael McGarry wrote: Hi,
I am reading strings from a text file using fscanf("%s", currToken);
This returns strings delimited by whitespace. How can I tell if the string was followed by a carriage return in the file?
You can read lines at a time, from a text file,
and find the white space in the resulting string,
if you're really looking to tokenise on white space.
--
pete
Thanks for your reply, I just want to check for the EOL sequence
whatever that may be on some arbitrary OS. I want this code to be
portable.
Thanks for any additional guidance,
Michael
"Michael McGarry" <mi*************@gmail.com> wrote: I am reading strings from a text file using fscanf("%s", currToken);
This returns strings delimited by whitespace. How can I tell if the string was followed by a carriage return in the file?
Others have given you an answer to your direct question. But let me make
another suggestion: if you need to know this because you want to read an
entire line, rather than separate words, you should also look at
fgets(). It will read a complete line for you without stopping for other
whitespace than newlines. It will also let (in fact, make) you specify a
buffer size, which means buffer overrun errors are less likely. So does
fscanf(), but it doesn't require it, and as you use it above it does not
know how large your buffer is and will merrily write over the end of it.
Richard rl*@hoekstra-uitgeverij.nl (Richard Bos) writes: "Michael McGarry" <mi*************@gmail.com> wrote:
I am reading strings from a text file using fscanf("%s", currToken);
This returns strings delimited by whitespace. How can I tell if the string was followed by a carriage return in the file?
Others have given you an answer to your direct question. But let me make another suggestion: if you need to know this because you want to read an entire line, rather than separate words, you should also look at fgets(). It will read a complete line for you without stopping for other whitespace than newlines. It will also let (in fact, make) you specify a buffer size, which means buffer overrun errors are less likely. So does fscanf(), but it doesn't require it, and as you use it above it does not know how large your buffer is and will merrily write over the end of it.
You also need to decide what to do if the input line is longer than
your buffer. In that case, fgets will read as much of the line as it
can, the buffer won't be terminated by a newline character, and the
remainder of the line will be left on the input stream to be read
later.
--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
"Michael McGarry" <mi*************@gmail.com> wrote I am reading strings from a text file using fscanf("%s", currToken);
This returns strings delimited by whitespace. How can I tell if the string was followed by a carriage return in the file?
Don't use fscanf().
The library functions are fine as long as you want the narrow purpose for
which they were designed. As soon as you want to do something a bit unusual,
such as check for carriage returns, your best bet is to build a custom
function on top of fgetc().
(You might need to open your file in binary mode, some OSes suppress the
'\r' character).
"Malcolm" <re*******@btinternet.com> writes: "Michael McGarry" <mi*************@gmail.com> wrote I am reading strings from a text file using fscanf("%s", currToken);
This returns strings delimited by whitespace. How can I tell if the string was followed by a carriage return in the file? Don't use fscanf(). The library functions are fine as long as you want the narrow purpose for which they were designed. As soon as you want to do something a bit unusual, such as check for carriage returns, your best bet is to build a custom function on top of fgetc(). (You might need to open your file in binary mode, some OSes suppress the '\r' character).
I suspect when the OP asked about a "carriage return", he really meant
an end-of-line, not a literal '\r' character. Opening a text file in
binary mode is seldon a good idea; the possible variations are bigger
than just the Unix vs. Windows "LF", vs. "CR/LF" line terminators.
--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Hi,
Thanks for all the advice. I just wound up using fgetc() and as soon as
it returned whitespace that was not a space, I assume end of line. In
my case the file contains space seperated fields, so a whitespace that
is not a space must be EOL. Is there explicitly an EOL character that
is OS independent?
I would like to make my design more robust to actually check for an
EOL.
Michael
>Thanks for all the advice. I just wound up using fgetc() and as soon as it returned whitespace that was not a space, I assume end of line. In my case the file contains space seperated fields, so a whitespace that is not a space must be EOL.
Warning: this ignores the existence of tabs, as well as other
characters considered to be white space. This may not be a problem
for *your* files, but you can't assume that for everyone.
Is there explicitly an EOL character that is OS independent?
I would like to make my design more robust to actually check for an EOL.
In C, lines from a text file, opened in text mode, as read by
functions like fgetc(), fgets(), fread(), and *scanf() end in '\n'.
It is irrelevant how the line ending is represented *on disk*.
C translates the line ending to '\n' on reading, and from '\n'
to whatever is the appropriate line ending on writing.
All bets are off for files being read in binary mode.
Gordon L. Burditt
Thanks Gordon,
I will explicitly read with fscanf(file,"%s"); until fgetc() returns
'\n'
Michael
Michael McGarry wrote: Hi,
Thanks for all the advice. I just wound up using fgetc() and as soon as it returned whitespace that was not a space, I assume end of line. In my case the file contains space seperated fields, so a whitespace that is not a space must be EOL. Is there explicitly an EOL character that is OS independent?
I would like to make my design more robust to actually check for an EOL.
Michael
Choice of fgetc() and/or fgets() makes a lot of sense to me. Also,
reading from a text file is a very exact exercise. You must know exactly
how this file was constructed in order to read it successfully. There
are few Standards in this regard. You have to know your file.
Generally, text files are comprised of lines of characters, each line
ending with the '\n' character. There may be any number of lines.
Whether the last line in the file is ended with '\n' is an
implementation detail. Usually it is.
Again, we have to know our file to make sense of it. You mention above
separated fields in text files. This suggests to me a database table of
rows and columns presented to us as text. Simply separating fields with
spaces is not normally useful.
--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein --- This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Suresh Kumaran |
last post by:
Hi All,
Does anybody know the sytax in VB.NET to write the
contents of a multiline text box to a text file?
Appreciate help.
Suresh
|
by: fabrice |
last post by:
Hello,
I've got trouble reading a text file (event viewer dump) by using the
getline() function...
After 200 - 300 lines that are read correctly, it suddenly stops
reading the rest of the...
|
by: Lionel B |
last post by:
Greetings,
I need to read (unformatted text) from stdin up to EOF into a char
buffer; of course I cannot allocate my buffer until I know how much
text is available, and I do not know how much...
|
by: Eric Lilja |
last post by:
Hello, I have a text file that contains a number of entries describing a
recipe. Each entry consists of a number of strings. Here's an example file
with only one entry (recipe):
Name=Maple Quill...
|
by: Magnus |
last post by:
allrite folks, got some questions here...
1) LAY-OUT OF REPORTS
How is it possible to fundamentaly change the lay-out/form of a report in
access? I dont really know it that "difficult", but...
|
by: Michael Mair |
last post by:
Cheerio,
I would appreciate opinions on the following:
Given the task to read a _complete_ text file into a string:
What is the "best" way to do it?
Handling the buffer is not the problem...
|
by: Sabin Finateanu |
last post by:
Hi I'm having problem reading a file from my program and I think it's from a
procedure I'm using but I don't see where I'm going wrong. Here is the code:
public bool AllowUsage()
{
...
|
by: dale zhang |
last post by:
Hi,
I am trying to save and read an image from MS Access DB based on the
following article:
http://www.vbdotnetheaven.com/Code/Sept2003/2175.asp
Right now, I saved images without any...
|
by: Amit Maheshwari |
last post by:
I need to read text file having data either comma seperated or tab seperated
or any custom seperator and convert into a DataSet in C# .
I tried Microsoft Text Driver and Microsoft.Jet.OLEDB.4.0...
|
by: The Cool Giraffe |
last post by:
Regarding the following code i have a problem.
void read () {
fstream file;
ios::open_mode opMode = ios::in;
file.open ("some.txt", opMode);
char *ch = new char;
vector <charv;
while...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| |